Skip to main content

link_cli/
parser.rs

1//! LiNo Parser - Parses LiNo notation into LinoLink structures.
2//!
3//! This module adapts the upstream `links-notation` parser into the local
4//! `LinoLink` representation used by the query processor.
5
6use crate::error::LinkError;
7use crate::lino_link::LinoLink;
8use links_notation::{parse_lino_to_links, LiNo};
9
10/// Parser for LiNo notation
11/// Corresponds to Link.Foundation.Links.Notation.Parser in C#
12pub struct Parser;
13
14impl Parser {
15    /// Creates a new Parser instance
16    pub fn new() -> Self {
17        Self
18    }
19
20    /// Parses a LiNo query string into a list of LinoLinks
21    pub fn parse(&self, query: &str) -> Result<Vec<LinoLink>, LinkError> {
22        parse_lino_to_links(query)
23            .map(|links| links.into_iter().map(Self::convert_link).collect())
24            .map_err(|error| LinkError::ParseError(error.to_string()))
25    }
26
27    fn convert_link(link: LiNo<String>) -> LinoLink {
28        match link {
29            LiNo::Ref(id) => LinoLink::new(Some(id)),
30            LiNo::Link { id, values } if values.is_empty() => {
31                id.map(|id| LinoLink::new(Some(id))).unwrap_or_default()
32            }
33            LiNo::Link { id, values } => {
34                LinoLink::with_values(id, values.into_iter().map(Self::convert_link).collect())
35            }
36        }
37    }
38}
39
40impl Default for Parser {
41    fn default() -> Self {
42        Self::new()
43    }
44}