| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use std::collections::{BTreeMap, BTreeSet}; |
| 7 | | use std::path::Path; |
| 8 | |
|
| 9 | |
|
| 10 | | pub type Crate = (String, String); |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | 2× | pub fn from_lock(text: &str) -> Result<BTreeSet<Crate>, String> { |
| 16 | 2× | let lock: toml::Value = text.parse().map_err(|e| format!("not a Cargo.lock: {e}"))?; |
| 17 | 1× | let mut out = BTreeSet::new(); |
| 18 | 4× | for p in lock.get("package").and_then(|p| p.as_array()).into_iter().flatten() { |
| 19 | 4× | let (Some(name), Some(version)) = (p.get("name").and_then(|v| v.as_str()), p.get("version").and_then(|v| v.as_str())) else { |
| 20 | 0× | continue; |
| 21 | | }; |
| 22 | 4× | if p.get("source").is_some() { |
| 23 | 3× | out.insert((name.to_string(), version.to_string())); |
| 24 | 3× | } |
| 25 | | } |
| 26 | 1× | Ok(out) |
| 27 | 2× | } |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 3× | pub fn from_list(text: &str) -> BTreeSet<Crate> { |
| 32 | 3× | text.lines() |
| 33 | 3× | .map(str::trim) |
| 34 | 9× | .filter(|l| !l.is_empty() && !l.starts_with('#')) |
| 35 | 7× | .filter_map(|l| { |
| 36 | 7× | let mut w = l.split_whitespace(); |
| 37 | 7× | Some((w.next()?.to_string(), w.next()?.to_string())) |
| 38 | 7× | }) |
| 39 | 3× | .collect() |
| 40 | 3× | } |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 1× | pub fn from_vendor(dir: &Path) -> Result<BTreeSet<Crate>, String> { |
| 45 | 1× | let entries = std::fs::read_dir(dir).map_err(|e| format!("{}: {e}", dir.display()))?; |
| 46 | 1× | let mut out = BTreeSet::new(); |
| 47 | 3× | for e in entries.flatten() { |
| 48 | 3× | let manifest = e.path().join("Cargo.toml"); |
| 49 | 3× | let Ok(text) = std::fs::read_to_string(&manifest) else { continue }; |
| 50 | 2× | let Ok(v) = text.parse::<toml::Value>() else { continue }; |
| 51 | 2× | let pkg = v.get("package"); |
| 52 | 4× | let field = |k: &str| pkg.and_then(|p| p.get(k)).and_then(|x| x.as_str()).map(str::to_string); |
| 53 | 2× | if let (Some(n), Some(ver)) = (field("name"), field("version")) { |
| 54 | 2× | out.insert((n, ver)); |
| 55 | 2× | } |
| 56 | | } |
| 57 | 1× | Ok(out) |
| 58 | 1× | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | 2× | pub fn missing(need: &BTreeSet<Crate>, have: &BTreeSet<Crate>) -> Vec<(Crate, Vec<String>)> { |
| 63 | 2× | let mut versions: BTreeMap<&str, Vec<String>> = BTreeMap::new(); |
| 64 | 5× | for (n, v) in have { |
| 65 | 5× | versions.entry(n.as_str()).or_default().push(v.clone()); |
| 66 | 5× | } |
| 67 | 6× | need.iter().filter(|c| !have.contains(*c)).map(|c| (c.clone(), versions.get(c.0.as_str()).cloned().unwrap_or_default())).collect() |
| 68 | 2× | } |
| 69 | |
|
| 70 | |
|
| 71 | 1× | pub fn render_list(crates: &BTreeSet<Crate>) -> String { |
| 72 | 3× | crates.iter().map(|(n, v)| format!("{n} {v}\n")).collect() |
| 73 | 1× | } |
| 74 | |
|
| 75 | | #[cfg(test)] |
| 76 | | mod tests { |
| 77 | | use super::*; |
| 78 | |
|
| 79 | | const LOCK: &str = r#" |
| 80 | | version = 4 |
| 81 | |
|
| 82 | | [[package]] |
| 83 | | name = "mine" |
| 84 | | version = "0.1.0" |
| 85 | | dependencies = ["rustls"] |
| 86 | |
|
| 87 | | [[package]] |
| 88 | | name = "rustls" |
| 89 | | version = "0.23.45" |
| 90 | | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 91 | |
|
| 92 | | [[package]] |
| 93 | | name = "tokio-rustls" |
| 94 | | version = "0.26.5" |
| 95 | | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 96 | |
|
| 97 | | [[package]] |
| 98 | | name = "deranged" |
| 99 | | version = "0.5.8" |
| 100 | | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 101 | | "#; |
| 102 | |
|
| 103 | | #[test] |
| 104 | 1× | fn names_what_the_image_lacks_and_what_it_has_instead() { |
| 105 | 1× | let need = from_lock(LOCK).unwrap(); |
| 106 | 1× | assert_eq!(need.len(), 3, "the workspace's own package is not vendored"); |
| 107 | 1× | let have = from_list("# the image\nrustls 0.23.45\ntokio-rustls 0.26.4\n\n"); |
| 108 | 1× | let m = missing(&need, &have); |
| 109 | 1× | assert_eq!( |
| 110 | | m, |
| 111 | 1× | vec![(("deranged".into(), "0.5.8".into()), vec![]), (("tokio-rustls".into(), "0.26.5".into()), vec!["0.26.4".into()]),] |
| 112 | | ); |
| 113 | 1× | assert!(missing(&need, &need).is_empty()); |
| 114 | 1× | assert_eq!(from_list(&render_list(&need)), need); |
| 115 | 1× | assert!(from_lock("not = [toml").is_err()); |
| 116 | 1× | } |
| 117 | |
|
| 118 | | #[test] |
| 119 | 1× | fn reads_a_vendor_directory() { |
| 120 | 1× | let dir = tempfile::tempdir().unwrap(); |
| 121 | 2× | for (d, n, v) in [("rustls-0.23.45", "rustls", "0.23.45"), ("pre-1.0.0-rc.1", "pre", "1.0.0-rc.1")] { |
| 122 | 2× | std::fs::create_dir(dir.path().join(d)).unwrap(); |
| 123 | 2× | std::fs::write(dir.path().join(d).join("Cargo.toml"), format!("[package]\nname = \"{n}\"\nversion = \"{v}\"\n")).unwrap(); |
| 124 | 2× | } |
| 125 | 1× | std::fs::create_dir(dir.path().join("junk")).unwrap(); |
| 126 | 1× | let got = from_vendor(dir.path()).unwrap(); |
| 127 | 1× | assert_eq!(got, from_list("rustls 0.23.45\npre 1.0.0-rc.1\n")); |
| 128 | 1× | } |
| 129 | | } |