pub mod docker;
pub mod nerdctl;
pub mod tar;
pub mod naming;
mod source;
pub use source::Source;
pub use docker::DockerSource;
pub use nerdctl::NerdctlSource;
pub use tar::TarSource;
pub fn sanitize_branch_name(name: &str) -> String {
name.chars()
.map(|c| match c {
' ' | '\t' | '\n' | '\r' => '-',
'/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '-',
'.' => '-',
c if c.is_alphanumeric() || c == '-' || c == '_' || c == '#' => c,
_ => '-',
})
.collect::<String>()
.split('-')
.filter(|s| !s.is_empty())
.collect::<Vec<_>>()
.join("-")
.trim_matches('-')
.to_string()
}
pub fn extract_short_digest(image_id: &str) -> Option<String> {
image_id
.strip_prefix("sha256:")
.map(|digest| digest.chars().take(12).collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_short_digest() {
assert_eq!(
extract_short_digest("sha256:1234567890abcdef1234567890abcdef12345678"),
Some("1234567890ab".to_string())
);
assert_eq!(
extract_short_digest("sha256:abcdef123456"),
Some("abcdef123456".to_string())
);
assert_eq!(extract_short_digest("invalid-id"), None);
assert_eq!(extract_short_digest(""), None);
}
#[test]
fn test_sanitize_branch_name() {
assert_eq!(sanitize_branch_name("hello-world"), "hello-world");
assert_eq!(sanitize_branch_name("hello world"), "hello-world");
assert_eq!(sanitize_branch_name("my:app/v1.0"), "my-app-v1-0");
assert_eq!(
sanitize_branch_name("file with spaces & symbols!"),
"file-with-spaces-symbols"
);
assert_eq!(sanitize_branch_name("---test---"), "test");
assert_eq!(sanitize_branch_name("a..b..c"), "a-b-c");
assert_eq!(
sanitize_branch_name("nginx_1.21-alpine"),
"nginx_1-21-alpine"
);
}
}