Skip to content

Commit

Permalink
error macro
Browse files Browse the repository at this point in the history
  • Loading branch information
m00sey committed Feb 2, 2023
1 parent 9e8d383 commit d596f39
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions src/core/indexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use base64::Engine;

use crate::{
core::indexer::tables::{BothSigCodex, CurrentSigCodex},
error::{Error, Result},
error::{err, Error, Result},
};

use super::util;
Expand Down Expand Up @@ -42,15 +42,15 @@ impl Indexer {
mut ondex: Option<u32>,
) -> Result<Indexer> {
if code.is_empty() {
return Err(Box::new(Error::EmptyMaterial("empty code".to_string())));
return err!(Error::EmptyMaterial("empty code".to_string()));
}

let code = code.to_string();
let szg = tables::sizage(&code)?;
if szg.fs == 0 {
return Err(Box::new(Error::InvalidVarRawSize(format!(
return err!(Error::InvalidVarRawSize(format!(
"unsupported variable size: code = '{code}'"
))));
)));
}

// both hard + soft code size
Expand All @@ -59,24 +59,24 @@ impl Indexer {

const SIXTY_FOUR: u32 = 64;
if index > (SIXTY_FOUR.pow(ms - 1)) {
return Err(Box::new(Error::InvalidVarIndex(format!(
return err!(Error::InvalidVarIndex(format!(
"Invalid index '{index}' for code '{code}'"
))));
)));
}

if let Some(o) = ondex {
if szg.os > 0 && o > SIXTY_FOUR.pow(szg.os - 1) {
return Err(Box::new(Error::InvalidVarIndex(format!(
return err!(Error::InvalidVarIndex(format!(
"Invalid ondex '{o}' for code '{code}'"
))));
)));
}
}

if CurrentSigCodex::has_code(&code) && ondex.is_some() {
return Err(Box::new(Error::InvalidVarIndex(format!(
return err!(Error::InvalidVarIndex(format!(
"Non None ondex '{o}' for code '{code}'",
o = ondex.unwrap()
))));
)));
}

if BothSigCodex::has_code(&code) {
Expand All @@ -86,25 +86,25 @@ impl Indexer {
}
} else if let Some(o) = ondex {
if o != index && szg.os == 0 {
return Err(Box::new(Error::InvalidVarIndex(format!(
return err!(Error::InvalidVarIndex(format!(
"Non matching ondex '{o}' and index '{index}' for code = '{code}'."
))));
)));
}
}

// compute fs from index
let fs = if szg.fs == u32::MAX {
if cs % 4 != 0 {
return Err(Box::new(Error::InvalidCodeSize(format!(
return err!(Error::InvalidCodeSize(format!(
"Whole code size not multiple of 4 for variable length material. cs = '{cs}'."
))));
)));
}

if szg.os != 0 {
return Err(Box::new(Error::InvalidCodeSize(format!(
return err!(Error::InvalidCodeSize(format!(
"Non-zero other index size for variable length material. os = '{o}'.",
o = szg.os
))));
)));
}

(index * 4) + cs
Expand All @@ -116,9 +116,9 @@ impl Indexer {

let raw_ = raw[..(raw_size as usize)].to_vec();
if raw_.len() != raw.len() {
return Err(Box::new(Error::RawMaterialError(format!(
return err!(Error::RawMaterialError(format!(
"Not enougth raw bytes for code = '{code}' and index = '{index}' , expected = '{raw_size}'"
))));
)));
}

Ok(Indexer {
Expand Down Expand Up @@ -151,16 +151,16 @@ impl Indexer {

fn exfil(&mut self, qb64: &str) -> Result<()> {
if qb64.is_empty() {
return Err(Box::new(Error::EmptyMaterial("empty qb64".to_string())));
return err!(Error::EmptyMaterial("empty qb64".to_string()));
}

let first = qb64.chars().next().unwrap();
let hs = tables::hardage(first)? as usize;
if qb64.len() < hs {
return Err(Box::new(Error::Shortage(format!(
return err!(Error::Shortage(format!(
"Need '{s}' more characters.",
s = (hs - qb64.len())
))));
)));
}

let hard = &qb64[..hs];
Expand All @@ -171,10 +171,10 @@ impl Indexer {
let ms = szg.ss - szg.os;

if qb64.len() < cs as usize {
return Err(Box::new(Error::Shortage(format!(
return err!(Error::Shortage(format!(
"Need '{l}' more characters",
l = ((cs as usize) - qb64.len()),
))));
)));
}

let index = util::b64_to_u32(&qb64[hs..(hs + ms as usize)])?;
Expand All @@ -187,10 +187,10 @@ impl Indexer {
}
// not zero or None
if ondex.is_some() && ondex.unwrap() != 0 {
return Err(Box::new(Error::Value(format!(
return err!(Error::Value(format!(
"Invalid ondex = '{o}' for code = '{hard}'.",
o = ondex.unwrap()
))));
)));
}
} else if szg.os != 0 {
ondex = Some(util::b64_to_u32(odx)?);
Expand All @@ -202,26 +202,26 @@ impl Indexer {
let mut fs = szg.fs;
if fs != 0 {
if (cs % 4) != 0 {
return Err(Box::new(Error::Validation(format!(
return err!(Error::Validation(format!(
"Whole code size not multiple of 4 for variable length material. cs = '{cs}'"
))));
)));
}

if szg.os != 0 {
return Err(Box::new(Error::Validation(format!(
return err!(Error::Validation(format!(
"Non-zero other index size for variable length material. os = '{o}'",
o = szg.os
))));
)));
}

fs = (index * 4) + cs;
}

if qb64.len() < (fs as usize) {
return Err(Box::new(Error::Shortage(format!(
return err!(Error::Shortage(format!(
"Need '{m}' more chars.",
m = { fs as usize - qb64.len() }
))));
)));
}

let qb64_ = &qb64[..fs as usize];
Expand All @@ -243,7 +243,7 @@ impl Indexer {

const TWO: i32 = 2;
if (pi & (TWO.pow(pbs) - 1)) != 0 {
return Err(Box::new(Error::Prepad()));
return err!(Error::Prepad());
}

raw = paw[ps as usize..].to_owned();
Expand All @@ -260,9 +260,9 @@ impl Indexer {

if li != 0 {
return if szg.ls == 1 {
Err(Box::new(Error::NonZeroedLeadByte()))
err!(Error::NonZeroedLeadByte())
} else {
Err(Box::new(Error::NonZeroedLeadBytes()))
err!(Error::NonZeroedLeadBytes())
};
}

Expand Down

0 comments on commit d596f39

Please sign in to comment.