better error handlers in rust and some UI improvement
This commit is contained in:
@@ -1,37 +1,82 @@
|
||||
use std::{env, fs, process};
|
||||
use pulldown_cmark::{Parser, Options, html};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn main() {
|
||||
// Get the file path from CLI args
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() != 2 {
|
||||
eprintln!("Usage: {} <markdown_file>", args[0]);
|
||||
process::exit(1);
|
||||
#[derive(Debug)]
|
||||
enum MarkdownError {
|
||||
IoError(std::io::Error),
|
||||
InvalidArgs,
|
||||
InvalidFileType,
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for MarkdownError {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
MarkdownError::IoError(err)
|
||||
}
|
||||
}
|
||||
|
||||
let path = &args[1];
|
||||
|
||||
// Read file contents
|
||||
let markdown = match fs::read_to_string(path) {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
eprintln!("Error reading {}: {}", path, e);
|
||||
process::exit(1);
|
||||
impl std::fmt::Display for MarkdownError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
MarkdownError::IoError(e) => write!(f, "IO error: {}", e),
|
||||
MarkdownError::InvalidArgs => write!(f, "Invalid arguments provided"),
|
||||
MarkdownError::InvalidFileType => write!(f, "File extension should be .md or .markdown"),
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Configure parser with GitHub-flavored options
|
||||
fn markdown_to_html(markdown: &str, body_top_content: &str) -> String {
|
||||
let mut options = Options::empty();
|
||||
options.insert(Options::ENABLE_TABLES);
|
||||
options.insert(Options::ENABLE_FOOTNOTES);
|
||||
options.insert(Options::ENABLE_STRIKETHROUGH);
|
||||
options.insert(Options::ENABLE_TASKLISTS);
|
||||
|
||||
// Parse and render to HTML
|
||||
let parser = Parser::new_ext(&markdown, options);
|
||||
let parser = Parser::new_ext(markdown, options);
|
||||
let mut html_output = String::new();
|
||||
html::push_html(&mut html_output, parser);
|
||||
|
||||
// Print only the body content (no <html>/<head>)
|
||||
println!("{} <!-- this was generated from rust with pulldown_cmark ; REASON: Large File -->", html_output);
|
||||
|
||||
// If body_top_content is not empty, wrap the output
|
||||
if !body_top_content.trim().is_empty() {
|
||||
format!("{}\n{}", body_top_content, html_output)
|
||||
} else {
|
||||
html_output
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_file_type(path: &PathBuf) -> Result<(), MarkdownError> {
|
||||
if let Some(ext) = path.extension() {
|
||||
if ext == "md" || ext == "markdown" {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(MarkdownError::InvalidFileType)
|
||||
}
|
||||
} else {
|
||||
Err(MarkdownError::InvalidFileType)
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> Result<String, MarkdownError> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() != 2 {
|
||||
return Err(MarkdownError::InvalidArgs);
|
||||
}
|
||||
|
||||
let path = PathBuf::from(&args[1]);
|
||||
validate_file_type(&path)?;
|
||||
let markdown = fs::read_to_string(&path)?;
|
||||
let top_content = "<!-- This was automatically parsed with fastmd (Rust Markdown Parser) -->";
|
||||
Ok(markdown_to_html(&markdown, top_content))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match run() {
|
||||
Ok(html_output) => {
|
||||
println!("{}", html_output);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user