lift main.rs business logic into the library
The chlog target-series resolution becomes changelog::series_candidates (UNRELEASED pinning, development-series default and fallbacks modeled by SeriesCandidates), PPA references get package_info::split_ppa (shared by pull and deb, now also rejecting empty parts), and SSH endpoints get context::ContextConfig::from_endpoint — so a library consumer can resolve series, validate PPAs and build context configurations without reimplementing the CLI's rules. All three carry unit tests; main.rs keeps only parsing of flags and error handling.
This commit is contained in:
@@ -9,6 +9,22 @@ use crate::apt::release::{self, VerifiedRelease};
|
||||
use crossterm::style::Stylize;
|
||||
use log::{debug, warn};
|
||||
|
||||
/// Split a PPA reference into its `(user, name)` parts
|
||||
///
|
||||
/// A PPA is written `user/ppa_name` (e.g. `user/my-ppa`); anything else —
|
||||
/// more segments, empty parts — is a format error carrying the canonical
|
||||
/// message.
|
||||
pub fn split_ppa(ppa: &str) -> Result<(&str, &str), String> {
|
||||
let parts: Vec<&str> = ppa.split('/').collect();
|
||||
if parts.len() == 2 && !parts[0].is_empty() && !parts[1].is_empty() {
|
||||
Ok((parts[0], parts[1]))
|
||||
} else {
|
||||
Err(format!(
|
||||
"Invalid PPA format: '{ppa}'. Expected: user/ppa_name"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert a PPA specification to a base URL
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -943,6 +959,25 @@ pub async fn lookup(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// `user/ppa_name` splits into its two parts.
|
||||
#[test]
|
||||
fn split_ppa_parses_the_canonical_form() {
|
||||
assert_eq!(split_ppa("user/my-ppa"), Ok(("user", "my-ppa")));
|
||||
}
|
||||
|
||||
/// Anything but exactly two non-empty segments is rejected, with the
|
||||
/// canonical message.
|
||||
#[test]
|
||||
fn split_ppa_rejects_malformed_references() {
|
||||
for bad in ["", "user", "user/", "/ppa", "a/b/c"] {
|
||||
let err = split_ppa(bad).unwrap_err();
|
||||
assert!(err.contains("Invalid PPA format"), "{err}");
|
||||
assert!(err.contains(bad), "{err}");
|
||||
}
|
||||
}
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Serve canned byte responses on a local port, one per connection (the
|
||||
/// last response repeats), and return the base URL
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user