mirror of
https://github.com/mbilker/kbinxml-rs.git
synced 2026-09-11 03:55:13 -05:00
value: add TryFrom implementations to get inner type from value
This commit is contained in:
@@ -23,3 +23,4 @@ serde_derive = { version = "1.0.79", optional = true }
|
||||
|
||||
[features]
|
||||
serde-1 = ["serde", "serde_bytes", "serde_derive", "indexmap/serde-1"]
|
||||
try_from = []
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#![cfg_attr(test, feature(test))]
|
||||
#![cfg_attr(feature = "try_from", feature(try_from))]
|
||||
|
||||
extern crate byteorder;
|
||||
extern crate bytes;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#[cfg(feature = "try_from")]
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::net::Ipv4Addr;
|
||||
use std::str::FromStr;
|
||||
@@ -412,6 +414,18 @@ macro_rules! construct_types {
|
||||
Value::$konst(value)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "try_from")]
|
||||
impl TryFrom<Value> for $($value_type)* {
|
||||
type Error = KbinError;
|
||||
|
||||
fn try_from(value: Value) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
Value::$konst(v) => Ok(v),
|
||||
value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::$konst, value).into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
)+
|
||||
|
||||
impl Value {
|
||||
|
||||
Reference in New Issue
Block a user