#![deny(missing_docs)]
#[cfg(feature = "lossless")]
pub mod lossless;
pub mod lossy;
pub use lossy::Copyright;
pub const CURRENT_FORMAT: &str =
"https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/";
pub const KNOWN_FORMATS: &[&str] = &[CURRENT_FORMAT];
pub mod expression;
pub mod glob;
pub use expression::{ExprParseError, LicenseExpr};
pub use glob::{GlobError, GlobPattern};
fn decode_field_text(text: &str) -> String {
text.lines()
.map(|line| {
if line == "." {
""
} else {
line
}
})
.collect::<Vec<_>>()
.join("\n")
}
fn encode_field_text(text: &str) -> String {
text.lines()
.map(|line| {
if line.is_empty() {
"."
} else {
line
}
})
.collect::<Vec<_>>()
.join("\n")
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum License {
Name(String),
Text(String),
Named(String, String),
}
impl License {
pub fn name(&self) -> Option<&str> {
match self {
License::Name(name) => Some(name),
License::Text(_) => None,
License::Named(name, _) => Some(name),
}
}
pub fn text(&self) -> Option<&str> {
match self {
License::Name(_) => None,
License::Text(text) => Some(text),
License::Named(_, text) => Some(text),
}
}
pub fn expr(&self) -> Option<LicenseExpr> {
self.name().map(LicenseExpr::parse)
}
}
impl std::str::FromStr for License {
type Err = String;
fn from_str(text: &str) -> Result<Self, Self::Err> {
if let Some((name, rest)) = text.split_once('\n') {
let decoded_text = decode_field_text(rest);
if name.is_empty() {
Ok(License::Text(decoded_text))
} else {
Ok(License::Named(name.to_string(), decoded_text))
}
} else {
Ok(License::Name(text.to_string()))
}
}
}
impl std::fmt::Display for License {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
License::Name(name) => f.write_str(name),
License::Text(text) => write!(f, "\n{}", encode_field_text(text)),
License::Named(name, text) => write!(f, "{}\n{}", name, encode_field_text(text)),
}
}
}
pub fn pattern_depth(pattern: &str) -> usize {
pattern.matches('/').count()
}
pub fn is_debian_pattern(pattern: &str) -> bool {
let trimmed = pattern.trim();
trimmed.starts_with("debian/") || trimmed == "debian/*"
}
pub fn pattern_sort_key(pattern: &str, depth: usize) -> (u8, usize) {
let trimmed = pattern.trim();
if trimmed == "*" {
(0, 0)
} else if is_debian_pattern(pattern) {
(2, depth)
} else {
(1, depth)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_decode_field_text() {
let input = "line 1\n.\nline 3";
let output = decode_field_text(input);
assert_eq!(output, "line 1\n\nline 3");
}
#[test]
fn test_decode_field_text_no_markers() {
let input = "line 1\nline 2\nline 3";
let output = decode_field_text(input);
assert_eq!(output, input);
}
#[test]
fn test_license_from_str_with_paragraph_markers() {
let input = "GPL-3+\nThis is line 1\n.\nThis is line 3";
let license: License = input.parse().unwrap();
match license {
License::Named(name, text) => {
assert_eq!(name, "GPL-3+");
assert_eq!(text, "This is line 1\n\nThis is line 3");
assert!(!text.contains("\n.\n"));
}
_ => panic!("Expected Named license"),
}
}
#[test]
fn test_encode_field_text() {
let input = "line 1\n\nline 3";
let output = encode_field_text(input);
assert_eq!(output, "line 1\n.\nline 3");
}
#[test]
fn test_encode_decode_round_trip() {
let original = "First paragraph\n\nSecond paragraph\n\nThird paragraph";
let encoded = encode_field_text(original);
let decoded = decode_field_text(&encoded);
assert_eq!(
decoded, original,
"Round-trip encoding/decoding should preserve text"
);
}
#[test]
fn test_license_display_encodes_blank_lines() {
let license = License::Named("MIT".to_string(), "Line 1\n\nLine 2".to_string());
let displayed = license.to_string();
assert_eq!(displayed, "MIT\nLine 1\n.\nLine 2");
assert!(displayed.contains("\n.\n"), "Should contain period marker");
assert_eq!(
displayed.matches("\n\n").count(),
0,
"Should not contain literal blank lines"
);
}
#[test]
fn test_pattern_depth() {
assert_eq!(pattern_depth("*"), 0);
assert_eq!(pattern_depth("src/*"), 1);
assert_eq!(pattern_depth("src/foo/*"), 2);
assert_eq!(pattern_depth("a/b/c/d/*"), 4);
assert_eq!(pattern_depth("debian/*"), 1);
}
#[test]
fn test_is_debian_pattern() {
assert!(is_debian_pattern("debian/*"));
assert!(is_debian_pattern("debian/patches/*"));
assert!(is_debian_pattern(" debian/* "));
assert!(!is_debian_pattern("*"));
assert!(!is_debian_pattern("src/*"));
assert!(!is_debian_pattern("src/debian/*"));
}
#[test]
fn test_pattern_sort_key() {
assert_eq!(pattern_sort_key("*", 0), (0, 0));
assert_eq!(pattern_sort_key(" * ", 0), (0, 0));
assert_eq!(pattern_sort_key("src/*", 1), (1, 1));
assert_eq!(pattern_sort_key("src/foo/*", 2), (1, 2));
assert_eq!(pattern_sort_key("tests/*", 1), (1, 1));
assert_eq!(pattern_sort_key("debian/*", 1), (2, 1));
assert_eq!(pattern_sort_key("debian/patches/*", 2), (2, 2));
}
#[test]
fn test_pattern_sort_key_ordering() {
assert!(pattern_sort_key("*", 0) < pattern_sort_key("src/*", 1));
assert!(pattern_sort_key("*", 0) < pattern_sort_key("debian/*", 1));
assert!(pattern_sort_key("src/*", 1) < pattern_sort_key("debian/*", 1));
assert!(pattern_sort_key("tests/*", 1) < pattern_sort_key("debian/*", 1));
assert!(pattern_sort_key("src/*", 1) < pattern_sort_key("src/foo/*", 2));
assert!(pattern_sort_key("debian/*", 1) < pattern_sort_key("debian/patches/*", 2));
assert!(pattern_sort_key("src/*", 1) < pattern_sort_key("debian/*", 1));
}
}