Skip to content

Commit

Permalink
test and update quick_xml update bucket deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Jul 18, 2024
1 parent 9e081d6 commit 2ff31b3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ url = "2"
serde_repr = "0.1"
webpki-roots = "0.26"
ar = "0.9"
quick-xml = { version = "0.31", features = ["serialize"] }
quick-xml = { version = "0.36", features = ["serialize"] }
semver = "1"
futures-util = "0.3"
self-replace = "1"
Expand Down
42 changes: 35 additions & 7 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ async fn latest_release(client: &Client) -> Result<Release, UpdateError> {
.contents
.into_iter()
.flat_map(Content::release)
.max()
.filter(|release| release.key.contains(effective_target()))
.max_by_key(|release| release.version.clone())
.ok_or(UpdateError::NoReleases)
}

Expand All @@ -93,18 +94,15 @@ struct ListBucket {
contents: Vec<Content>,
}

#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct Content {
key: String,
}

impl Content {
fn release(self) -> Option<Release> {
let (version, filename) = self.key.split_once('/')?;
if !filename.contains(effective_target()) {
return None;
}
let (version, _filename) = self.key.split_once('/')?;
let version = version.strip_prefix('v')?;
Some(Release {
version: version.parse().ok()?,
Expand All @@ -113,7 +111,7 @@ impl Content {
}
}

#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)]
#[derive(Debug, Clone)]
struct Release {
version: Version,
key: String,
Expand Down Expand Up @@ -170,3 +168,33 @@ impl From<Elapsed> for UpdateError {
UpdateError::Timeout
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_list_bucket() {
let sample = r#"
<?xml version="1.0" encoding="UTF-8"?>
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<Name>fishnet-releases</Name>
<Prefix/>
<KeyCount>74</KeyCount>
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
<Contents>
<Key>v2.6.10/fishnet-v2.6.10-aarch64-apple-darwin</Key>
<LastModified>2023-05-01T16:27:52.000Z</LastModified>
<ETag>"f7ed5e695e421adbf153ee35a4d46fca-6"</ETag>
<Size>30471464</Size>
<StorageClass>STANDARD</StorageClass>
</Contents>
</ListBucketResult>"#;

let bucket: ListBucket = quick_xml::de::from_str(sample).unwrap();
let release = bucket.contents[0].clone().release().unwrap();
assert_eq!(release.version, Version::new(2, 6, 10));
assert_eq!(release.key, "v2.6.10/fishnet-v2.6.10-aarch64-apple-darwin");
}
}

0 comments on commit 2ff31b3

Please sign in to comment.