use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReferencesFile {
pub meta: Meta,
pub references: Vec<Reference>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Meta {
pub created: String,
pub last_verified: Option<String>,
pub tool: String,
pub total_links: usize,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Reference {
pub url: String,
pub title: String,
pub categories: Vec<String>,
pub cited_in: Vec<String>,
pub status: Status,
#[serde(skip_serializing_if = "Option::is_none")]
pub verified: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub notes: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Status {
Pending,
Ok,
Dead,
Redirect,
Paywall,
Login,
}
impl std::fmt::Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Status::Pending => write!(f, "pending"),
Status::Ok => write!(f, "ok"),
Status::Dead => write!(f, "dead"),
Status::Redirect => write!(f, "redirect"),
Status::Paywall => write!(f, "paywall"),
Status::Login => write!(f, "login"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_status_display() {
assert_eq!(Status::Pending.to_string(), "pending");
assert_eq!(Status::Ok.to_string(), "ok");
assert_eq!(Status::Dead.to_string(), "dead");
assert_eq!(Status::Redirect.to_string(), "redirect");
assert_eq!(Status::Paywall.to_string(), "paywall");
assert_eq!(Status::Login.to_string(), "login");
}
#[test]
fn test_deserialize_status() {
let json = r#""ok""#;
let status: Status = serde_json::from_str(json).unwrap();
assert_eq!(status, Status::Ok);
}
#[test]
fn test_serialize_reference() {
let reference = Reference {
url: "https://example.com".to_string(),
title: "Example".to_string(),
categories: vec!["test".to_string()],
cited_in: vec!["README.md".to_string()],
status: Status::Pending,
verified: None,
notes: None,
};
let yaml = serde_yaml::to_string(&reference).unwrap();
assert!(yaml.contains("url: https://example.com"));
assert!(yaml.contains("status: pending"));
assert!(!yaml.contains("verified:"));
assert!(!yaml.contains("notes:"));
}
#[test]
fn test_full_file_roundtrip() {
let file = ReferencesFile {
meta: Meta {
created: "2025-12-15".to_string(),
last_verified: None,
tool: "ref".to_string(),
total_links: 1,
},
references: vec![Reference {
url: "https://example.com".to_string(),
title: "Example".to_string(),
categories: vec!["test".to_string()],
cited_in: vec!["README.md".to_string()],
status: Status::Ok,
verified: Some("2025-12-15T10:00:00Z".to_string()),
notes: None,
}],
};
let yaml = serde_yaml::to_string(&file).unwrap();
let parsed: ReferencesFile = serde_yaml::from_str(&yaml).unwrap();
assert_eq!(parsed.meta.total_links, 1);
assert_eq!(parsed.references[0].status, Status::Ok);
}
}