miden_assembly_syntax/ast/attribute/meta/
list.rs

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