• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use name_and_version::{NameAndVersion, NameAndVersionMap, NamedAndVersioned};
16 use rooted_path::RootedPath;
17 
18 use std::path::{Path, PathBuf};
19 
20 use anyhow::{anyhow, Result};
21 use walkdir::WalkDir;
22 
23 use crate::{crate_type::Crate, CrateError};
24 
25 use std::collections::BTreeMap;
26 
27 #[derive(Debug)]
28 pub struct CrateCollection {
29     crates: BTreeMap<NameAndVersion, Crate>,
30     repo_root: PathBuf,
31 }
32 
33 impl CrateCollection {
new<P: Into<PathBuf>>(repo_root: P) -> CrateCollection34     pub fn new<P: Into<PathBuf>>(repo_root: P) -> CrateCollection {
35         CrateCollection { crates: BTreeMap::new(), repo_root: repo_root.into() }
36     }
add_from(&mut self, path: impl AsRef<Path>) -> Result<()>37     pub fn add_from(&mut self, path: impl AsRef<Path>) -> Result<()> {
38         for entry_or_err in WalkDir::new(self.repo_root.join(path)) {
39             let entry = entry_or_err?;
40             if entry.file_name() == "Cargo.toml" {
41                 match Crate::from(RootedPath::new(
42                     self.repo_root.clone(),
43                     entry
44                         .path()
45                         .parent()
46                         .ok_or(anyhow!("Failed to get parent of {}", entry.path().display()))?
47                         .strip_prefix(&self.repo_root)?,
48                 )?) {
49                     Ok(krate) => self.crates.insert_or_error(
50                         NameAndVersion::new(krate.name().to_string(), krate.version().clone()),
51                         krate,
52                     )?,
53                     Err(e) => match e.downcast_ref() {
54                         Some(CrateError::VirtualCrate(_)) => (),
55                         _ => return Err(e),
56                     },
57                 };
58             }
59         }
60         Ok(())
61     }
get(&self, nv: &dyn NamedAndVersioned) -> Option<&Crate>62     pub fn get(&self, nv: &dyn NamedAndVersioned) -> Option<&Crate> {
63         self.crates.get(nv)
64     }
values(&self) -> impl Iterator<Item = &Crate>65     pub fn values(&self) -> impl Iterator<Item = &Crate> {
66         self.crates.values()
67     }
get_versions( &self, crate_name: impl AsRef<str>, ) -> Box<dyn Iterator<Item = (&NameAndVersion, &Crate)> + '_>68     pub fn get_versions(
69         &self,
70         crate_name: impl AsRef<str>,
71     ) -> Box<dyn Iterator<Item = (&NameAndVersion, &Crate)> + '_> {
72         self.crates.get_versions(crate_name.as_ref())
73     }
contains_crate(&self, crate_name: impl AsRef<str>) -> bool74     pub fn contains_crate(&self, crate_name: impl AsRef<str>) -> bool {
75         self.crates.contains_name(crate_name.as_ref())
76     }
77 }
78