1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use crate::parser::Markdown;
use crate::{error::Error, parser::parse};

/// read content from a md file
/// and return parse result
pub fn read_file<P>(path: P) -> Result<Markdown, Error>
where
    P: AsRef<std::path::Path>,
{
    parse(std::fs::read_to_string(path)?.as_str())
}

/// write a Markdown struct into a file
pub fn write_file<P>(markdown: &Markdown, path: P) -> Result<(), Error>
where
    P: AsRef<std::path::Path>,
{
    use std::fs::File;
    use std::io::prelude::Write;

    let mut file = File::create(path)?;
    file.write_all(&markdown.bytes())?;

    Ok(())
}