mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 00:31:07 +00:00
Update to parity-scale-codec (#3232)
* WIP: update codec * WIP * compiling * WIP * rename parity-scale-codec to codec * WIP * fix * remove old comments * use published crates * fix expected error msg * bump version * fmt and fix * remove old comment * fix wrong decoding impl * implement encode like for structures * undo removal of old pending changes * trailingzeroinput * Apply suggestions from code review Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com> * update codec * fmt * version is 1.0.0 * show more error * fmt
This commit is contained in:
committed by
Bastian Köcher
parent
a0d442333f
commit
4ed67e03a4
@@ -19,7 +19,7 @@
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use crate::codec::{Decode, Encode, Input, Output};
|
||||
use crate::codec::{Decode, Encode, Input, Output, Error};
|
||||
|
||||
/// Era period
|
||||
pub type Period = u64;
|
||||
@@ -111,20 +111,22 @@ impl Encode for Era {
|
||||
}
|
||||
}
|
||||
|
||||
impl codec::EncodeLike for Era {}
|
||||
|
||||
impl Decode for Era {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
fn decode<I: Input>(input: &mut I) -> Result<Self, Error> {
|
||||
let first = input.read_byte()?;
|
||||
if first == 0 {
|
||||
Some(Era::Immortal)
|
||||
Ok(Era::Immortal)
|
||||
} else {
|
||||
let encoded = first as u64 + ((input.read_byte()? as u64) << 8);
|
||||
let period = 2 << (encoded % (1 << 4));
|
||||
let quantize_factor = (period >> 12).max(1);
|
||||
let phase = (encoded >> 4) * quantize_factor;
|
||||
if period >= 4 && phase < period {
|
||||
Some(Era::Mortal(period, phase))
|
||||
Ok(Era::Mortal(period, phase))
|
||||
} else {
|
||||
None
|
||||
Err("Invalid period and phase".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user