mirror of
https://github.com/YGGverse/aquatic-crawler.git
synced 2026-04-02 01:55:28 +00:00
make api source response optional, implement tests
This commit is contained in:
parent
6a3915a3f5
commit
15c8d8c350
4 changed files with 140 additions and 141 deletions
17
src/api.rs
17
src/api.rs
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue