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
use self::{error::VyperCompilationError, input::VyperInput, parser::VyperParsedSource};
use super::{Compiler, CompilerInput, CompilerOutput};
use crate::{
    artifacts::Source,
    error::{Result, SolcError},
};
use semver::Version;
use serde::{de::DeserializeOwned, Serialize};
use std::{
    path::{Path, PathBuf},
    process::{Command, Stdio},
    str::FromStr,
};

pub mod error;
pub mod input;
pub mod parser;
pub mod settings;
pub use settings::VyperSettings;

pub type VyperCompilerOutput = CompilerOutput<VyperCompilationError>;

/// File extensions that are recognized as Vyper source files.
pub const VYPER_EXTENSIONS: &[&str] = &["vy"];

#[derive(Debug, Clone)]
pub struct Vyper {
    pub path: PathBuf,
    pub version: Version,
}

impl Vyper {
    /// Creates a new instance of the Vyper compiler. Uses the `vyper` binary in the system `PATH`.
    pub fn new(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();
        let version = Self::version(path)?;
        Ok(Self { path: path.into(), version })
    }

    /// Convenience function for compiling all sources under the given path
    pub fn compile_source(&self, path: impl AsRef<Path>) -> Result<VyperCompilerOutput> {
        let path = path.as_ref();
        let mut res: VyperCompilerOutput = Default::default();
        for input in VyperInput::build(
            Source::read_all_from(path, VYPER_EXTENSIONS)?,
            Default::default(),
            &self.version,
        ) {
            let output = self.compile(&input)?;
            res.merge(output)
        }
        Ok(res)
    }

    /// Same as [`Self::compile()`], but only returns those files which are included in the
    /// `CompilerInput`.
    ///
    /// In other words, this removes those files from the `VyperCompilerOutput` that are __not__
    /// included in the provided `CompilerInput`.
    ///
    /// # Examples
    pub fn compile_exact(&self, input: &VyperInput) -> Result<VyperCompilerOutput> {
        let mut out = self.compile(input)?;
        out.retain_files(input.sources.keys().map(|p| p.as_path()));
        Ok(out)
    }

    /// Compiles with `--standard-json` and deserializes the output as [`VyperCompilerOutput`].
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use foundry_compilers::{CompilerInput, Solc};
    ///
    /// let solc = Solc::default();
    /// let input = CompilerInput::new("./contracts")?;
    /// let output = solc.compile(&input)?;
    /// # Ok::<_, Box<dyn std::error::Error>>(())
    /// ```
    pub fn compile<T: Serialize>(&self, input: &T) -> Result<VyperCompilerOutput> {
        self.compile_as(input)
    }

    /// Compiles with `--standard-json` and deserializes the output as the given `D`.
    pub fn compile_as<T: Serialize, D: DeserializeOwned>(&self, input: &T) -> Result<D> {
        let output = self.compile_output(input)?;

        // Only run UTF-8 validation once.
        let output = std::str::from_utf8(&output).map_err(|_| SolcError::InvalidUtf8)?;

        trace!("vyper compiler output: {}", output);

        Ok(serde_json::from_str(output)?)
    }

    /// Compiles with `--standard-json` and returns the raw `stdout` output.
    #[instrument(name = "compile", level = "debug", skip_all)]
    pub fn compile_output<T: Serialize>(&self, input: &T) -> Result<Vec<u8>> {
        let mut cmd = Command::new(&self.path);
        cmd.arg("--standard-json")
            .stdin(Stdio::piped())
            .stderr(Stdio::piped())
            .stdout(Stdio::piped());

        trace!(input=%serde_json::to_string(input).unwrap_or_else(|e| e.to_string()));
        debug!(?cmd, "compiling");

        let mut child = cmd.spawn().map_err(self.map_io_err())?;
        debug!("spawned");

        let stdin = child.stdin.as_mut().unwrap();
        serde_json::to_writer(stdin, input)?;
        debug!("wrote JSON input to stdin");

        let output = child.wait_with_output().map_err(self.map_io_err())?;
        debug!(%output.status, output.stderr = ?String::from_utf8_lossy(&output.stderr), "finished");

        if output.status.success() {
            Ok(output.stdout)
        } else {
            Err(SolcError::solc_output(&output))
        }
    }

    /// Invokes `vyper --version` and parses the output as a SemVer [`Version`].
    #[instrument(level = "debug", skip_all)]
    pub fn version(vyper: impl Into<PathBuf>) -> Result<Version> {
        let vyper = vyper.into();
        let mut cmd = Command::new(vyper.clone());
        cmd.arg("--version").stdin(Stdio::piped()).stderr(Stdio::piped()).stdout(Stdio::piped());
        debug!(?cmd, "getting Solc version");
        let output = cmd.output().map_err(|e| SolcError::io(e, vyper))?;
        trace!(?output);
        if output.status.success() {
            let stdout = String::from_utf8_lossy(&output.stdout);
            Ok(Version::from_str(stdout.trim())?)
        } else {
            Err(SolcError::solc_output(&output))
        }
    }

    fn map_io_err(&self) -> impl FnOnce(std::io::Error) -> SolcError + '_ {
        move |err| SolcError::io(err, &self.path)
    }
}

impl Compiler for Vyper {
    const FILE_EXTENSIONS: &'static [&'static str] = VYPER_EXTENSIONS;

    type Settings = VyperSettings;
    type CompilationError = VyperCompilationError;
    type ParsedSource = VyperParsedSource;
    type Input = VyperInput;

    fn compile(&self, input: &Self::Input) -> Result<VyperCompilerOutput> {
        self.compile(input)
    }

    fn version(&self) -> &Version {
        &self.version
    }
}