diff --git a/src/stelae/stele.rs b/src/stelae/stele.rs index b131fba..ef24533 100644 --- a/src/stelae/stele.rs +++ b/src/stelae/stele.rs @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf}; -use super::types::repositories::Repository; +use super::types::{repositories::Repository, targets_metadata::TargetsMetadata}; use crate::{ stelae::types::{dependencies::Dependencies, repositories::Repositories}, utils::git::Repo, @@ -87,16 +87,39 @@ impl Stele { /// # Errors /// Will error if unable to find or parse repositories file at `targets/repositories.json` pub fn get_repositories(&mut self) -> anyhow::Result> { - let blob = self + let Ok(blob) = self .auth_repo - .get_bytes_at_path("HEAD", "targets/repositories.json"); - if let Ok(repositories_blob) = blob { - let repositories_str = String::from_utf8(repositories_blob)?; - let repositories: Repositories = serde_json::from_str(&repositories_str)?; - self.repositories = Some(repositories.clone()); - return Ok(Some(repositories)); - } - Ok(None) + .get_bytes_at_path("HEAD", "targets/repositories.json") + else { + return Ok(None); + }; + let repositories_str = String::from_utf8(blob)?; + let repositories: Repositories = serde_json::from_str(&repositories_str)?; + self.repositories = Some(repositories.clone()); + Ok(Some(repositories)) + } + + /// Get Stele's targets metadata file at a specific committish and filename. + /// + /// # Arguments + /// * `committish` - The committish to look for the targets metadata file. + /// * `path` - The path to the targets metadata file. + /// + /// # Returns + /// Returns the targets metadata file if found, or None if not found. + pub fn get_targets_metadata_at_commit_and_filename( + &self, + committish: &str, + filename: &str, + ) -> anyhow::Result> { + let org = &self.auth_repo.org; + let file_path = format!("targets/{org}/{filename}"); + let Ok(blob) = self.auth_repo.get_bytes_at_path(committish, &file_path) else { + return Ok(None); + }; + let targets_metadata_str = String::from_utf8(blob)?; + let targets_metadata: TargetsMetadata = serde_json::from_str(&targets_metadata_str)?; + Ok(Some(targets_metadata)) } /// Get Stele's qualified name. diff --git a/src/stelae/types/mod.rs b/src/stelae/types/mod.rs index c52d4d8..7269c0c 100644 --- a/src/stelae/types/mod.rs +++ b/src/stelae/types/mod.rs @@ -1,4 +1,5 @@ -//! The Types module contains data models for Stelae. +//! The `types` module contains data models for stelae. pub mod dependencies; pub mod repositories; +pub mod targets_metadata; diff --git a/src/stelae/types/targets_metadata.rs b/src/stelae/types/targets_metadata.rs new file mode 100644 index 0000000..6e0519c --- /dev/null +++ b/src/stelae/types/targets_metadata.rs @@ -0,0 +1,39 @@ +//! Targets metadata types. +//! Represents the metadata in the `targets//` file. +use serde::Deserialize; +use serde_derive::Serialize; + +/// An entry in the `targets//` file. +/// +/// Example: +/// ```rust +/// use stelae::stelae::types::targets_metadata::TargetsMetadata; +/// use serde_json::json; +/// +/// let data = r#" +/// { +/// "branch": "publication/2024-09-16", +/// "build-date": "2024-09-16", +/// "commit": "1b7334f58f41a53d6e4d9fc11fba6793cb22eb36", +/// "codified-date": "2024-10-03" +/// } +/// "#; +/// let targets_metadata: TargetsMetadata = serde_json::from_str(data).unwrap(); +/// assert_eq!(targets_metadata.branch, "publication/2024-09-16"); +/// assert_eq!(targets_metadata.build_date.unwrap(), "2024-09-16"); +/// assert_eq!(targets_metadata.commit, "1b7334f58f41a53d6e4d9fc11fba6793cb22eb36"); +/// assert_eq!(targets_metadata.codified_date.unwrap(), "2024-10-03"); +/// ``` +#[derive(Serialize, Deserialize, Debug)] +pub struct TargetsMetadata { + /// A git branch name. + pub branch: String, + /// The date the build was created. + #[serde(rename = "build-date")] + pub build_date: Option, + /// The commit hash. + pub commit: String, + /// The date the code was codified. + #[serde(rename = "codified-date")] + pub codified_date: Option, +}