Skip to content

Commit

Permalink
feat(hub): published at method for PackageMeta (#4201)
Browse files Browse the repository at this point in the history
* feat(hub): `published at` method for `PackageMeta`

* feat: move `publisted_at` to `PackageMetaExt`
  • Loading branch information
EstebanBorai authored Oct 2, 2024
1 parent 062cbf8 commit 134b057
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/fluvio-hub-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ connector-cmds = [
anyhow = { workspace = true }
async-trait = { workspace = true }
cargo_toml = { workspace = true }
chrono = { workspace = true }
const_format = { workspace = true }
dirs = { workspace = true }
ed25519-dalek = { version = "2.1", features = ["serde", "rand_core"] }
Expand Down
68 changes: 66 additions & 2 deletions crates/fluvio-hub-util/src/package_meta_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::fs;
use std::io::Read;
use std::path::Path;

use chrono::{DateTime, Utc};
use tracing::debug;

use fluvio_hub_protocol::{PackageMeta, HubError};
use fluvio_hub_protocol::constants::HUB_PACKAGE_META;
use fluvio_hub_protocol::{HubError, PackageMeta, PkgTag};
use fluvio_hub_protocol::constants::{HUB_PACKAGE_META, PKG_TAG_META_PUBLISHED_AT};
use fluvio_hub_protocol::validate_allowedchars;

use crate::package_get_topfile;
Expand All @@ -17,6 +18,7 @@ pub trait PackageMetaExt {
fn manifest_paths<P: AsRef<Path>>(&self, pkgpath_in: P) -> Result<Vec<String>>;
fn write<P: AsRef<Path>>(&self, pmetapath: P) -> Result<()>;
fn update_from_cargo_toml<P: AsRef<Path>>(&mut self, fpath: P) -> Result<()>;
fn published_at(&self) -> Result<DateTime<Utc>>;
}

impl PackageMetaExt for PackageMeta {
Expand Down Expand Up @@ -56,6 +58,23 @@ impl PackageMetaExt for PackageMeta {

Ok(())
}

fn published_at(&self) -> Result<DateTime<Utc>> {
let published_at_pkg_tag = self
.tags
.as_ref()
.ok_or(HubError::General(String::from("No tags available")))?
.iter()
.find(|PkgTag { tag, .. }| tag == PKG_TAG_META_PUBLISHED_AT)
.ok_or(HubError::General(format!(
"Missing {PKG_TAG_META_PUBLISHED_AT} tag"
)))?;

let publisted_at = DateTime::parse_from_rfc2822(published_at_pkg_tag.value.as_str())
.map_err(|err| HubError::General(format!("Failed to parse publish date. {}", err)))?;

Ok(publisted_at.to_utc())
}
}

pub fn packagename_validate(pkgname: &str) -> Result<()> {
Expand Down Expand Up @@ -144,6 +163,8 @@ pub fn package_meta_relative_path<P: AsRef<Path>, T: AsRef<Path>>(

#[cfg(test)]
mod tests {
use fluvio_hub_protocol::PkgTag;

use super::*;

#[test]
Expand Down Expand Up @@ -458,4 +479,47 @@ mod tests {
}
}
}

#[test]
fn inf_meta_published_at_success() {
let pm = PackageMeta {
group: "infinyon".into(),
name: "example".into(),
version: "0.0.1".into(),
manifest: ["module.wasm".into()].to_vec(),
tags: Some(vec![PkgTag {
tag: PKG_TAG_META_PUBLISHED_AT.to_string(),
value: "Tue, 22 Nov 2022 21:24:11 GMT".to_string(),
}]),
..Default::default()
};
let published_at = pm.published_at();

assert!(published_at.is_ok());

let published_at = published_at.unwrap();
assert_eq!(published_at.to_string(), "2022-11-22 21:24:11 UTC");
}

#[test]
fn inf_meta_published_at_missing_tag() {
let pm = PackageMeta {
group: "infinyon".into(),
name: "example".into(),
version: "0.0.1".into(),
manifest: ["module.wasm".into()].to_vec(),
tags: Some(vec![PkgTag {
tag: String::from("foo"),
value: String::from("bar"),
}]),
..Default::default()
};
let published_at = pm.published_at();

assert!(published_at.is_err());
assert_eq!(
published_at.err().unwrap().to_string(),
format!("General Error: Missing inf::meta::published_at tag")
);
}
}

0 comments on commit 134b057

Please sign in to comment.