#![allow(dead_code, unused_variables)]
mod rustla_options;
pub mod parser;
use parser::Parser;
use parser::state_machine::State;
pub mod doctree;
use doctree::DocTree;
pub mod common;
mod utf8_to_latex;
use std::io::BufRead;
pub fn run() -> Result<(), MainError> {
copyright();
let args: Vec<String> = std::env::args().collect();
let args_len = args.len();
let rustla_options = crate::rustla_options::ruSTLaOptions::new(&args);
let mut src_lines = Vec::new();
let path: std::path::PathBuf = if let Some(arg) = args.last() {
if args_len == 1 {
let stdin = std::io::stdin();
for line in stdin.lock().lines() {
match line {
Ok(line) => src_lines.push(line),
Err(e) => {
return Err(MainError::InputError(format!(
"Error when reading stdin: {}",
e
)))
}
}
}
std::path::PathBuf::new()
} else if let Ok(pathbuf) = std::fs::canonicalize(arg) {
let line_iter = match crate::common::read_path_lines(&pathbuf) {
Ok(lines) => lines,
Err(e) => {
return Err(MainError::PathError(format!(
"Could not split file into lines: {}",
e
)))
}
};
for line in line_iter {
match line {
Ok(line) => src_lines.push(line),
Err(e) => {
return Err(MainError::InputError(String::from(
"Could not construct a line vector from input...",
)))
}
}
}
pathbuf
} else {
let stdin = std::io::stdin();
for line in stdin.lock().lines() {
match line {
Ok(line) => src_lines.push(line),
Err(e) => {
return Err(MainError::InputError(format!(
"Error when reading stdin: {}",
e
)))
}
}
}
std::path::PathBuf::new()
}
} else {
unreachable!("No arguments, not even the program itself? Computer says no...")
};
let mut doctree = DocTree::new(path);
let mut parser = Parser::new(src_lines, doctree, 0, 0, State::Body, 0);
use common::ParsingResult;
doctree = match parser.parse() {
ParsingResult::EOF { doctree, .. } | ParsingResult::EmptyStateStack { doctree, .. } => {
doctree
}
ParsingResult::Failure { message, doctree } => {
eprintln!("Parsing error: {}", message);
doctree
}
};
doctree = doctree.perform_restructuredtext_transforms();
doctree.write_to_larst(&rustla_options);
Ok(())
}
fn copyright() {
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const AUTHOR_NAME: &'static str = env!("AUTHOR_NAME");
const AUTHOR_YEAR: &'static str = env!("AUTHOR_YEAR");
eprintln!("\nThis is ruSTLa, version {}", VERSION);
eprintln!("Copyright © {} {}\n", AUTHOR_YEAR, AUTHOR_NAME);
}
fn usage() {
println!("Instructions");
println!("============");
println!("In order to transpile a document,");
println!("point ruSTLa to an rST file with");
println!("\n $ rustla path/to/file.rst\n");
println!("Capabilities to transpile an entire");
println!("toctree will be added later.");
}
#[derive(Debug)]
pub enum MainError {
PathError(String),
InputError(String),
ParseError(String),
PrintError(String),
ArgumentError(String),
}