tmx/
property.rs

1use std::path::PathBuf;
2
3/// A custom property
4#[derive(Debug, Clone)]
5#[allow(missing_docs)]
6pub enum Property {
7    String(String),
8    Int(i32),
9    Float(f64),
10    Bool(bool),
11    /// A color in the format `[a, r, g, b]`
12    Color([u8; 4]),
13    File(String),
14}
15
16impl Property {
17    /// Return &str value if this property is a string, `None` otherwise.
18    pub fn as_str(&self) -> Option<&str> {
19        match self {
20            Property::String(x) => Some(x.as_str()),
21            _ => None,
22        }
23    }
24
25    /// Return i32 value if this property is an int or float, `None` otherwise.
26    pub fn as_int(&self) -> Option<i32> {
27        match *self {
28            Property::Int(x) => Some(x),
29            Property::Float(x) => Some(x as i32),
30            _ => None,
31        }
32    }
33
34    /// Return f64 value if this property is a float or int, `None` otherwise.
35    pub fn as_float(&self) -> Option<f64> {
36        match *self {
37            Property::Float(x) => Some(x),
38            Property::Int(x) => Some(x as f64),
39            _ => None,
40        }
41    }
42
43    /// Return bool value if this property is a bool, `None` otherwise.
44    pub fn as_bool(&self) -> Option<bool> {
45        match *self {
46            Property::Bool(x) => Some(x),
47            _ => None,
48        }
49    }
50
51    /// Return `[u8; 4]` value if this property is a color, `None` otherwise.
52    pub fn as_color(&self) -> Option<[u8; 4]> {
53        match *self {
54            Property::Color(x) => Some(x),
55            _ => None,
56        }
57    }
58
59    /// Return PathBuf value if this property is a file, `None` otherwise.
60    pub fn as_file(&self) -> Option<PathBuf> {
61        match self {
62            Property::File(x) => Some(PathBuf::from(x)),
63            _ => None,
64        }
65    }
66}