rustyphoenixlecture 1.8.0

This project aims to provide a simple a powerfull lecture compilation to generate html web sites
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

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

use crate::phighlighter::PLocation;
use crate::plectureparser::find_common_start::remove_common_start;

///Describe a ressource file of the lecture
pub struct PRessourceFile{
	///Path of the Ressource file relative to the current lecture file 
	p_relative_ressource_file: PathBuf,
	///Asbolute path of the Ressource file
	p_absolute_ressource_file: PathBuf,
	///Relative output ressource file
	p_relative_output_ressource_file: PathBuf,
	///Absolute output ressource file
	p_absolute_output_ressource_file: PathBuf,
	///Absolute path to the output directory of the current ressource file
	p_absolute_output_ressource_directory: PathBuf,
	
	///Location of the lecture file (file, line and column) (given by the PFileParserIter)
	p_location_lecture_file: PLocation,
	///Asbolute path of the lecture output directory
	p_absolute_lecture_output_dir: PathBuf,
}


impl PRessourceFile{
	///Constructor of the PRessourceFile
	/// # Parameters
	/// - `relative_ressource_file` : path of the Ressource file relative to the current lecture file
	/// - `location_lecture_file` : location of the lecture file (file, line and column) (given by the PFileParserIter)
	/// - `absolute_lecture_output_dir` : asbolute path of the lecture output directory
	/// # Returns
	/// Initialised PRessourceFile
	pub fn new(relative_ressource_file: &PathBuf, location_lecture_file: &PLocation, absolute_lecture_output_dir: &PathBuf) -> Self{
		let mut other = PRessourceFile{
			p_relative_ressource_file: relative_ressource_file.clone(),
			p_absolute_ressource_file: Default::default(),
			p_relative_output_ressource_file: Default::default(),
			p_absolute_output_ressource_file: Default::default(),
			p_absolute_output_ressource_directory: Default::default(),
			
			p_location_lecture_file: location_lecture_file.clone(),
			p_absolute_lecture_output_dir: absolute_lecture_output_dir.clone(),
		};
		other.make_ressource_path();
		return other;
	}
	///Make the missing path of the PRessourceFile
	/// # Errors
	/// This function may panic if the lecture source file PLocation does not exist, but the error should be understandable
	fn make_ressource_path(&mut self){
		// Let's get the absolute path of the input source_file
		self.p_absolute_ressource_file = match self.p_location_lecture_file.get_filename().canonicalize(){
			Ok(absolute_path) => match absolute_path.parent(){
				Some(parent_path) => parent_path.join(&self.p_relative_ressource_file),
				None => panic!("PRessourceFile::make_ressource_path : Cannot get parent of absolute path of {:?}", self.p_location_lecture_file)
			},
			Err(err) => panic!("PRessourceFile::make_ressource_path : Cannot get absolute path of {:?}\n\tError {}", self.p_location_lecture_file, err)
		};
		//Let's get the relative path to the file without the common prefix
		let end_source_file: PathBuf = remove_common_start(&self.p_absolute_ressource_file, &self.p_absolute_lecture_output_dir);
		//Let's save the relative output source file
		self.p_relative_output_ressource_file = PathBuf::from("ressource").join(&end_source_file);
		
		self.p_absolute_output_ressource_file = self.p_absolute_lecture_output_dir.join(&self.p_relative_output_ressource_file);
		//Now, we have to create the output directory
		self.p_absolute_output_ressource_directory = PathBuf::from(self.p_absolute_output_ressource_file.parent().unwrap());
	}
	///Create the output directory for the ressource file
	pub fn create_dir_all(&self){
		match fs::create_dir_all(&self.p_absolute_output_ressource_directory) {
			Ok(_) => {},
			Err(err) => panic!("PRessourceFile::create_dir_all : cannot create output directory {:?}\n\tError {}", self.p_absolute_output_ressource_directory, err)
		};
	}
	///Copy the file to the output directory
	pub fn copy_to_output_dir(&self){
		//First, we check if the output directory does exist
		if !self.p_absolute_output_ressource_directory.exists() {
			//If not we create it
			self.create_dir_all();
		}
		//Then we can copy the file
		match fs::copy(&self.p_absolute_ressource_file, &self.p_absolute_output_ressource_file){
			Ok(_) => {},
			Err(err) => panic!("PRessourceFile::copy_to_output_dir : cannot copy file {:?}\n\tto {:?}\n\tDefined in the lecture at {}\n\tError {}", self.p_absolute_ressource_file, self.p_absolute_output_ressource_file, self.p_location_lecture_file, err)
		};
	}
	///Get the absolute path to the input file
	/// # Returns
	/// Absolute path to the input file
	pub fn get_absolute_input_file(&self) -> &PathBuf{
		&self.p_absolute_ressource_file
	}
	///Get the relative path to the output file
	/// # Returns
	/// Relative path to the output file
	pub fn get_relative_output_file(&self) -> &PathBuf{
		&self.p_relative_output_ressource_file
	}
	///Get the absolute path to the output file
	/// # Returns
	/// Relative path to the output file
	pub fn get_absolute_output_file(&self) -> &PathBuf{
		&self.p_absolute_output_ressource_file
	}
}


#[cfg(test)]
mod tests{
	use super::*;
	
	///Test the PContentText for a Part
	#[test]
	fn test_copy_ressource_file(){
		fs::create_dir_all(&PathBuf::from("target/test_base_lecture")).unwrap();
		
		let location = PLocation::new(&PathBuf::from("tests/index_copy_ressource.md"), 3, 1);
		let ressource_file = PRessourceFile::new(&PathBuf::from("Examples/CopyRessource/main.cpp"), &location, &PathBuf::from("target/test_base_lecture").canonicalize().unwrap());
		//Let's check if the relative output path is OK
		assert_eq!(ressource_file.get_relative_output_file(), &PathBuf::from("ressource/tests/Examples/CopyRessource/main.cpp"));
		
		ressource_file.copy_to_output_dir();
	}
}