|
| 1 | +use crate::error::{Error, Result}; |
| 2 | + |
| 3 | +use super::{GenericUpdateInfo, GitHubUpdateInfo, UpdateInfo}; |
| 4 | + |
| 5 | +pub fn parse(s: &str) -> Result<UpdateInfo> { |
| 6 | + let parts: Vec<&str> = s.split('|').collect(); |
| 7 | + |
| 8 | + if parts.is_empty() { |
| 9 | + return Err(Error::InvalidUpdateInfo("Empty update information".into())); |
| 10 | + } |
| 11 | + |
| 12 | + match parts[0] { |
| 13 | + "zsync" => { |
| 14 | + if parts.len() != 2 { |
| 15 | + return Err(Error::InvalidUpdateInfo( |
| 16 | + "zsync format requires exactly 1 parameter: zsync|<url>".into(), |
| 17 | + )); |
| 18 | + } |
| 19 | + Ok(UpdateInfo::Generic(GenericUpdateInfo { |
| 20 | + url: parts[1].into(), |
| 21 | + })) |
| 22 | + } |
| 23 | + "gh-releases-zsync" => { |
| 24 | + if parts.len() != 5 { |
| 25 | + return Err(Error::InvalidUpdateInfo( |
| 26 | + "gh-releases-zsync format requires 4 parameters: gh-releases-zsync|<username>|<repo>|<tag>|<filename>".into(), |
| 27 | + )); |
| 28 | + } |
| 29 | + Ok(UpdateInfo::GitHub(GitHubUpdateInfo::new( |
| 30 | + parts[1].into(), |
| 31 | + parts[2].into(), |
| 32 | + parts[3].into(), |
| 33 | + parts[4].into(), |
| 34 | + ))) |
| 35 | + } |
| 36 | + _ => Err(Error::InvalidUpdateInfo(format!( |
| 37 | + "Unknown update information type: {}", |
| 38 | + parts[0] |
| 39 | + ))), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg(test)] |
| 44 | +mod tests { |
| 45 | + use super::*; |
| 46 | + |
| 47 | + #[test] |
| 48 | + fn parse_generic_zsync() { |
| 49 | + let info = parse("zsync|https://example.com/app.AppImage.zsync").unwrap(); |
| 50 | + match info { |
| 51 | + UpdateInfo::Generic(g) => { |
| 52 | + assert_eq!(g.url, "https://example.com/app.AppImage.zsync"); |
| 53 | + } |
| 54 | + _ => panic!("Expected Generic variant"), |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + #[test] |
| 59 | + fn parse_github_releases() { |
| 60 | + let info = parse("gh-releases-zsync|user|repo|latest|app-*.AppImage").unwrap(); |
| 61 | + match info { |
| 62 | + UpdateInfo::GitHub(g) => { |
| 63 | + assert_eq!(g.username, "user"); |
| 64 | + assert_eq!(g.repo, "repo"); |
| 65 | + assert_eq!(g.tag, "latest"); |
| 66 | + assert_eq!(g.filename, "app-*.AppImage"); |
| 67 | + } |
| 68 | + _ => panic!("Expected GitHub variant"), |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn parse_invalid_type() { |
| 74 | + assert!(parse("invalid|params").is_err()); |
| 75 | + } |
| 76 | + |
| 77 | + #[test] |
| 78 | + fn parse_missing_params() { |
| 79 | + assert!(parse("zsync").is_err()); |
| 80 | + assert!(parse("gh-releases-zsync|user|repo").is_err()); |
| 81 | + } |
| 82 | +} |
0 commit comments