commit
4f72875787
@ -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,54 @@
|
||||
use crate::utils::tag_list_to_string_list;
|
||||
use crate::wrapper::tag::Tag;
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Clone, Debug, PartialOrd, PartialEq)]
|
||||
pub struct OrChain {
|
||||
tags: Vec<Tag>,
|
||||
}
|
||||
|
||||
impl Eq for OrChain {}
|
||||
|
||||
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 {
|
||||
lazy_static! {
|
||||
static ref CHAIN_REGEX: Regex = Regex::new(r#"(\s|'|")or(\s|'|")"#).unwrap();
|
||||
}
|
||||
let s = s.as_ref().to_ascii_lowercase();
|
||||
let tags = CHAIN_REGEX
|
||||
.split(&s)
|
||||
.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();
|
||||
log::debug!("String parsed to or-chain {:?}", tags);
|
||||
|
||||
Self { tags }
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
use super::super::common;
|
||||
use hydrus_api::wrapper::builders::or_chain_builder::OrChainBuilder;
|
||||
use hydrus_api::wrapper::builders::tag_builder::TagBuilder;
|
||||
use hydrus_api::wrapper::or_chain::OrChain;
|
||||
|
||||
#[test]
|
||||
fn it_parses_from_string() {
|
||||
common::setup();
|
||||
let chain_string =
|
||||
"'character:megumin' or 'character:aqua' OR '-character:hatsune miku'or 'terminator'";
|
||||
let chain = OrChain::from(chain_string);
|
||||
assert_eq!(
|
||||
chain,
|
||||
OrChainBuilder::new()
|
||||
.add_tag("character:megumin".into())
|
||||
.add_tag("character:aqua".into())
|
||||
.add_tag(
|
||||
TagBuilder::new("hatsune miku")
|
||||
.namespace("character")
|
||||
.negate()
|
||||
.build()
|
||||
)
|
||||
.add_tag("terminator".into())
|
||||
.build()
|
||||
);
|
||||
}
|
Loading…
Reference in New Issue