Add error handling for section nesting problems

pull/1/head
trivernis 5 years ago
parent 22b6c9cc00
commit e9ec656740

@ -20,6 +20,7 @@ pub enum Inline {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Document { pub struct Document {
pub(crate) elements: Vec<Block>, pub(crate) elements: Vec<Block>,
pub(crate) is_root: bool,
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -168,9 +169,10 @@ pub struct Image {
// implementations // implementations
impl Document { impl Document {
pub fn new() -> Self { pub fn new(is_root: bool) -> Self {
Self { Self {
elements: Vec::new(), elements: Vec::new(),
is_root,
} }
} }

@ -49,7 +49,14 @@ impl ToHtml for Document {
.elements .elements
.iter() .iter()
.fold("".to_string(), |a, b| format!("{}{}", a, b.to_html())); .fold("".to_string(), |a, b| format!("{}{}", a, b.to_html()));
format!("<html><body>{}</body></html>", inner) if self.is_root {
format!("<html><body>{}</body></html>", inner)
} else {
format!(
"<div class='documentImport' document-import=true>{}</div>",
inner
)
}
} }
} }

@ -27,7 +27,13 @@ pub struct ParseError {
} }
impl Display for ParseError { impl Display for ParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "parse error at index {}", self.index) write!(
f,
"{} Parse Error at index {}{}",
Fg(color::Red),
self.index,
style::Reset
)
} }
} }
impl Error for ParseError {} impl Error for ParseError {}
@ -46,6 +52,7 @@ pub struct Parser {
path: Option<String>, path: Option<String>,
paths: Arc<Mutex<Vec<String>>>, paths: Arc<Mutex<Vec<String>>>,
wg: WaitGroup, wg: WaitGroup,
is_child: bool,
} }
impl Parser { impl Parser {
@ -55,13 +62,18 @@ impl Parser {
} }
pub fn new(text: String, path: Option<String>) -> Self { pub fn new(text: String, path: Option<String>) -> Self {
Parser::new_as_child(text, path, Arc::new(Mutex::new(Vec::new()))) Parser::create(text, path, Arc::new(Mutex::new(Vec::new())), false)
} }
pub fn new_as_child( pub fn new_as_child(text: String, path: String, paths: Arc<Mutex<Vec<String>>>) -> Self {
Self::create(text, Some(path), paths, true)
}
fn create(
text: String, text: String,
path: Option<String>, path: Option<String>,
paths: Arc<Mutex<Vec<String>>>, paths: Arc<Mutex<Vec<String>>>,
is_child: bool,
) -> Self { ) -> Self {
let mut text: Vec<char> = text.chars().collect(); let mut text: Vec<char> = text.chars().collect();
text.append(&mut vec!['\n', ' ', '\n']); // push space and newline of eof. it fixes stuff and I don't know why. text.append(&mut vec!['\n', ' ', '\n']); // push space and newline of eof. it fixes stuff and I don't know why.
@ -82,6 +94,7 @@ impl Parser {
path, path,
paths, paths,
wg: WaitGroup::new(), wg: WaitGroup::new(),
is_child,
} }
} }
@ -197,6 +210,7 @@ impl Parser {
Ok(()) Ok(())
} }
/// transform an import path to be relative to the current parsers file
fn transform_path(&mut self, path: String) -> String { fn transform_path(&mut self, path: String) -> String {
let mut path = path; let mut path = path;
let first_path_info = Path::new(&path); let first_path_info = Path::new(&path);
@ -249,7 +263,7 @@ impl Parser {
let _ = thread::spawn(move || { let _ = thread::spawn(move || {
let text = read_to_string(path.clone()).unwrap(); let text = read_to_string(path.clone()).unwrap();
let mut parser = Parser::new_as_child(text.to_string(), Some(path), paths); let mut parser = Parser::new_as_child(text.to_string(), path, paths);
let document = parser.parse(); let document = parser.parse();
anchor_clone.lock().unwrap().set_document(document); anchor_clone.lock().unwrap().set_document(document);
@ -261,10 +275,18 @@ impl Parser {
/// parses the given text into a document /// parses the given text into a document
pub fn parse(&mut self) -> Document { pub fn parse(&mut self) -> Document {
let mut document = Document::new(); let mut document = Document::new(!self.is_child);
while self.index < self.text.len() { while self.index < self.text.len() {
if let Ok(token) = self.parse_block() { match self.parse_block() {
document.add_element(token); Ok(block) => document.add_element(block),
Err(err) => {
if let Some(path) = &self.path {
println!("{} Error in File {}: {}", Fg(color::Red), path, err);
} else {
println!("{}", err);
}
break;
}
} }
} }

Loading…
Cancel
Save