fix(solc): use correct empty output selection (#1185)

This commit is contained in:
Matthias Seitz 2022-04-27 20:27:54 +02:00 committed by GitHub
parent 82d5741bcc
commit 5de7086ba7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 2 deletions

View File

@ -1,6 +1,6 @@
//! bindings for standard json output selection
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
use std::{collections::BTreeMap, fmt, str::FromStr};
/// Represents the desired outputs based on a File `(file -> (contract -> [outputs]))`
@ -68,7 +68,7 @@ pub type FileOutputSelection = BTreeMap<String, Vec<String>>;
/// }
/// }
/// ```
#[derive(Debug, Clone, Eq, PartialEq, Default, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Default, Deserialize)]
#[serde(transparent)]
pub struct OutputSelection(pub BTreeMap<String, FileOutputSelection>);
@ -121,6 +121,39 @@ impl OutputSelection {
}
}
// this will make sure that if the `FileOutputSelection` for a certain file is empty will be
// serializes as `"*" : []` because
// > Contract level (needs the contract name or "*") <https://docs.soliditylang.org/en/v0.8.13/using-the-compiler.html>
impl Serialize for OutputSelection {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
struct EmptyFileOutput;
impl Serialize for EmptyFileOutput {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("*", &[] as &[String])?;
map.end()
}
}
let mut map = serializer.serialize_map(Some(self.0.len()))?;
for (file, selection) in self.0.iter() {
if selection.is_empty() {
map.serialize_entry(file, &EmptyFileOutput {})?;
} else {
map.serialize_entry(file, selection)?;
}
}
map.end()
}
}
impl AsRef<BTreeMap<String, FileOutputSelection>> for OutputSelection {
fn as_ref(&self) -> &BTreeMap<String, FileOutputSelection> {
&self.0
@ -532,4 +565,12 @@ mod tests {
assert_eq!(json, serde_json::to_string(&deserde_selection).unwrap());
}
#[test]
fn empty_outputselection_serde_works() {
let mut empty = OutputSelection::default();
empty.0.insert("contract.sol".to_string(), OutputSelection::empty_file_output_select());
let s = serde_json::to_string(&empty).unwrap();
assert_eq!(s, r#"{"contract.sol":{"*":[]}}"#);
}
}