value: add as_slice and as_array helper conversion methods

This commit is contained in:
Matt Bilker
2018-10-29 15:17:27 +00:00
parent 1fe1dc7323
commit c24cbf895f
2 changed files with 15 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "kbinxml"
version = "0.13.2"
version = "0.13.3"
authors = ["Matt Bilker <me@mbilker.us>"]
[dependencies]

View File

@@ -478,6 +478,13 @@ macro_rules! construct_types {
}
}
pub fn as_slice(&self) -> Result<&[u8], KbinError> {
match self {
Value::Binary(ref data) => Ok(data),
value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::Binary, value.clone()).into()),
}
}
pub fn as_str(&self) -> Result<&str, KbinError> {
match self {
Value::String(ref s) => Ok(s),
@@ -498,6 +505,13 @@ macro_rules! construct_types {
value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::Attribute, value).into()),
}
}
pub fn as_array(&self) -> Result<&[Value], KbinError> {
match self {
Value::Array(_, ref values) => Ok(values),
value => Err(KbinErrorKind::ExpectedValueArray(value.clone()).into()),
}
}
}
#[cfg(feature = "serde")]