zrx_id/id/specificity/segment/atom.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the `Software`), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED `AS IS`, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Atom.
27
28use std::fmt::{self, Display, Write};
29
30use super::set::Segments;
31
32mod character;
33mod wildcard;
34
35pub use character::Character;
36pub use wildcard::Wildcard;
37
38// ----------------------------------------------------------------------------
39// Enums
40// ----------------------------------------------------------------------------
41
42/// Atom.
43///
44/// Atoms are the basic building blocks of [`Segments`], representing literals,
45/// wildcards, character classes and groups of alternatives. Each [`Segment`][]
46/// contains a set of atoms that define which [`Specificity`][] the segment has,
47/// where specificity is determined by the least specific atom in the segment.
48///
49/// [`Segment`]: crate::id::specificity::segment::Segment
50/// [`Specificity`]: crate::id::specificity::Specificity
51#[derive(Clone, Debug, PartialEq, Eq)]
52pub enum Atom<'a> {
53 /// Literal, e.g., `main.rs`
54 Literal(&'a str),
55 /// Wildcard, i.e., `?`, `*`, or `**`.
56 Wildcard(Wildcard),
57 /// Character class, e.g., `[xyz]`.
58 Character(Character<'a>),
59 /// Alternate group, e.g., `{*.rs,*.md}`.
60 Group(Vec<Segments<'a>>),
61}
62
63// ----------------------------------------------------------------------------
64// Trait implementations
65// ----------------------------------------------------------------------------
66
67impl Display for Atom<'_> {
68 /// Formats the atom for display.
69 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70 match self {
71 Atom::Literal(literal) => Display::fmt(literal, f),
72 Atom::Wildcard(wildcard) => Display::fmt(wildcard, f),
73 Atom::Character(character) => Display::fmt(character, f),
74 Atom::Group(group) => {
75 f.write_char('{')?;
76 for (i, segments) in group.iter().enumerate() {
77 Display::fmt(&segments, f)?;
78
79 // Write comma if not last
80 if i < group.len() - 1 {
81 f.write_char(',')?;
82 }
83 }
84 f.write_char('}')
85 }
86 }
87 }
88}