1use std::error::Error as StdError;
2use std::fs::File;
3use std::io::{Read, Write};
4use std::path::Path;
5
6use pulldown_cmark::{html, Options, Parser};
7use tracing::error;
8
9use crate::error::Result;
10
11pub fn write_file(path: &Path, buf: &[u8]) -> Result<()> {
15 if let Some(p) = path.parent() {
16 std::fs::create_dir_all(p)?;
17 }
18 let mut file = File::create(path)?;
19 file.write_all(buf)?;
20 Ok(())
21}
22
23pub fn read_file<P: AsRef<Path>>(path: P, buf: &mut Vec<u8>) -> Result<()> {
25 let mut f = File::open(path)?;
26 f.read_to_end(buf)?;
27 Ok(())
28}
29
30pub fn markdown_to_html(content: &str) -> String {
32 let mut opts = Options::empty();
33 opts.insert(Options::ENABLE_TABLES);
34 opts.insert(Options::ENABLE_FOOTNOTES);
35 opts.insert(Options::ENABLE_TASKLISTS);
36 opts.insert(Options::ENABLE_STRIKETHROUGH);
37 let mut s = String::with_capacity(content.len() * 3 / 2);
38 let p = Parser::new_ext(content, opts);
39 html::push_html(&mut s, p);
40 s
41}
42
43pub(crate) fn log_error_chain(mut e: &dyn StdError) {
45 error!("error: {}", e);
46 while let Some(source) = e.source() {
47 error!("caused by: {}", source);
48 e = source;
49 }
50}