umya_spreadsheet/structs/
enum_value.rs

1use super::EnumTrait;
2use std::str::FromStr;
3
4#[derive(Clone, Default, Debug, Eq, Ord, PartialEq, PartialOrd)]
5pub struct EnumValue<T: EnumTrait + FromStr> {
6    value: Option<T>,
7    value_default: T,
8}
9
10impl<T: EnumTrait + FromStr> EnumValue<T> {
11    #[inline]
12    pub(crate) fn get_value(&self) -> &T {
13        match &self.value {
14            Some(v) => v,
15            None => &self.value_default,
16        }
17    }
18
19    #[inline]
20    pub(crate) fn get_value_string(&self) -> &str {
21        self.get_value().get_value_string()
22    }
23
24    #[inline]
25    pub(crate) fn set_value(&mut self, value: T) -> &mut EnumValue<T> {
26        self.value = Some(value);
27        self
28    }
29
30    #[inline]
31    pub(crate) fn set_value_string<S: Into<String>>(&mut self, value: S) -> &mut EnumValue<T> {
32        if let Ok(v) = T::from_str(value.into().as_str()) {
33            self.set_value(v);
34        }
35        self
36    }
37
38    #[inline]
39    pub(crate) fn has_value(&self) -> bool {
40        self.value.is_some()
41    }
42
43    #[inline]
44    pub(crate) fn get_hash_string(&self) -> &str {
45        if self.has_value() {
46            return self.get_value_string();
47        }
48        "empty!!"
49    }
50}