hydrus_api/wrapper/
or_chain.rs

1use crate::utils::tag_list_to_string_list;
2use crate::wrapper::tag::Tag;
3use lazy_static::lazy_static;
4use regex::Regex;
5
6#[derive(Clone, Debug, PartialOrd, PartialEq)]
7pub struct OrChain {
8    tags: Vec<Tag>,
9}
10
11impl Eq for OrChain {}
12
13impl OrChain {
14    /// Creates a new or chain directly from a list of tags
15    pub fn new(tags: Vec<Tag>) -> Self {
16        Self { tags }
17    }
18
19    /// Returns the tags of this or chain
20    pub fn tags(&self) -> &Vec<Tag> {
21        &self.tags
22    }
23
24    pub(crate) fn into_string_list(self) -> Vec<String> {
25        tag_list_to_string_list(self.tags)
26    }
27}
28
29impl<S> From<S> for OrChain
30where
31    S: AsRef<str>,
32{
33    fn from(s: S) -> Self {
34        lazy_static! {
35            static ref CHAIN_REGEX: Regex = Regex::new(r#"(\s|'|")or(\s|'|")"#).unwrap();
36        }
37        let s = s.as_ref().to_ascii_lowercase();
38        let tags = CHAIN_REGEX
39            .split(&s)
40            .map(|mut t| {
41                t = t
42                    .trim_start()
43                    .trim_start_matches("'")
44                    .trim_start_matches("\"");
45                t = t.trim_end().trim_end_matches("'").trim_end_matches("\"");
46                t
47            })
48            .map(Tag::from)
49            .collect();
50        tracing::debug!("String parsed to or-chain {:?}", tags);
51
52        Self { tags }
53    }
54}