Skip to main content

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(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}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use foundry_compilers_artifacts_solc::EvmVersion;
69
70    #[test]
71    fn normalizes_evm_version_on_creation() {
72        // Vyper 0.4.3 only supports up to Prague, not Osaka
73        let vyper_version = Version::new(0, 4, 3);
74        let settings = VyperSettings { evm_version: Some(EvmVersion::Osaka), ..Default::default() };
75
76        let input = VyperInput::new(Sources::new(), settings, &vyper_version);
77
78        // Should be normalized to Prague (max supported by 0.4.3)
79        assert_eq!(input.settings.evm_version, Some(EvmVersion::Prague));
80    }
81
82    #[test]
83    fn normalizes_evm_version_for_older_vyper() {
84        // Vyper 0.3.7 only supports up to Paris
85        let vyper_version = Version::new(0, 3, 7);
86        let settings =
87            VyperSettings { evm_version: Some(EvmVersion::Cancun), ..Default::default() };
88
89        let input = VyperInput::new(Sources::new(), settings, &vyper_version);
90
91        // Should be normalized to Paris (max supported by 0.3.7)
92        assert_eq!(input.settings.evm_version, Some(EvmVersion::Paris));
93    }
94
95    #[test]
96    fn keeps_supported_evm_version() {
97        let vyper_version = Version::new(0, 4, 3);
98        let settings =
99            VyperSettings { evm_version: Some(EvmVersion::Cancun), ..Default::default() };
100
101        let input = VyperInput::new(Sources::new(), settings, &vyper_version);
102
103        // Cancun is supported by 0.4.3, should remain unchanged
104        assert_eq!(input.settings.evm_version, Some(EvmVersion::Cancun));
105    }
106}