foundry_compilers_artifacts_vyper/
input.rs

1use super::VyperSettings;
2use foundry_compilers_artifacts_solc::sources::Sources;
3use foundry_compilers_core::utils::strip_prefix_owned;
4use semver::Version;
5use serde::{Deserialize, Serialize};
6use std::path::Path;
7
8/// Extension of Vyper interface file.
9pub const VYPER_INTERFACE_EXTENSION: &str = "vyi";
10
11#[derive(Clone, Debug, Serialize, Deserialize)]
12pub struct VyperInput {
13    pub language: String,
14    pub sources: Sources,
15    pub interfaces: Sources,
16    pub settings: VyperSettings,
17}
18
19impl VyperInput {
20    pub fn new(sources: Sources, mut settings: VyperSettings, version: &Version) -> Self {
21        let mut new_sources = Sources::new();
22        let mut interfaces = Sources::new();
23
24        for (path, content) in sources {
25            if path.extension().is_some_and(|ext| ext == VYPER_INTERFACE_EXTENSION) {
26                // Interface .vyi files should be removed from the output selection.
27                settings.output_selection.0.remove(path.to_string_lossy().as_ref());
28                interfaces.insert(path, content);
29            } else {
30                new_sources.insert(path, content);
31            }
32        }
33
34        settings.sanitize_output_selection(version);
35        Self { language: "Vyper".to_string(), sources: new_sources, interfaces, settings }
36    }
37
38    pub fn strip_prefix(&mut self, base: &Path) {
39        self.sources = std::mem::take(&mut self.sources)
40            .into_iter()
41            .map(|(path, s)| (strip_prefix_owned(path, base), s))
42            .collect();
43
44        self.interfaces = std::mem::take(&mut self.interfaces)
45            .into_iter()
46            .map(|(path, s)| (strip_prefix_owned(path, base), s))
47            .collect();
48
49        self.settings.strip_prefix(base)
50    }
51
52    /// This will remove/adjust values in the [`VyperInput`] that are not compatible with this
53    /// version
54    pub fn sanitize(&mut self, version: &Version) {
55        self.settings.sanitize(version);
56    }
57
58    /// Consumes the type and returns a [VyperInput::sanitized] version
59    pub fn sanitized(mut self, version: &Version) -> Self {
60        self.sanitize(version);
61        self
62    }
63}