1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/***************************************
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;
}
}