orbtk_utils/
value.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use serde::de::DeserializeOwned;
/// Wraps a ron value and is used to support conversion to different types.
pub struct Value(pub ron::Value);

impl Value {
    /// Converts the internal value to the given type.
    pub fn get<T>(self) -> T
    where
        T: Default + DeserializeOwned,
    {
        if let Ok(value) = self.0.into_rust::<T>() {
            return value;
        }

        T::default()
    }
}

impl From<ron::Value> for Value {
    fn from(v: ron::Value) -> Self {
        Value(v)
    }
}

impl From<Value> for String {
    fn from(v: Value) -> String {
        v.get::<String>()
    }
}

impl From<Value> for f64 {
    fn from(v: Value) -> f64 {
        v.get::<f64>()
    }
}

impl From<Value> for f32 {
    fn from(v: Value) -> f32 {
        v.get::<f32>()
    }
}