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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::SourceFile;
use semver::Version;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, path::Path};

/// (source_file path  -> `SourceFile` + solc version)
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct VersionedSourceFiles(pub BTreeMap<String, Vec<VersionedSourceFile>>);

impl VersionedSourceFiles {
    /// Converts all `\\` separators in _all_ paths to `/`
    pub fn slash_paths(&mut self) {
        #[cfg(windows)]
        {
            use path_slash::PathExt;
            self.0 = std::mem::take(&mut self.0)
                .into_iter()
                .map(|(path, files)| (Path::new(&path).to_slash_lossy().to_string(), files))
                .collect()
        }
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns an iterator over all files
    pub fn files(&self) -> impl Iterator<Item = &String> + '_ {
        self.0.keys()
    }

    /// Returns an iterator over the source files' ids and path
    ///
    /// ```
    /// use std::collections::BTreeMap;
    /// use ethers_solc::sources::VersionedSourceFiles;
    /// # fn demo(files: VersionedSourceFiles) {
    /// let sources: BTreeMap<u32,String> = files.into_ids().collect();
    /// # }
    /// ```
    pub fn into_ids(self) -> impl Iterator<Item = (u32, String)> {
        self.into_sources().map(|(path, source)| (source.id, path))
    }

    /// Returns an iterator over the source files' paths and ids
    ///
    /// ```
    /// use std::collections::BTreeMap;
    /// use ethers_solc::artifacts::SourceFiles;
    /// # fn demo(files: SourceFiles) {
    /// let sources :BTreeMap<String, u32> = files.into_paths().collect();
    /// # }
    /// ```
    pub fn into_paths(self) -> impl Iterator<Item = (String, u32)> {
        self.into_ids().map(|(id, path)| (path, id))
    }

    /// Returns an iterator over the source files' ids and path
    ///
    /// ```
    /// use std::collections::BTreeMap;
    /// use semver::Version;
    /// use ethers_solc::sources::VersionedSourceFiles;
    /// # fn demo(files: VersionedSourceFiles) {
    /// let sources: BTreeMap<(u32, Version) ,String> = files.into_ids_with_version().map(|(id, source, version)|((id, version), source)).collect();
    /// # }
    /// ```
    pub fn into_ids_with_version(self) -> impl Iterator<Item = (u32, String, Version)> {
        self.into_sources_with_version().map(|(path, source, version)| (source.id, path, version))
    }

    /// Finds the _first_ source file with the given path
    ///
    /// # Example
    ///
    /// ```
    /// use ethers_solc::Project;
    /// use ethers_solc::artifacts::*;
    /// # fn demo(project: Project) {
    /// let output = project.compile().unwrap().output();
    /// let source_file = output.sources.find_file("src/Greeter.sol").unwrap();
    /// # }
    /// ```
    pub fn find_file(&self, source_file: impl AsRef<str>) -> Option<&SourceFile> {
        let source_file_name = source_file.as_ref();
        self.sources().find_map(
            |(path, source_file)| {
                if path == source_file_name {
                    Some(source_file)
                } else {
                    None
                }
            },
        )
    }

    /// Same as [Self::find_file] but also checks for version
    pub fn find_file_and_version(&self, path: &str, version: &Version) -> Option<&SourceFile> {
        self.0.get(path).and_then(|contracts| {
            contracts.iter().find_map(|source| {
                if source.version == *version {
                    Some(&source.source_file)
                } else {
                    None
                }
            })
        })
    }

    /// Finds the _first_ source file with the given id
    ///
    /// # Example
    ///
    /// ```
    /// use ethers_solc::Project;
    /// use ethers_solc::artifacts::*;
    /// # fn demo(project: Project) {
    /// let output = project.compile().unwrap().output();
    /// let source_file = output.sources.find_id(0).unwrap();
    /// # }
    /// ```
    pub fn find_id(&self, id: u32) -> Option<&SourceFile> {
        self.sources().filter(|(_, source)| source.id == id).map(|(_, source)| source).next()
    }

    /// Same as [Self::find_id] but also checks for version
    pub fn find_id_and_version(&self, id: u32, version: &Version) -> Option<&SourceFile> {
        self.sources_with_version()
            .filter(|(_, source, v)| source.id == id && *v == version)
            .map(|(_, source, _)| source)
            .next()
    }

    /// Removes the _first_ source_file with the given path from the set
    ///
    /// # Example
    ///
    /// ```
    /// use ethers_solc::Project;
    /// use ethers_solc::artifacts::*;
    /// # fn demo(project: Project) {
    /// let (mut sources, _) = project.compile().unwrap().output().split();
    /// let source_file = sources.remove_by_path("src/Greeter.sol").unwrap();
    /// # }
    /// ```
    pub fn remove_by_path(&mut self, source_file: impl AsRef<str>) -> Option<SourceFile> {
        let source_file_path = source_file.as_ref();
        self.0.get_mut(source_file_path).and_then(|all_sources| {
            if !all_sources.is_empty() {
                Some(all_sources.remove(0).source_file)
            } else {
                None
            }
        })
    }

    /// Removes the _first_ source_file with the given id from the set
    ///
    /// # Example
    ///
    /// ```
    /// use ethers_solc::Project;
    /// use ethers_solc::artifacts::*;
    /// # fn demo(project: Project) {
    /// let (mut sources, _) = project.compile().unwrap().output().split();
    /// let source_file = sources.remove_by_id(0).unwrap();
    /// # }
    /// ```
    pub fn remove_by_id(&mut self, id: u32) -> Option<SourceFile> {
        self.0
            .values_mut()
            .filter_map(|sources| {
                sources
                    .iter()
                    .position(|source| source.source_file.id == id)
                    .map(|pos| sources.remove(pos).source_file)
            })
            .next()
    }

    /// Iterate over all contracts and their names
    pub fn sources(&self) -> impl Iterator<Item = (&String, &SourceFile)> {
        self.0.iter().flat_map(|(path, sources)| {
            sources.iter().map(move |source| (path, &source.source_file))
        })
    }

    /// Returns an iterator over (`file`,  `SourceFile`, `Version`)
    pub fn sources_with_version(&self) -> impl Iterator<Item = (&String, &SourceFile, &Version)> {
        self.0.iter().flat_map(|(file, sources)| {
            sources.iter().map(move |c| (file, &c.source_file, &c.version))
        })
    }

    /// Returns an iterator over all contracts and their source names.
    ///
    /// ```
    /// use std::collections::BTreeMap;
    /// use ethers_solc::{ artifacts::* };
    /// use ethers_solc::sources::VersionedSourceFiles;
    /// # fn demo(sources: VersionedSourceFiles) {
    /// let sources: BTreeMap<String, SourceFile> = sources
    ///     .into_sources()
    ///     .collect();
    /// # }
    /// ```
    pub fn into_sources(self) -> impl Iterator<Item = (String, SourceFile)> {
        self.0.into_iter().flat_map(|(path, sources)| {
            sources.into_iter().map(move |source| (path.clone(), source.source_file))
        })
    }

    /// Returns an iterator over all contracts and their source names.
    ///
    /// ```
    /// use std::collections::BTreeMap;
    /// use semver::Version;
    /// use ethers_solc::{ artifacts::* };
    /// use ethers_solc::sources::VersionedSourceFiles;
    /// # fn demo(sources: VersionedSourceFiles) {
    /// let sources: BTreeMap<(String,Version), SourceFile> = sources
    ///     .into_sources_with_version().map(|(path, source, version)|((path,version), source))
    ///     .collect();
    /// # }
    /// ```
    pub fn into_sources_with_version(self) -> impl Iterator<Item = (String, SourceFile, Version)> {
        self.0.into_iter().flat_map(|(path, sources)| {
            sources
                .into_iter()
                .map(move |source| (path.clone(), source.source_file, source.version))
        })
    }

    /// Sets the sources' file paths to `root` adjoined to `self.file`.
    pub fn join_all(&mut self, root: impl AsRef<Path>) -> &mut Self {
        let root = root.as_ref();
        self.0 = std::mem::take(&mut self.0)
            .into_iter()
            .map(|(file_path, sources)| {
                (root.join(file_path).to_string_lossy().to_string(), sources)
            })
            .collect();
        self
    }

    /// Removes `base` from all source file paths
    pub fn strip_prefix_all(&mut self, base: impl AsRef<Path>) -> &mut Self {
        let base = base.as_ref();
        self.0 = std::mem::take(&mut self.0)
            .into_iter()
            .map(|(file_path, sources)| {
                let p = Path::new(&file_path);
                (
                    p.strip_prefix(base)
                        .map(|p| p.to_string_lossy().to_string())
                        .unwrap_or(file_path),
                    sources,
                )
            })
            .collect();
        self
    }
}

impl AsRef<BTreeMap<String, Vec<VersionedSourceFile>>> for VersionedSourceFiles {
    fn as_ref(&self) -> &BTreeMap<String, Vec<VersionedSourceFile>> {
        &self.0
    }
}

impl AsMut<BTreeMap<String, Vec<VersionedSourceFile>>> for VersionedSourceFiles {
    fn as_mut(&mut self) -> &mut BTreeMap<String, Vec<VersionedSourceFile>> {
        &mut self.0
    }
}

impl IntoIterator for VersionedSourceFiles {
    type Item = (String, Vec<VersionedSourceFile>);
    type IntoIter = std::collections::btree_map::IntoIter<String, Vec<VersionedSourceFile>>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

/// A [SourceFile] and the compiler version used to compile it
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct VersionedSourceFile {
    pub source_file: SourceFile,
    pub version: Version,
}