mirror of
https://github.com/mbilker/kbinxml-rs.git
synced 2026-09-09 19:15:27 -05:00
value: add TryFrom impl for borrowed Binary values
This commit is contained in:
@@ -599,6 +599,13 @@ impl Value {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_binary(&self) -> Result<&[u8], KbinError> {
|
||||
match self {
|
||||
Value::Binary(ref data) => Ok(data),
|
||||
value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::Binary, value.clone()).into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_array(&self) -> Result<&[Value], KbinError> {
|
||||
match self {
|
||||
Value::Array(_, ref values) => Ok(values),
|
||||
@@ -623,6 +630,15 @@ impl TryFrom<Value> for Vec<u8> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "try_from")]
|
||||
impl TryFrom<&Value> for Vec<u8> {
|
||||
type Error = KbinError;
|
||||
|
||||
fn try_from(value: &Value) -> Result<Self, Self::Error> {
|
||||
value.as_binary().map(Vec::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<u8>> for Value {
|
||||
fn from(value: Vec<u8>) -> Value {
|
||||
Value::Binary(value)
|
||||
|
||||
Reference in New Issue
Block a user