rustyphoenixlecture 1.5.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::io::prelude::Write;
use std::io::BufWriter;
use std::fs::File;
use std::fs;
use std::path::PathBuf;

///Create the output directory and returns the corresponding output file
/// # Parameters
/// - `output_dir_str` : output directory
/// - `output_file_str` : output file
/// # Returns
/// Corresponding full output file
pub fn phoenix_create_output_test_dir(output_dir_str: &PathBuf, output_file_str: &PathBuf) -> PathBuf {
	let output_dir = PathBuf::from(output_dir_str);
	if !output_dir_str.exists() {
		fs::create_dir_all(&output_dir).unwrap();
	}
	let output_file = output_dir.join(&PathBuf::from(output_file_str));
	return output_file;
}

///Buffer which allows to use String and not the stupid u8 of Rust with its horible conversion
pub struct PStrBuffer{
	///Absolute path we are writing to
	p_absolute_filename: PathBuf,
	///Buffer to be used to write html files
	p_buffer: Option<BufWriter<File>>,
}

impl Drop for PStrBuffer {
	///Destructor of the PStrBuffer
	fn drop(&mut self) {
		//Let's close properly the very last file
		self.close();
	}
}

impl PStrBuffer {
	///Constructor of the PStrBuffer
	/// # Paramaters
	/// - `output_filename` : file to be created
	/// # Returns
	/// Initialised PStrBuffer
	pub fn new(output_filename: &PathBuf) -> Self {
		PStrBuffer{
			p_absolute_filename: output_filename.clone(),
			p_buffer: Some(BufWriter::new(match File::create(&output_filename) {
				Ok(buffer) => buffer,
				Err(err) => panic!("PStrBuffer::new : cannot create file {:?}. Error {}", output_filename, err)
			}))
		}
	}
	///Say if the PStrBuffer is writable or not
	/// # Returns
	/// True if the PStrBuffer is writable, false otherwise
	pub fn is_writable(&self) -> bool {
		match self.p_buffer {
			Some(_) => true,
			None => false
		}
	}
	///Write text in the current file
	/// # Parameters
	/// - `text` : text to write in the current file
	pub fn write(&mut self, text: &String){
		match &mut self.p_buffer {
			Some(buffer) => {
				match buffer.write(&text.as_bytes()) {
					Ok(_) => {},
					Err(err) => panic!("PStrBuffer::write : Error {}.\n\t- in current file '{:?}'\n\t- cannot write string '{}'", err, self.p_absolute_filename, text)
				};
			},
			None => panic!("PStrBuffer::write : cannot write string '{}' in current file '{:?}'", text, self.p_absolute_filename),
		};
	}
	///Close the current file
	pub fn close(&mut self){
		match &mut self.p_buffer {
			Some(buffer) => {
				//Let's finish to write the current file before closing it
				match buffer.flush() {
					Ok(_) => {},
					Err(err) => panic!("PStrBuffer::close : cannot close current buffer. Error {}", err)
				}
			},
			None => {}
		};
		//We finish to close the buffer, so we cannot use is any more and wait for the next one
		self.p_buffer = None;
	}
}

#[cfg(test)]
mod tests{
	use super::*;
	
	///Test the creation of the PStrBuffer
	#[test]
	fn test_pstrbuffer(){
		let output_file = phoenix_create_output_test_dir(&PathBuf::from("target/test_pstrbuffer"), &PathBuf::from("pstrbuffer.txt"));
		
		let mut buffer = PStrBuffer::new(&output_file);
		assert!(buffer.is_writable());
		buffer.write(&String::from("Just some text in a file\n"));
		
		buffer.close();
		assert!(!buffer.is_writable());
	}
}