rustyphoenixlecture 1.4.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::path::PathBuf;

use crate::phighlighter::PLocation;
use crate::pcontent::PAbstractLectureBackend;

///Buffer to flush the lecture in files
#[derive(Debug,Default)]
pub struct PLectureBuffer{
	///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 PLectureBuffer {
	///Destructor of the PLectureBuffer
	fn drop(&mut self) {
		//Let's close properly the very last file
		self.close();
	}
}

impl PLectureBuffer {
	///Constructor of the PLectureBuffer
	/// # Paramaters
	/// - `output_filename` : file to be created
	/// # Returns
	/// Initialised PLectureBuffer
	pub fn new(output_filename: &PathBuf) -> Self {
		PLectureBuffer{
			p_absolute_filename: output_filename.clone(),
			p_buffer: Some(BufWriter::new(match File::create(&output_filename) {
				Ok(buffer) => buffer,
				Err(err) => panic!("PLectureBuffer::new : cannot create file {:?}. Error {}", output_filename, err)
			}))
		}
	}
	///Say if the PLectureBuffer is writable or not
	/// # Returns
	/// True if the PLectureBuffer is writable, false otherwise
	pub fn is_writable(&self) -> bool {
		match self.p_buffer {
			Some(_) => true,
			None => false
		}
	}
}

impl PAbstractLectureBackend for PLectureBuffer {
	///Create a new file
	/// # Parameters
	/// - `_output_filename` : name of the file to write
	/// - `_title` : title of the section
	/// - `_location` : location of the current source file where this section was defined
	/// - `_prev_page` : previous page to visit
	/// - `_next_page` : next page to visit
	fn create_file(&mut self, _output_filename: &String, _title: &String,
		_location: &PLocation,
		_prev_page: &String, _next_page: &String)
	{
		//Let's close the previous file is there was one
		self.close();
		panic!("PLectureBuffer::create_file : you are not supposed to call this method");
	}
	///Write text in the current file
	/// # Parameters
	/// - `text` : text to write in the current file
	fn write(&mut self, text: &String){
		match &mut self.p_buffer {
			Some(buffer) => {
				match buffer.write(&text.as_bytes()) {
					Ok(_) => {},
					Err(err) => panic!("PLectureBuffer::write : Error {}.\n\t- in current file '{:?}'\n\t- cannot write string '{}'", err, self.p_absolute_filename, text)
				};
			},
			None => panic!("PLectureBuffer::write : cannot write string '{}' in current file '{:?}'", text, self.p_absolute_filename),
		};
	}
	///Write a formula in html
	/// # Parameters
	/// - `formula` : formula to write in html
	/// # Errors
	/// This function will panic if there is a problem during the formula generation
	fn write_formula(&mut self, formula: &String){
		self.write(&formula);
	}
	///Close the current file
	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!("PLectureBuffer::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;
	}
}