Skip to main content

link_cli/
lino_link.rs

1//! LinoLink - A parsed LiNo link structure
2//!
3//! This module provides the LinoLink data structure that represents
4//! a parsed link from LiNo notation.
5
6/// LinoLink represents a parsed link from LiNo notation
7/// Corresponds to Link.Foundation.Links.Notation.Link<string> in C#
8#[derive(Debug, Clone, PartialEq, Eq, Default)]
9pub struct LinoLink {
10    /// The ID/name of this link (can be a number, variable, or name)
11    pub id: Option<String>,
12    /// Child values (for composite links)
13    pub values: Option<Vec<LinoLink>>,
14}
15
16impl LinoLink {
17    /// Creates a new LinoLink with just an ID
18    pub fn new(id: Option<String>) -> Self {
19        Self { id, values: None }
20    }
21
22    /// Creates a new LinoLink with an ID and values
23    pub fn with_values(id: Option<String>, values: Vec<LinoLink>) -> Self {
24        Self {
25            id,
26            values: Some(values),
27        }
28    }
29
30    /// Returns true if this link has no ID
31    pub fn is_empty(&self) -> bool {
32        self.id.is_none() && self.values.as_ref().is_none_or(|v| v.is_empty())
33    }
34
35    /// Returns true if this link has child values
36    pub fn has_values(&self) -> bool {
37        self.values.as_ref().is_some_and(|v| !v.is_empty())
38    }
39
40    /// Gets the number of child values
41    pub fn values_count(&self) -> usize {
42        self.values.as_ref().map_or(0, |v| v.len())
43    }
44
45    /// Returns true if the ID is a variable (starts with $)
46    pub fn is_variable(&self) -> bool {
47        self.id.as_ref().is_some_and(|id| id.starts_with('$'))
48    }
49
50    /// Returns true if the ID is a wildcard (*)
51    pub fn is_wildcard(&self) -> bool {
52        self.id.as_ref().is_some_and(|id| id == "*")
53    }
54
55    /// Returns true if the ID is numeric
56    pub fn is_numeric(&self) -> bool {
57        self.id.as_ref().is_some_and(|id| id.parse::<u32>().is_ok())
58    }
59}