Skip to content

Commit

Permalink
Merge pull request #244 from dcSpark/byron-multi-era
Browse files Browse the repository at this point in the history
Byron multi era
  • Loading branch information
rooooooooob authored Jul 17, 2023
2 parents 648460b + fcd191b commit 556fe37
Show file tree
Hide file tree
Showing 40 changed files with 12,042 additions and 31 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ members = [
"crypto/rust",
"crypto/wasm",
"multi-era/rust",
"multi-era/wasm"
"multi-era/wasm",
"multi-era/wasm/json-gen"
]

# exclude old crate structure to avoid error in it
Expand Down
27 changes: 22 additions & 5 deletions core/rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum DeserializeFailure {
InvalidStructure(Box<dyn std::error::Error>),
MandatoryFieldMissing(Key),
NoVariantMatched,
NoVariantMatchedWithCauses(Vec<DeserializeError>),
OutOfRange{
min: usize,
max: usize,
Expand Down Expand Up @@ -70,12 +71,12 @@ impl DeserializeError {
None => Self::new(location, self.failure),
}
}
}

impl std::error::Error for DeserializeError {}

impl std::fmt::Display for DeserializeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt_indent(&self, f: &mut std::fmt::Formatter<'_>, indent: u32) -> std::fmt::Result {
use std::fmt::Display;
for _ in 0..indent {
write!(f, "\t")?;
}
match &self.location {
Some(loc) => write!(f, "Deserialization failed in {} because: ", loc),
None => write!(f, "Deserialization: "),
Expand Down Expand Up @@ -109,6 +110,14 @@ impl std::fmt::Display for DeserializeError {
write!(f, "Mandatory field {} not found", key)
}
DeserializeFailure::NoVariantMatched => write!(f, "No variant matched"),
DeserializeFailure::NoVariantMatchedWithCauses(errs) => {
write!(f, "No variant matched. Failures:\n")?;
for e in errs {
e.fmt_indent(f, indent + 1)?;
write!(f, "\n")?;
}
Ok(())
},
DeserializeFailure::OutOfRange{ min, max, found } => write!(f, "Out of range: {} - must be in range {} - {}", found, min, max),
DeserializeFailure::RangeCheck { found, min, max } => match (min, max) {
(Some(min), Some(max)) => write!(f, "{} not in range {} - {}", found, min, max),
Expand All @@ -130,6 +139,14 @@ impl std::fmt::Display for DeserializeError {
}
}

impl std::error::Error for DeserializeError {}

impl std::fmt::Display for DeserializeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fmt_indent(f, 0)
}
}

impl From<DeserializeFailure> for DeserializeError {
fn from(failure: DeserializeFailure) -> DeserializeError {
DeserializeError {
Expand Down
12 changes: 8 additions & 4 deletions core/rust/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ impl From<cbor_event::Len> for CBORReadLen {
// to facilitate mixing with crates that use preserve-encodings=false to generate
// we need to create it from cbor_event::Len instead
fn from(len: cbor_event::Len) -> Self {
Self::new(match len {
cbor_event::Len::Len(n) => cbor_event::LenSz::Len(n, fit_sz(n, None, true)),
cbor_event::Len::Indefinite => cbor_event::LenSz::Indefinite,
})
Self::new(len_to_len_sz(len))
}
}

pub fn len_to_len_sz(len: cbor_event::Len) -> cbor_event::LenSz {
match len {
cbor_event::Len::Len(n) => cbor_event::LenSz::Len(n, fit_sz(n, None, true)),
cbor_event::Len::Indefinite => cbor_event::LenSz::Indefinite,
}
}

Expand Down
4 changes: 4 additions & 0 deletions multi-era/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.57"
schemars = "0.8.8"
wasm-bindgen = { version = "0.2", features=["serde-serialize"] }

# only for declaring hash types
bech32 = "0.7.2"
hex = "0.4.0"
Loading

0 comments on commit 556fe37

Please sign in to comment.