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