make api source response optional, implement tests

This commit is contained in:
yggverse 2025-07-07 19:22:11 +03:00
parent 6a3915a3f5
commit 15c8d8c350
4 changed files with 140 additions and 141 deletions

View file

@ -1,10 +1,17 @@
/// Parse infohash from the source filepath,
/// decode JSON to array on success, return None if the feed is damaged (incomplete)
pub fn infohashes(path: &str) -> anyhow::Result<Option<Vec<String>>> {
/// decode JSON to array on success, return None if the feed file is not reachable
pub fn get(path: &str) -> Option<Vec<String>> {
if path.contains("://") {
todo!("URL sources yet not supported")
}
let s = std::fs::read_to_string(path)?;
let r: Option<Vec<String>> = serde_json::from_str(&s).ok();
Ok(r)
let s = std::fs::read_to_string(path).ok()?; // is updating?
let r: Option<Vec<String>> = serde_json::from_str(&s).ok(); // is incomplete?
r
}
#[test]
fn test() {
assert!(get("test/api/0.json").is_none());
assert!(get("test/api/1.json").is_some());
assert!(get("test/api/2.json").is_none());
}