miden_assembly/ast/attribute/meta/
expr.rs

1use alloc::{string::String, sync::Arc};
2
3use crate::{Felt, SourceSpan, Span, Spanned, ast::Ident, parser::HexEncodedValue, prettier};
4
5/// Represents a metadata expression of an [crate::ast::Attribute]
6#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum MetaExpr {
8    /// An identifier/keyword, e.g. `inline`
9    Ident(Ident),
10    /// A decimal or hexadecimal integer value
11    Int(Span<HexEncodedValue>),
12    /// A quoted string or identifier
13    String(Ident),
14}
15
16impl prettier::PrettyPrint for MetaExpr {
17    fn render(&self) -> prettier::Document {
18        use prettier::*;
19
20        match self {
21            Self::Ident(id) => text(id),
22            Self::Int(value) => text(value),
23            Self::String(id) => text(format!("\"{}\"", id.as_str().escape_default())),
24        }
25    }
26}
27
28impl From<Ident> for MetaExpr {
29    fn from(value: Ident) -> Self {
30        Self::Ident(value)
31    }
32}
33
34impl From<&str> for MetaExpr {
35    fn from(value: &str) -> Self {
36        Self::String(Ident::from_raw_parts(Span::new(SourceSpan::UNKNOWN, Arc::from(value))))
37    }
38}
39
40impl From<String> for MetaExpr {
41    fn from(value: String) -> Self {
42        Self::String(Ident::from_raw_parts(Span::new(
43            SourceSpan::UNKNOWN,
44            Arc::from(value.into_boxed_str()),
45        )))
46    }
47}
48
49impl From<u8> for MetaExpr {
50    fn from(value: u8) -> Self {
51        Self::Int(Span::new(SourceSpan::UNKNOWN, HexEncodedValue::U8(value)))
52    }
53}
54
55impl From<u16> for MetaExpr {
56    fn from(value: u16) -> Self {
57        Self::Int(Span::new(SourceSpan::UNKNOWN, HexEncodedValue::U16(value)))
58    }
59}
60
61impl From<u32> for MetaExpr {
62    fn from(value: u32) -> Self {
63        Self::Int(Span::new(SourceSpan::UNKNOWN, HexEncodedValue::U32(value)))
64    }
65}
66
67impl From<Felt> for MetaExpr {
68    fn from(value: Felt) -> Self {
69        Self::Int(Span::new(SourceSpan::UNKNOWN, HexEncodedValue::Felt(value)))
70    }
71}
72
73impl From<[Felt; 4]> for MetaExpr {
74    fn from(value: [Felt; 4]) -> Self {
75        Self::Int(Span::new(SourceSpan::UNKNOWN, HexEncodedValue::Word(value)))
76    }
77}
78
79impl Spanned for MetaExpr {
80    fn span(&self) -> SourceSpan {
81        match self {
82            Self::Ident(spanned) | Self::String(spanned) => spanned.span(),
83            Self::Int(spanned) => spanned.span(),
84        }
85    }
86}