Skip to content

Commit

Permalink
value from bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Aug 1, 2024
1 parent a39755d commit a8bc540
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions crates/libs/registry/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,23 @@ impl TryFrom<Value> for Vec<String> {
}
}
}

impl TryFrom<&[u8]> for Value {
type Error = Error;
fn try_from(from: &[u8]) -> Result<Self> {
Ok(Self {
data: Data::from_slice(from)?,
ty: Type::Bytes,
})
}
}

impl<const N: usize> TryFrom<[u8; N]> for Value {
type Error = Error;
fn try_from(from: [u8; N]) -> Result<Self> {
Ok(Self {
data: Data::from_slice(&from)?,
ty: Type::Bytes,
})
}
}
10 changes: 10 additions & 0 deletions crates/tests/registry/tests/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,15 @@ fn value() -> Result<()> {
assert_eq!(key.get_value("expand")?, value);
assert_eq!(key.get_string("expand")?, "expand");

key.set_value("bytes", &Value::try_from([1u8, 2u8, 3u8])?)?;
assert_eq!(key.get_type("bytes")?, Type::Bytes);
assert_eq!(key.get_value("bytes")?, Value::try_from([1, 2, 3])?);

let mut value = Value::try_from([1u8, 2u8, 3u8, 4u8].as_slice())?;
value.set_ty(Type::Other(1234));
key.set_value("slice", &value)?;
assert_eq!(key.get_type("slice")?, Type::Other(1234));
assert_eq!(key.get_value("slice")?, value);

Ok(())
}

0 comments on commit a8bc540

Please sign in to comment.