markdown_parser/fs.rs
1use crate::parser::Markdown;
2use crate::{error::Error, parser::parse};
3
4/// read content from a md file
5/// and return parse result
6pub fn read_file<P>(path: P) -> Result<Markdown, Error>
7where
8 P: AsRef<std::path::Path>,
9{
10 parse(std::fs::read_to_string(path)?.as_str())
11}
12
13/// write a Markdown struct into a file
14pub fn write_file<P>(markdown: &Markdown, path: P) -> Result<(), Error>
15where
16 P: AsRef<std::path::Path>,
17{
18 use std::fs::File;
19 use std::io::prelude::Write;
20
21 let mut file = File::create(path)?;
22 file.write_all(&markdown.bytes())?;
23
24 Ok(())
25}