Add support for or-chains
Signed-off-by: trivernis <trivernis@protonmail.com>pull/8/head
parent
daffe24734
commit
f9e06a9af2
@ -1,4 +1,5 @@
|
||||
pub mod import_builder;
|
||||
pub mod tagging_builder;
|
||||
pub mod tag_builder;
|
||||
pub mod or_chain_builder;
|
||||
pub mod search_builder;
|
||||
pub mod tag_builder;
|
||||
pub mod tagging_builder;
|
||||
|
@ -0,0 +1,30 @@
|
||||
use crate::wrapper::or_chain::OrChain;
|
||||
use crate::wrapper::tag::Tag;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct OrChainBuilder {
|
||||
tags: Vec<Tag>,
|
||||
}
|
||||
|
||||
impl OrChainBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self { tags: Vec::new() }
|
||||
}
|
||||
|
||||
/// Adds a tag to the or expression
|
||||
pub fn add_tag(mut self, tag: Tag) -> Self {
|
||||
self.tags.push(tag);
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds multiple tags to the or expression
|
||||
pub fn add_tags(mut self, mut tags: Vec<Tag>) -> Self {
|
||||
self.tags.append(&mut tags);
|
||||
self
|
||||
}
|
||||
|
||||
/// Builds the or chain
|
||||
pub fn build(self) -> OrChain {
|
||||
OrChain::new(self.tags)
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
use crate::utils::tag_list_to_string_list;
|
||||
use crate::wrapper::tag::Tag;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct OrChain {
|
||||
tags: Vec<Tag>,
|
||||
}
|
||||
|
||||
impl OrChain {
|
||||
/// Creates a new or chain directly from a list of tags
|
||||
pub fn new(tags: Vec<Tag>) -> Self {
|
||||
Self { tags }
|
||||
}
|
||||
|
||||
/// Returns the tags of this or chain
|
||||
pub fn tags(&self) -> &Vec<Tag> {
|
||||
&self.tags
|
||||
}
|
||||
|
||||
pub(crate) fn into_string_list(self) -> Vec<String> {
|
||||
tag_list_to_string_list(self.tags)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> From<S> for OrChain
|
||||
where
|
||||
S: AsRef<str>,
|
||||
{
|
||||
fn from(s: S) -> Self {
|
||||
let s = s.as_ref().to_ascii_lowercase();
|
||||
let tags = s
|
||||
.split("or")
|
||||
.map(|mut t| {
|
||||
t = t
|
||||
.trim_start()
|
||||
.trim_start_matches("'")
|
||||
.trim_start_matches("\"");
|
||||
t = t.trim_end().trim_end_matches("'").trim_end_matches("\"");
|
||||
t
|
||||
})
|
||||
.map(Tag::from)
|
||||
.collect();
|
||||
|
||||
Self { tags }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue