|
| 1 | +use std::fmt::Write; |
| 2 | + |
| 3 | +use super::error::TomlEditError; |
| 4 | + |
| 5 | +pub struct DepSpec { |
| 6 | + pub alias: String, |
| 7 | + pub source: Source, |
| 8 | +} |
| 9 | + |
| 10 | +pub enum Source { |
| 11 | + Git(String), |
| 12 | + Path(String), |
| 13 | +} |
| 14 | + |
| 15 | +impl DepSpec { |
| 16 | + /// Parses a raw CLI token into a [`DepSpec`]. |
| 17 | + /// |
| 18 | + /// Accepted forms: |
| 19 | + /// - `<source>`: The alias is derived from the last path segment of the source, |
| 20 | + /// with a trailing `.git` stripped. |
| 21 | + /// - `<alias>=<source>`: Both parts must be non-empty. |
| 22 | + /// |
| 23 | + /// The source is then classified as [`Source::Git`] or [`Source::Path`] based on |
| 24 | + /// its scheme or `.git` suffix. |
| 25 | + /// |
| 26 | + /// # Errors |
| 27 | + /// - `TomlEditError::MalformedDep`: If `raw` contains `=` but either side is empty, |
| 28 | + /// or if the alias cannot be derived from the source (e.g. the source contains |
| 29 | + /// no non-empty path segment). |
| 30 | + pub fn parse_dep(raw: &str) -> Result<DepSpec, TomlEditError> { |
| 31 | + let (alias, source_str) = match raw.split_once('=') { |
| 32 | + Some((a, s)) if !a.is_empty() && !s.is_empty() => (a.to_owned(), s), |
| 33 | + Some(_) => return Err(TomlEditError::MalformedDep(raw.to_owned())), |
| 34 | + None => (Self::derive_alias(raw)?, raw), |
| 35 | + }; |
| 36 | + |
| 37 | + let source = Self::classify_source(source_str); |
| 38 | + |
| 39 | + Ok(DepSpec { alias, source }) |
| 40 | + } |
| 41 | + |
| 42 | + /// Formats a batch of dependency specs as a bracketed, one-per-line list. |
| 43 | + #[must_use] |
| 44 | + pub fn format_batch(specs: &[DepSpec]) -> String { |
| 45 | + if specs.is_empty() { |
| 46 | + return "[]".to_owned(); |
| 47 | + } |
| 48 | + |
| 49 | + let mut out = String::from("["); |
| 50 | + |
| 51 | + for (index, spec) in specs.iter().enumerate() { |
| 52 | + if index > 0 { |
| 53 | + out.push(','); |
| 54 | + } |
| 55 | + |
| 56 | + let source = match &spec.source { |
| 57 | + Source::Git(url) => url.as_str(), |
| 58 | + Source::Path(p) => p.as_str(), |
| 59 | + }; |
| 60 | + let _ = write!(out, "\n {} = {}", spec.alias, source); |
| 61 | + } |
| 62 | + |
| 63 | + out.push_str("\n]"); |
| 64 | + |
| 65 | + out |
| 66 | + } |
| 67 | + |
| 68 | + /// Derives a default alias from a source string by taking its last non-empty |
| 69 | + /// path segment and stripping a trailing `.git`. |
| 70 | + /// |
| 71 | + /// # Errors |
| 72 | + /// - `BuildError::MalformedDep`: If `source` contains no non-empty path segment |
| 73 | + /// (e.g. an empty string or one consisting only of separators). |
| 74 | + fn derive_alias(source: &str) -> Result<String, TomlEditError> { |
| 75 | + let last = source |
| 76 | + .rsplit(['/', '\\']) |
| 77 | + .find(|s| !s.is_empty()) |
| 78 | + .ok_or_else(|| TomlEditError::MalformedDep(source.to_owned()))?; |
| 79 | + |
| 80 | + Ok(last.trim_end_matches(".git").to_owned()) |
| 81 | + } |
| 82 | + |
| 83 | + /// Classifies a source string as [`Source::Git`] if it carries a recognised |
| 84 | + /// scheme (`http`, `https`, `git`, `ssh`) or ends in `.git`, otherwise as |
| 85 | + /// [`Source::Path`]. The `.git` check is case-insensitive. |
| 86 | + fn classify_source(s: &str) -> Source { |
| 87 | + let git_ext = std::path::Path::new(s) |
| 88 | + .extension() |
| 89 | + .is_some_and(|ext| ext.eq_ignore_ascii_case("git")); |
| 90 | + |
| 91 | + if s.starts_with("http://") |
| 92 | + || s.starts_with("https://") |
| 93 | + || s.starts_with("git://") |
| 94 | + || s.starts_with("ssh://") |
| 95 | + || git_ext |
| 96 | + { |
| 97 | + Source::Git(s.to_owned()) |
| 98 | + } else { |
| 99 | + Source::Path(s.to_owned()) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments