feat: Implement add_tags function with tag merging and deduplication logic

This commit is contained in:
Moritz Böhme 2025-02-23 15:52:34 +01:00
parent 4dd9edee61
commit 78180a516f

View file

@ -67,6 +67,18 @@ pub fn parse_tags(filename: &str) -> Result<(String, Vec<String>, String), Parse
Ok((base_name, tags, extension))
}
pub fn add_tags(current: Vec<String>, new: Vec<String>) -> Vec<String> {
let mut result = current;
for tag in new {
if !result.contains(&tag) {
result.push(tag);
}
}
result
}
pub fn serialize_tags(base: &str, tags: &[String], extension: &str) -> String {
let mut sorted_tags = tags.to_vec();
sorted_tags.sort();
@ -117,6 +129,46 @@ mod tests {
assert_eq!(result, "README -- note");
}
#[test]
fn test_add_tags_no_duplicates() {
let current = vec!["tag1".to_string(), "tag2".to_string()];
let new = vec!["tag3".to_string(), "tag4".to_string()];
let result = add_tags(current, new);
assert_eq!(result, vec!["tag1", "tag2", "tag3", "tag4"]);
}
#[test]
fn test_add_tags_with_duplicates() {
let current = vec!["tag1".to_string(), "tag2".to_string()];
let new = vec!["tag2".to_string(), "tag3".to_string()];
let result = add_tags(current, new);
assert_eq!(result, vec!["tag1", "tag2", "tag3"]);
}
#[test]
fn test_add_tags_case_sensitive() {
let current = vec!["Tag1".to_string(), "tag2".to_string()];
let new = vec!["tag1".to_string(), "Tag2".to_string()];
let result = add_tags(current, new);
assert_eq!(result, vec!["Tag1", "tag2", "tag1", "Tag2"]);
}
#[test]
fn test_add_tags_empty_current() {
let current = Vec::new();
let new = vec!["tag1".to_string(), "tag2".to_string()];
let result = add_tags(current, new);
assert_eq!(result, vec!["tag1", "tag2"]);
}
#[test]
fn test_add_tags_empty_new() {
let current = vec!["tag1".to_string(), "tag2".to_string()];
let new = Vec::new();
let result = add_tags(current, new);
assert_eq!(result, vec!["tag1", "tag2"]);
}
#[test]
fn test_validate_tag_valid() {
assert!(validate_tag("valid-tag").is_ok());