Skip to main content

aam_core/
aam_value.rs

1use std::collections::HashMap;
2use std::fmt::Display;
3use std::ops::Deref;
4
5/// Represents a value in the AAML map, which can be a string, a list of values, or an inline object.
6#[derive(Debug, Clone, PartialEq)]
7pub enum AamValue {
8    String(String),
9    List(Vec<Self>),
10    Object(HashMap<String, Self>),
11}
12
13impl AamValue {
14    /// Returns the inner string if this is a `String` variant, otherwise returns `None`.
15    #[must_use]
16    pub fn as_str(&self) -> Option<&str> {
17        if let Self::String(s) = self {
18            Some(s)
19        } else {
20            None
21        }
22    }
23
24    /// Returns link for the list
25    #[must_use]
26    pub const fn as_list(&self) -> Option<&Vec<Self>> {
27        if let Self::List(l) = self {
28            Some(l)
29        } else {
30            None
31        }
32    }
33
34    /// Returns links to the fields of an inline object.
35    #[must_use]
36    pub const fn as_object(&self) -> Option<&HashMap<String, Self>> {
37        if let Self::Object(o) = self {
38            Some(o)
39        } else {
40            None
41        }
42    }
43
44    #[must_use]
45    pub const fn is_list(&self) -> bool {
46        matches!(self, Self::List(_))
47    }
48
49    #[must_use]
50    pub const fn is_object(&self) -> bool {
51        matches!(self, Self::Object(_))
52    }
53}
54
55impl PartialEq<&str> for AamValue {
56    fn eq(&self, other: &&str) -> bool {
57        self.as_str().expect("Here must be AamlError") == *other
58    }
59}
60
61impl Display for AamValue {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        f.write_str(self.as_str().expect("Here must be AamlError")) // Will be... Maybe...
64    }
65}
66
67impl Deref for AamValue {
68    type Target = str;
69
70    fn deref(&self) -> &Self::Target {
71        self.as_str().expect("Here must be AamlError")
72    }
73}