1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! Additional logging [CompilerInput] and [CompilerOutput]
//!
//! Useful for debugging purposes.
//! As solc compiler input and output can become quite large (in the tens of MB) we still want a way
//! to get this info when debugging an issue. Most convenient way to look at these object is as a
//! separate json file

use crate::{CompilerInput, CompilerOutput};
use semver::Version;
use std::{env, path::PathBuf, str::FromStr};

/// Debug Helper type that can be used to write the [crate::Solc] [CompilerInput] and
/// [CompilerOutput] to disk if configured.
///
/// # Example
///
/// If `ETHERS_SOLC_LOG=in=in.json,out=out.json` is then the reporter will be configured to write
/// the compiler input as pretty formatted json to `in.{solc version}.json` and the compiler output
/// to `out.{solc version}.json`
///
/// ```no_run
/// use ethers_solc::report::SolcCompilerIoReporter;
/// std::env::set_var("ETHERS_SOLC_LOG", "in=in.json,out=out.json");
/// let rep = SolcCompilerIoReporter::from_default_env();
/// ```
#[derive(Debug, Clone, Default)]
pub struct SolcCompilerIoReporter {
    /// where to write the output to, `None` if not enabled
    target: Option<Target>,
}

impl SolcCompilerIoReporter {
    /// Returns a new `SolcCompilerIOLayer` from the fields in the given string,
    /// ignoring any that are invalid.
    pub fn new(value: impl AsRef<str>) -> Self {
        Self { target: Some(value.as_ref().parse().unwrap_or_default()) }
    }

    /// `ETHERS_SOLC_LOG` is the default environment variable used by
    /// [`SolcCompilerIOLayer::from_default_env`]
    ///
    /// [`SolcCompilerIOLayer::from_default_env`]: #method.from_default_env
    pub const DEFAULT_ENV: &'static str = "ETHERS_SOLC_LOG";

    /// Returns a new `SolcCompilerIOLayer` from the value of the `ETHERS_SOLC_LOG` environment
    /// variable, ignoring any invalid filter directives.
    pub fn from_default_env() -> Self {
        Self::from_env(Self::DEFAULT_ENV)
    }

    /// Returns a new `SolcCompilerIOLayer` from the value of the given environment
    /// variable, ignoring any invalid filter directives.
    pub fn from_env<A: AsRef<str>>(env: A) -> Self {
        env::var(env.as_ref()).map(Self::new).unwrap_or_default()
    }

    /// Callback to write the input to disk if target is set
    pub fn log_compiler_input(&self, input: &CompilerInput, version: &Version) {
        if let Some(ref target) = self.target {
            target.write_input(input, version)
        }
    }

    /// Callback to write the input to disk if target is set
    pub fn log_compiler_output(&self, output: &CompilerOutput, version: &Version) {
        if let Some(ref target) = self.target {
            target.write_output(output, version)
        }
    }
}

impl<S> From<S> for SolcCompilerIoReporter
where
    S: AsRef<str>,
{
    fn from(s: S) -> Self {
        Self::new(s)
    }
}

/// Represents the `in=<path>,out=<path>` value
#[derive(Debug, Clone, Eq, PartialEq)]
struct Target {
    /// path where the compiler input file should be written to
    dest_input: PathBuf,
    /// path where the compiler output file should be written to
    dest_output: PathBuf,
}

impl Target {
    fn write_input(&self, input: &CompilerInput, version: &Version) {
        tracing::trace!("logging compiler input to {}", self.dest_input.display());
        match serde_json::to_string_pretty(input) {
            Ok(json) => {
                if let Err(err) = std::fs::write(get_file_name(&self.dest_input, version), json) {
                    tracing::error!("Failed to write compiler input: {}", err)
                }
            }
            Err(err) => {
                tracing::error!("Failed to serialize compiler input: {}", err)
            }
        }
    }

    fn write_output(&self, output: &CompilerOutput, version: &Version) {
        tracing::trace!("logging compiler output to {}", self.dest_output.display());
        match serde_json::to_string_pretty(output) {
            Ok(json) => {
                if let Err(err) = std::fs::write(get_file_name(&self.dest_output, version), json) {
                    tracing::error!("Failed to write compiler output: {}", err)
                }
            }
            Err(err) => {
                tracing::error!("Failed to serialize compiler output: {}", err)
            }
        }
    }
}

impl Default for Target {
    fn default() -> Self {
        Self {
            dest_input: "compiler-input.json".into(),
            dest_output: "compiler-output.json".into(),
        }
    }
}

impl FromStr for Target {
    type Err = Box<dyn std::error::Error + Send + Sync>;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut dest_input = None;
        let mut dest_output = None;
        for part in s.split(',') {
            let (name, val) =
                part.split_once('=').ok_or_else(|| BadName { name: part.to_string() })?;
            match name {
                "i" | "in" | "input" | "compilerinput" => {
                    dest_input = Some(PathBuf::from(val));
                }
                "o" | "out" | "output" | "compileroutput" => {
                    dest_output = Some(PathBuf::from(val));
                }
                _ => return Err(BadName { name: part.to_string() }.into()),
            };
        }

        Ok(Self {
            dest_input: dest_input.unwrap_or_else(|| "compiler-input.json".into()),
            dest_output: dest_output.unwrap_or_else(|| "compiler-output.json".into()),
        })
    }
}

/// Indicates that a field name specified in the env value was invalid.
#[derive(Clone, Debug, thiserror::Error)]
#[error("{}", self.name)]
pub struct BadName {
    name: String,
}

/// Returns the file name for the given version
fn get_file_name(path: impl Into<PathBuf>, v: &Version) -> PathBuf {
    let mut path = path.into();
    if let Some(stem) = path.file_stem().and_then(|s| s.to_str().map(|s| s.to_string())) {
        path.set_file_name(format!("{stem}.{}.{}.{}.json", v.major, v.minor, v.patch));
    }
    path
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_set_file_name() {
        let s = "/a/b/c/in.json";
        let p = get_file_name(s, &Version::parse("0.8.10").unwrap());
        assert_eq!(PathBuf::from("/a/b/c/in.0.8.10.json"), p);

        let s = "abc.json";
        let p = get_file_name(s, &Version::parse("0.8.10").unwrap());
        assert_eq!(PathBuf::from("abc.0.8.10.json"), p);
    }

    #[test]
    fn can_parse_target() {
        let target: Target = "in=in.json,out=out.json".parse().unwrap();
        assert_eq!(target, Target { dest_input: "in.json".into(), dest_output: "out.json".into() });

        let target: Target = "in=in.json".parse().unwrap();
        assert_eq!(target, Target { dest_input: "in.json".into(), ..Default::default() });

        let target: Target = "out=out.json".parse().unwrap();
        assert_eq!(target, Target { dest_output: "out.json".into(), ..Default::default() });
    }

    #[test]
    fn can_init_reporter_from_env() {
        let rep = SolcCompilerIoReporter::from_default_env();
        assert!(rep.target.is_none());
        std::env::set_var("ETHERS_SOLC_LOG", "in=in.json,out=out.json");
        let rep = SolcCompilerIoReporter::from_default_env();
        assert!(rep.target.is_some());
        assert_eq!(
            rep.target.unwrap(),
            Target { dest_input: "in.json".into(), dest_output: "out.json".into() }
        );
        std::env::remove_var("ETHERS_SOLC_LOG");
    }
}