Skip to main content

meta_language/
query.rs

1use crate::link_network::{Link, LinkType};
2
3/// Structural query over links.
4#[derive(Clone, Debug, Default, PartialEq, Eq)]
5pub struct LinkQuery {
6    link_type: Option<LinkType>,
7    term: Option<String>,
8    language: Option<String>,
9    named: Option<bool>,
10}
11
12impl LinkQuery {
13    /// Creates an empty query that matches every link.
14    #[must_use]
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    /// Restricts matches to a link type.
20    #[must_use]
21    pub const fn with_link_type(mut self, link_type: LinkType) -> Self {
22        self.link_type = Some(link_type);
23        self
24    }
25
26    /// Restricts matches to a term.
27    #[must_use]
28    pub fn with_term(mut self, term: impl Into<String>) -> Self {
29        self.term = Some(term.into());
30        self
31    }
32
33    /// Restricts matches to a language label.
34    #[must_use]
35    pub fn with_language(mut self, language: impl Into<String>) -> Self {
36        self.language = Some(language.into());
37        self
38    }
39
40    /// Restricts matches by the named flag.
41    #[must_use]
42    pub const fn with_named(mut self, named: bool) -> Self {
43        self.named = Some(named);
44        self
45    }
46
47    pub(crate) fn matches(&self, link: &Link) -> bool {
48        let metadata = link.metadata();
49        self.link_type
50            .map_or(true, |link_type| metadata.link_type() == Some(link_type))
51            && self
52                .term
53                .as_deref()
54                .map_or(true, |term| metadata.term() == Some(term))
55            && self
56                .language
57                .as_deref()
58                .map_or(true, |language| metadata.language() == Some(language))
59            && self
60                .named
61                .map_or(true, |named| metadata.is_named() == named)
62    }
63}