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(super) mod serialization {
41    use alloc::{sync::Arc, vec::Vec};
42
43    use miden_assembly_syntax::debuginfo::Span;
44    use serde::de::{MapAccess, Visitor};
45
46    use super::Profile;
47
48    struct ProfileMapVisitor;
49
50    impl<'de> Visitor<'de> for ProfileMapVisitor {
51        type Value = Vec<Profile>;
52
53        fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
54            formatter.write_str("a profile map")
55        }
56
57        fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
58        where
59            M: MapAccess<'de>,
60        {
61            let mut profiles = Self::Value::default();
62
63            while let Some((key, mut value)) = access.next_entry::<Span<Arc<str>>, Profile>()? {
64                value.name = key;
65
66                if let Some(prev) =
67                    profiles.iter_mut().find(|p| p.name.inner() == value.name.inner())
68                {
69                    *prev = value;
70                } else {
71                    profiles.push(value);
72                }
73            }
74
75            Ok(profiles)
76        }
77    }
78
79    pub fn serialize<S>(profiles: &[Profile], serializer: S) -> Result<S::Ok, S::Error>
80    where
81        S: serde::Serializer,
82    {
83        serializer
84            .collect_map(profiles.iter().map(|profile| (profile.name.inner().clone(), profile)))
85    }
86
87    pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<Profile>, D::Error>
88    where
89        D: serde::Deserializer<'de>,
90    {
91        deserializer.deserialize_map(ProfileMapVisitor)
92    }
93}