Skip to main content

miden_project/ast/
profile.rs

1use alloc::sync::Arc;
2
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6use super::parsing::SetSourceId;
7use crate::{Metadata, SourceId, Span};
8
9/// Represents configuration options for a specific build profile, e.g. `release`
10#[derive(Default, Debug, Clone)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub struct Profile {
13    /// The name of another profile that this profile inherits from
14    pub inherits: Option<Span<Arc<str>>>,
15    /// The name of this profile, e.g. `release`
16    #[cfg_attr(feature = "serde", serde(default, skip))]
17    pub name: Span<Arc<str>>,
18    /// Whether to emit debugging information for this profile
19    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
20    pub debug: Option<bool>,
21    #[cfg_attr(
22        feature = "serde",
23        serde(default, skip_serializing_if = "Option::is_none", rename = "trim-paths")
24    )]
25    pub trim_paths: Option<bool>,
26    #[cfg_attr(
27        feature = "serde",
28        serde(default, flatten, skip_serializing_if = "Metadata::is_empty")
29    )]
30    pub metadata: Metadata,
31}
32
33impl SetSourceId for Profile {
34    fn set_source_id(&mut self, source_id: SourceId) {
35        self.metadata.set_source_id(source_id);
36    }
37}
38
39#[cfg(feature = "serde")]
40pub use self::serialization::deserialize_profiles_table;
41
42#[cfg(feature = "serde")]
43mod serialization {
44    use alloc::{sync::Arc, vec::Vec};
45
46    use miden_assembly_syntax::debuginfo::Span;
47    use serde::de::{MapAccess, Visitor};
48
49    use super::Profile;
50
51    struct ProfileMapVisitor;
52
53    impl<'de> Visitor<'de> for ProfileMapVisitor {
54        type Value = Vec<Profile>;
55
56        fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
57            formatter.write_str("a profile map")
58        }
59
60        fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
61        where
62            M: MapAccess<'de>,
63        {
64            let mut profiles = Self::Value::default();
65
66            while let Some((key, mut value)) = access.next_entry::<Span<Arc<str>>, Profile>()? {
67                value.name = key;
68
69                if let Some(prev) =
70                    profiles.iter_mut().find(|p| p.name.inner() == value.name.inner())
71                {
72                    *prev = value;
73                } else {
74                    profiles.push(value);
75                }
76            }
77
78            Ok(profiles)
79        }
80    }
81
82    pub fn deserialize_profiles_table<'de, D>(deserializer: D) -> Result<Vec<Profile>, D::Error>
83    where
84        D: serde::Deserializer<'de>,
85    {
86        deserializer.deserialize_map(ProfileMapVisitor)
87    }
88}