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 Into<String> for Value {
    fn into(self) -> String {
        self.get::<String>()
    }
}

impl Into<f64> for Value {
    fn into(self) -> f64 {
        self.get::<f64>()
    }
}

impl Into<f32> for Value {
    fn into(self) -> f32 {
        self.get::<f32>()
    }
}