rustyphoenixcodemeta 1.1.0

This project aims to generate a `codemeta.json` file used by zenodo from the `pixi.toml` of the current project
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

use std::fs;
use std::path::{Path, PathBuf};

use serde::Deserialize;
// use std::collections::HashMap;
use toml::{self, de::Error};


///Get info from a package
#[derive(Debug, Deserialize)]
pub struct PackageConfig{
	///Name of the project
	#[serde(rename = "name")]
	p_name: String,
	///Version of the project
	#[serde(rename = "version")]
	p_version: String,
	///Description of the project
	#[serde(rename = "description")]
	p_description: String,
	///Repository of the project
	#[serde(rename = "repository")]
	p_repository: String,
}


///Configuration of a pixi project
#[derive(Debug, Deserialize)]
pub struct PixiConfig{
	///Package configuration of the project
	#[serde(rename = "package")]
	p_package: PackageConfig,
}

impl PixiConfig {
	///Get the name of the project
	/// # Returns
	/// Name of the project
	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()
		}
		// &self.p_package.p_name
	}
	///Get the version of the project
	/// # Returns
	/// Version of the project
	pub fn get_version(&self) -> &String{
		&self.p_package.p_version
	}
	///Get the description of the project
	/// # Returns
	/// Description of the project
	pub fn get_description(&self) -> &String{
		&self.p_package.p_description
	}
	///Get the repository of the project
	/// # Returns
	/// Repository of the project
	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);
		}
	}
	///Get the repository of the project
	/// # Returns
	/// Repository of the project
	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();
		}
	}
	///Get the continuous integration url of the current project
	/// # Returns
	/// Continuous integration url of the current project
	pub fn get_continuous_integration(&self) -> String{
		format!("{}/-/pipelines", self.get_base_repository())
	}
	///Get the issues url of the current project
	/// # Returns
	/// Issues url of the current project
	pub fn get_issue_tracker(&self) -> String{
		format!("{}/-/issues", self.get_base_repository())
	}
	///Get the readme url of the current project
	/// # Returns
	/// Readme url of the current project
	pub fn get_readme(&self) -> String{
		format!("{}-/blob/{}/README.md", self.get_base_repository(), self.p_package.p_version)
	}
	///Get the archive download url of the current project
	/// # Returns
	/// Archive download url of the current project
	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)
	}
}

///Load the project file
/// # Parameters
/// - `filename` : name of the project file (in toml)
/// # Returns
/// Replace parsed configuration or error on fail
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 save of a CodeMeta to JSON
	#[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"));
	}
}