use crate::{
error::Error,
metadata::{
DecodeWithMetadata,
Metadata,
},
};
pub use scale_value::Value;
pub type DecodedValue = scale_value::Value<scale_value::scale::TypeId>;
pub use crate::tx::dynamic as tx;
pub use crate::constants::dynamic as constant;
pub use crate::storage::{
dynamic as storage,
dynamic_root as storage_root,
};
pub struct DecodedValueThunk {
type_id: u32,
metadata: Metadata,
scale_bytes: Vec<u8>,
}
impl DecodeWithMetadata for DecodedValueThunk {
type Target = Self;
fn decode_with_metadata(
bytes: &mut &[u8],
type_id: u32,
metadata: &Metadata,
) -> Result<Self::Target, Error> {
let mut v = Vec::with_capacity(bytes.len());
v.extend_from_slice(bytes);
*bytes = &[];
Ok(DecodedValueThunk {
type_id,
metadata: metadata.clone(),
scale_bytes: v,
})
}
}
impl DecodedValueThunk {
pub fn into_encoded(self) -> Vec<u8> {
self.scale_bytes
}
pub fn encoded(&self) -> &[u8] {
&self.scale_bytes
}
pub fn to_value(&self) -> Result<DecodedValue, Error> {
DecodedValue::decode_with_metadata(
&mut &*self.scale_bytes,
self.type_id,
&self.metadata,
)
}
}