use std::fs;
use std::path::{Path, PathBuf};
use serde::Deserialize;
use toml::{self, de::Error};
#[derive(Debug, Deserialize)]
pub struct PackageConfig{
#[serde(rename = "name")]
p_name: String,
#[serde(rename = "version")]
p_version: String,
#[serde(rename = "description")]
p_description: String,
#[serde(rename = "repository")]
p_repository: String,
}
#[derive(Debug, Deserialize)]
pub struct PixiConfig{
#[serde(rename = "package")]
p_package: PackageConfig,
}
impl PixiConfig {
pub fn get_name(&self) -> String{
match PathBuf::from(self.get_base_repository().clone()).file_name(){
Some(p) => String::from(p.to_str().unwrap()),
None => self.p_package.p_name.clone()
}
}
pub fn get_version(&self) -> &String{
&self.p_package.p_version
}
pub fn get_description(&self) -> &String{
&self.p_package.p_description
}
pub fn get_repository(&self) -> String{
if self.p_package.p_repository.ends_with(".git") {
return self.p_package.p_repository.clone();
}else{
return format!("{}.git", self.p_package.p_repository);
}
}
pub fn get_base_repository(&self) -> String{
if self.p_package.p_repository.ends_with(".git") {
return self.p_package.p_repository.replace(".git", "");
}else{
return self.p_package.p_repository.clone();
}
}
pub fn get_continuous_integration(&self) -> String{
format!("{}/-/pipelines", self.get_base_repository())
}
pub fn get_issue_tracker(&self) -> String{
format!("{}/-/issues", self.get_base_repository())
}
pub fn get_readme(&self) -> String{
format!("{}-/blob/{}/README.md", self.get_base_repository(), self.p_package.p_version)
}
pub fn get_download_url(&self) -> String{
let urlname: String = match PathBuf::from(self.get_base_repository().clone()).file_name(){
Some(p) => String::from(p.to_str().unwrap()),
None => self.p_package.p_name.clone()
};
format!("{}/-/archive/{}/{}-{}.tar.gz", self.get_base_repository(), self.p_package.p_version, urlname, self.p_package.p_version)
}
}
pub fn load_pixi_project(filename: &Path) -> Result<PixiConfig, Error>{
let simu: PixiConfig = match fs::read_to_string(filename) {
Ok(str) => match toml::from_str(&str){
Ok(value) => value,
Err(error) => panic!("load_pixi_project : cannot parse file '{:?}', parsing error {}", filename, error)
},
Err(err) => panic!("load_pixi_project : cannot read file '{:?}', error: {}", filename, err)
};
println!("load_pixi_project : '{:?}'", filename);
Ok(simu)
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
#[test]
fn test_load_pixi_config(){
let filename = PathBuf::from("tests/test_config_pixi.toml");
let config: PixiConfig = load_pixi_project(&filename).unwrap();
assert_eq!(config.get_name().to_owned(), String::from("RustyPhoenixPng"));
assert_eq!(config.get_version().to_owned(), String::from("1.0.1"));
assert_eq!(config.get_description().to_owned(), String::from("This project eases manipulation of png image inside Phoenix ecosystem"));
assert_eq!(config.get_repository(), String::from("https://gitlab.in2p3.fr/CTA-LAPP/PHOENIX_LIBS2/static-site-generator/RustyPhoenixPng.git"));
}
}