use std::error::Error; use serde::{Deserialize, Serialize}; use super::repository::{Repository, RepositoryImpl}; #[derive(Serialize, Deserialize, PartialEq, Debug, Clone, Default)] pub struct Chart { // A name of the helm chart pub name: String, // A reference to repository by name pub repository: String, pub mirrors: Vec, // Versions to be mirrored #[serde(default = "latest")] pub version: String, } pub(crate) fn latest() -> String { "latest".to_string() } impl Chart { pub fn find_repo( &self, repositories: Vec, ) -> Result, Box> { for repository in repositories { if repository.name == self.repository { if let Some(helm) = repository.helm { return Ok(Box::from(helm)); } else if let Some(git) = repository.git { return Ok(Box::from(git)); } else { return Err(Box::from("unsupported kind of repository")); } } } //let err = error!("repo {} is not found in the repo list", self.repository); let error_msg = format!("repo {} is not found in the repo list", self.repository); Err(Box::from(error_msg)) } } #[cfg(test)] mod tests { use std::error::Error; use crate::helm::{ chart::latest, helm_repository::HelmRepo, repository::{Repository, RepositoryImpl}, }; use super::Chart; #[test] fn test_find_repo() -> Result<(), Box> { let chart = Chart { name: "test".to_string(), repository: "test".to_string(), mirrors: vec!["test".to_string()], version: latest(), }; let repo = Repository { name: "test".to_string(), helm: Some(HelmRepo { url: "test.rocks".to_string(), }), }; let res = chart.find_repo(vec![repo])?; assert_eq!(res.get_url(), "test.rocks".to_string()); Ok(()) } }