use std::error::Error as StdError;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use pulldown_cmark::{html, Options, Parser};
use tracing::error;
use crate::error::Result;
pub fn write_file(path: &Path, buf: &[u8]) -> Result<()> {
if let Some(p) = path.parent() {
std::fs::create_dir_all(p)?;
}
let mut file = File::create(path)?;
file.write_all(buf)?;
Ok(())
}
pub fn read_file<P: AsRef<Path>>(path: P, buf: &mut Vec<u8>) -> Result<()> {
let mut f = File::open(path)?;
f.read_to_end(buf)?;
Ok(())
}
pub fn markdown_to_html(content: &str) -> String {
let mut opts = Options::empty();
opts.insert(Options::ENABLE_TABLES);
opts.insert(Options::ENABLE_FOOTNOTES);
opts.insert(Options::ENABLE_TASKLISTS);
opts.insert(Options::ENABLE_STRIKETHROUGH);
let mut s = String::with_capacity(content.len() * 3 / 2);
let p = Parser::new_ext(content, opts);
html::push_html(&mut s, p);
s
}
pub(crate) fn log_error_chain(mut e: &dyn StdError) {
error!("error: {}", e);
while let Some(source) = e.source() {
error!("caused by: {}", source);
e = source;
}
}