miden_assembly/ast/attribute/meta/
list.rs

1use alloc::vec::Vec;
2
3use super::MetaExpr;
4use crate::{SourceSpan, Spanned, ast::Ident};
5
6/// Represents the metadata of a named list [crate::ast::Attribute], i.e. `@name(item0, .., itemN)`
7#[derive(Clone)]
8pub struct MetaList {
9    pub span: SourceSpan,
10    /// The identifier used as the name of this attribute
11    pub name: Ident,
12    /// The list of items representing the value of this attribute - will always contain at least
13    /// one element when parsed.
14    pub items: Vec<MetaExpr>,
15}
16
17impl Spanned for MetaList {
18    #[inline(always)]
19    fn span(&self) -> SourceSpan {
20        self.span
21    }
22}
23
24impl MetaList {
25    pub fn new<I>(name: Ident, items: I) -> Self
26    where
27        I: IntoIterator<Item = MetaExpr>,
28    {
29        Self {
30            span: SourceSpan::default(),
31            name,
32            items: items.into_iter().collect(),
33        }
34    }
35
36    pub fn with_span(mut self, span: SourceSpan) -> Self {
37        self.span = span;
38        self
39    }
40
41    /// Get the name of this attribute as a string
42    pub fn name(&self) -> &str {
43        self.name.as_str()
44    }
45
46    /// Get the name of this attribute as an [Ident]
47    pub fn id(&self) -> Ident {
48        self.name.clone()
49    }
50
51    /// Returns true if the metadata list is empty
52    #[inline]
53    pub fn is_empty(&self) -> bool {
54        self.items.is_empty()
55    }
56
57    /// Returns the number of items in the metadata list
58    #[inline]
59    pub fn len(&self) -> usize {
60        self.items.len()
61    }
62
63    /// Get the metadata list as a slice
64    #[inline]
65    pub fn as_slice(&self) -> &[MetaExpr] {
66        self.items.as_slice()
67    }
68
69    /// Get the metadata list as a mutable slice
70    #[inline]
71    pub fn as_mut_slice(&mut self) -> &mut [MetaExpr] {
72        self.items.as_mut_slice()
73    }
74}
75
76impl Eq for MetaList {}
77
78impl PartialEq for MetaList {
79    fn eq(&self, other: &Self) -> bool {
80        self.name == other.name && self.items == other.items
81    }
82}
83
84impl PartialOrd for MetaList {
85    #[inline]
86    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
87        Some(self.cmp(other))
88    }
89}
90
91impl Ord for MetaList {
92    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
93        self.name.cmp(&other.name).then_with(|| self.items.cmp(&other.items))
94    }
95}
96
97impl core::hash::Hash for MetaList {
98    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
99        self.name.hash(state);
100        self.items.hash(state);
101    }
102}