Skip to content

Commit

Permalink
fix(solc): use correct empty output selection (gakonst#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Apr 27, 2022
1 parent 82d5741 commit 5de7086
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions ethers-solc/src/artifacts/output_selection.rs
Original file line number Diff line number Diff line change
@@ -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]))`
Expand Down Expand Up @@ -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>);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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":{"*":[]}}"#);
}
}

0 comments on commit 5de7086

Please sign in to comment.