feat(markdown) add from_str

This commit is contained in:
lumither committed 2025-03-26 19:52:19 -07:00
1 parent b748764d79
commit 1038351f4a
2 files changed
+26 -25

No files matched your search

+17 -17
View File
@@ -1,20 +1,20 @@
mod error;
mod utils;
use std::fs;
use std::path::Path;
use std::{fs, path::Path, str::FromStr};
use crate::error::Error;
use crate::utils::front_matter::{parse_front_matter, split_md_front_matter};
use crate::{
error::Error,
utils::front_matter::{parse_front_matter, split_md_front_matter},
};
use base64::{engine::general_purpose, Engine};
use base64::{Engine, engine::general_purpose};
use serde::Serialize;
use serde_json::Value;
use sha2::{Digest, Sha256};
#[derive(Default, Debug, Serialize)]
pub struct MdFile {
pub filename: String,
pub meta: Value,
pub content: String,
pub hash: String,
@@ -26,22 +26,22 @@ impl MdFile {
Ok(file) => file,
Err(e) => return Err(Error::FSError { msg: e.to_string() }),
};
let raw_file_bytes = raw_file.as_bytes().to_vec();
let (front_matter_str, content) = split_md_front_matter(raw_file);
Self::from_str(&raw_file)
}
}
impl FromStr for MdFile {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let bytes = s.as_bytes().to_vec();
let (front_matter_str, content) = split_md_front_matter(s);
let front_matter = match parse_front_matter(&front_matter_str) {
Ok(front_matter) => front_matter,
Err(e) => return Err(Error::InvalidFrontMatter { msg: e.to_string() }),
};
let hash = calculate_hash_base64(&raw_file_bytes);
let file_name = Path::new(file_path)
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string();
let hash = calculate_hash_base64(&bytes);
Ok(MdFile {
filename: file_name,
meta: front_matter,
content,
hash,