revive_solc_json_interface/standard_json/input/settings/selection/
mod.rs

1//! The `solc --standard-json` output selection.
2
3pub mod file;
4
5use std::collections::BTreeMap;
6
7use serde::Deserialize;
8use serde::Serialize;
9
10use self::file::File as FileSelection;
11
12/// The `solc --standard-json` output selection.
13#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
14pub struct Selection {
15    /// Only the 'all' wildcard is available for robustness reasons.
16    #[serde(rename = "*", skip_serializing_if = "Option::is_none")]
17    all: Option<FileSelection>,
18
19    #[serde(skip_serializing_if = "BTreeMap::is_empty", flatten)]
20    pub files: BTreeMap<String, FileSelection>,
21}
22
23impl Selection {
24    /// Creates the selection required by our compilation process.
25    pub fn new_required() -> Self {
26        Self {
27            all: Some(FileSelection::new_required()),
28            files: BTreeMap::new(),
29        }
30    }
31
32    /// Extends the user's output selection with flag required by our compilation process.
33    pub fn extend_with_required(&mut self) -> &mut Self {
34        self.all
35            .get_or_insert_with(FileSelection::new_required)
36            .extend_with_required();
37        for (_, v) in self.files.iter_mut() {
38            v.extend_with_required();
39        }
40        self
41    }
42}
43
44#[cfg(test)]
45mod test {
46    use std::collections::BTreeMap;
47
48    use crate::SolcStandardJsonInputSettingsSelectionFile;
49
50    use super::Selection;
51
52    #[test]
53    fn per_file() {
54        let init = Selection {
55            all: None,
56            files: BTreeMap::from([(
57                "Test".to_owned(),
58                SolcStandardJsonInputSettingsSelectionFile::new_required(),
59            )]),
60        };
61
62        let deser = serde_json::to_string(&init)
63            .and_then(|string| serde_json::from_str(&string))
64            .unwrap();
65
66        assert_eq!(init, deser)
67    }
68
69    #[test]
70    fn all() {
71        let init = Selection {
72            all: Some(SolcStandardJsonInputSettingsSelectionFile::new_required()),
73            files: BTreeMap::new(),
74        };
75
76        let deser = serde_json::to_string(&init)
77            .and_then(|string| serde_json::from_str(&string))
78            .unwrap();
79
80        assert_eq!(init, deser)
81    }
82
83    #[test]
84    fn all_and_override() {
85        let init = Selection {
86            all: Some(SolcStandardJsonInputSettingsSelectionFile::new_required()),
87            files: BTreeMap::from([(
88                "Test".to_owned(),
89                SolcStandardJsonInputSettingsSelectionFile::new_required(),
90            )]),
91        };
92
93        let deser = serde_json::to_string(&init)
94            .and_then(|string| serde_json::from_str(&string))
95            .unwrap();
96
97        assert_eq!(init, deser)
98    }
99}