use std::hash::Hash;
use rustolio_utils::bytes::encoding::{decode_from_bytes, encode_to_bytes};
use rustolio_utils::bytes::Bytes;
use rustolio_utils::crypto::signature::PublicKey;
use rustolio_utils::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Decode, Encode)]
pub struct Value {
signer: Option<PublicKey>,
encoded: Bytes,
}
impl Value {
pub fn from_value(t: &impl Encode) -> rustolio_utils::Result<Self> {
Ok(Value {
signer: None,
encoded: encode_to_bytes(t).context("Failed to encode value")?,
})
}
pub fn signed_from_value(t: &impl Encode, signer: PublicKey) -> rustolio_utils::Result<Self> {
Ok(Value {
signer: Some(signer),
encoded: encode_to_bytes(t).context("Failed to encode value")?,
})
}
pub fn into_value<T: Decode>(self) -> rustolio_utils::Result<T> {
decode_from_bytes(self.encoded).context("Failed to decode value")
}
pub fn signer(&self) -> Option<PublicKey> {
self.signer
}
}