df_ls_core/
allow_empty.rs

1use serde::{Deserialize, Serialize};
2
3/// Work same way as `Option<T>` except for `TokenDeserialize` trait
4#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash)]
5pub enum AllowEmpty<T> {
6    None,
7    Some(T),
8}
9
10impl<T> Default for AllowEmpty<T> {
11    fn default() -> Self {
12        Self::None
13    }
14}
15
16/// Work same way as `Option<T>`
17impl<T> AllowEmpty<T> {
18    pub const fn is_some(&self) -> bool {
19        matches!(*self, AllowEmpty::Some(_))
20    }
21    pub const fn is_none(&self) -> bool {
22        !self.is_some()
23    }
24    pub fn expect(self, msg: &str) -> T {
25        match self {
26            AllowEmpty::Some(val) => val,
27            AllowEmpty::None => panic!("{}", msg),
28        }
29    }
30    pub fn unwrap(self) -> T {
31        match self {
32            AllowEmpty::Some(val) => val,
33            AllowEmpty::None => panic!("called `AllowEmpty::unwrap()` on a `None` value"),
34        }
35    }
36}
37
38impl<T: Copy> AllowEmpty<&T> {
39    pub fn copied(self) -> AllowEmpty<T> {
40        match self {
41            AllowEmpty::Some(&t) => AllowEmpty::Some(t),
42            AllowEmpty::None => AllowEmpty::None,
43        }
44    }
45}
46
47impl<T: Clone> AllowEmpty<&T> {
48    pub fn cloned(self) -> AllowEmpty<T> {
49        match self {
50            AllowEmpty::Some(t) => AllowEmpty::Some(t.clone()),
51            AllowEmpty::None => AllowEmpty::None,
52        }
53    }
54}