zrx_id/id/specificity/segment.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//! Segment.
27
28use std::fmt::{self, Display};
29use std::slice::Iter;
30
31pub mod atom;
32mod convert;
33mod set;
34
35pub use atom::Atom;
36pub use convert::ToSegments;
37pub use set::Segments;
38
39// ----------------------------------------------------------------------------
40// Structs
41// ----------------------------------------------------------------------------
42
43/// Segment.
44#[derive(Clone, Debug, PartialEq, Eq)]
45pub struct Segment<'a> {
46 /// Atoms.
47 atoms: Vec<Atom<'a>>,
48}
49
50// ----------------------------------------------------------------------------
51// Implementations
52// ----------------------------------------------------------------------------
53
54impl Segment<'_> {
55 /// Creates an iterator over the segment.
56 #[inline]
57 pub fn iter(&self) -> Iter<'_, Atom<'_>> {
58 self.atoms.iter()
59 }
60}
61
62// ----------------------------------------------------------------------------
63// Trait implementations
64// ----------------------------------------------------------------------------
65
66impl<'a> FromIterator<Atom<'a>> for Segment<'a> {
67 /// Creates a segment from an iterator.
68 #[inline]
69 fn from_iter<T>(iter: T) -> Self
70 where
71 T: IntoIterator<Item = Atom<'a>>,
72 {
73 Self {
74 atoms: iter.into_iter().collect(),
75 }
76 }
77}
78
79impl<'a> IntoIterator for &'a Segment<'a> {
80 type Item = &'a Atom<'a>;
81 type IntoIter = Iter<'a, Atom<'a>>;
82
83 /// Creates an iterator over the segment.
84 #[inline]
85 fn into_iter(self) -> Self::IntoIter {
86 self.iter()
87 }
88}
89
90// ----------------------------------------------------------------------------
91
92impl Display for Segment<'_> {
93 /// Formats the segment for display.
94 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95 for atom in &self.atoms {
96 Display::fmt(atom, f)?;
97 }
98
99 // No errors occurred
100 Ok(())
101 }
102}