ofdb_entities/
category.rs

1use crate::id::Id;
2
3#[derive(Debug, Clone, Eq, PartialEq)]
4pub struct Category {
5    pub id: Id,
6    pub tag: String,
7}
8
9impl Category {
10    pub fn name(&self) -> String {
11        format!("#{}", self.tag)
12    }
13}
14
15impl Category {
16    pub const ID_NON_PROFIT: &'static str = "2cd00bebec0c48ba9db761da48678134";
17    pub const ID_COMMERCIAL: &'static str = "77b3c33a92554bcf8e8c2c86cedd6f6f";
18    pub const ID_EVENT: &'static str = "c2dc278a2d6a4b9b8a50cb606fc017ed";
19
20    pub const TAG_NON_PROFIT: &'static str = "non-profit";
21    pub const TAG_COMMERCIAL: &'static str = "commercial";
22    pub const TAG_EVENT: &'static str = "event";
23
24    pub fn new_non_profit() -> Self {
25        Self {
26            id: Self::ID_NON_PROFIT.into(),
27            tag: Self::TAG_NON_PROFIT.into(),
28        }
29    }
30
31    pub fn new_commercial() -> Self {
32        Self {
33            id: Self::ID_COMMERCIAL.into(),
34            tag: Self::TAG_COMMERCIAL.into(),
35        }
36    }
37
38    pub fn new_event() -> Self {
39        Self {
40            id: Self::ID_EVENT.into(),
41            tag: Self::TAG_EVENT.into(),
42        }
43    }
44
45    pub fn split_from_tags(tags: Vec<String>) -> (Vec<String>, Vec<Category>) {
46        let mut categories = Vec::with_capacity(3);
47        let tags = tags
48            .into_iter()
49            .filter(|t| match t.as_str() {
50                Self::TAG_NON_PROFIT => {
51                    categories.push(Self::new_non_profit());
52                    false
53                }
54                Self::TAG_COMMERCIAL => {
55                    categories.push(Self::new_commercial());
56                    false
57                }
58                Self::TAG_EVENT => {
59                    categories.push(Self::new_event());
60                    false
61                }
62                _ => true,
63            })
64            .collect();
65        (tags, categories)
66    }
67
68    pub fn merge_ids_into_tags(ids: &[Id], mut tags: Vec<String>) -> Vec<String> {
69        tags.reserve(ids.len());
70        tags = ids.iter().fold(tags, |mut tags, id| {
71            match id.as_ref() {
72                Self::ID_NON_PROFIT => tags.push(Self::TAG_NON_PROFIT.into()),
73                Self::ID_COMMERCIAL => tags.push(Self::TAG_COMMERCIAL.into()),
74                Self::ID_EVENT => tags.push(Self::TAG_EVENT.into()),
75                _ => (),
76            }
77            tags
78        });
79        tags.sort_unstable();
80        tags.dedup();
81        tags
82    }
83}