mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
Compare commits
2
Commits
ac5f30151f
...
1038351f4a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1038351f4a | ||
|
|
b748764d79 |
No files matched your search
@@ -2,12 +2,6 @@ use thiserror::Error;
|
|||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("missing front-matter field: id")]
|
|
||||||
MissingID,
|
|
||||||
|
|
||||||
#[error("invalid file id: {}", msg)]
|
|
||||||
InvalidID { msg: String },
|
|
||||||
|
|
||||||
#[error("invalid front-matter: {}", msg)]
|
#[error("invalid front-matter: {}", msg)]
|
||||||
InvalidFrontMatter { msg: String },
|
InvalidFrontMatter { msg: String },
|
||||||
|
|
||||||
|
|||||||
+16
-27
@@ -1,23 +1,20 @@
|
|||||||
mod error;
|
mod error;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
use std::fs;
|
use std::{fs, path::Path, str::FromStr};
|
||||||
use std::path::Path;
|
|
||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::{
|
||||||
use crate::utils::front_matter::{parse_front_matter, split_md_front_matter};
|
error::Error,
|
||||||
|
utils::front_matter::{parse_front_matter, split_md_front_matter},
|
||||||
|
};
|
||||||
|
|
||||||
use base64::{Engine, engine::general_purpose};
|
use base64::{Engine, engine::general_purpose};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Serialize)]
|
#[derive(Default, Debug, Serialize)]
|
||||||
pub struct MdFile {
|
pub struct MdFile {
|
||||||
pub file_id: Uuid,
|
|
||||||
pub filename: String,
|
|
||||||
pub meta: Value,
|
pub meta: Value,
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub hash: String,
|
pub hash: String,
|
||||||
@@ -29,30 +26,22 @@ impl MdFile {
|
|||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
Err(e) => return Err(Error::FSError { msg: e.to_string() }),
|
Err(e) => return Err(Error::FSError { msg: e.to_string() }),
|
||||||
};
|
};
|
||||||
let raw_file_bytes = raw_file.as_bytes().to_vec();
|
Self::from_str(&raw_file)
|
||||||
let (front_matter_str, content) = split_md_front_matter(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) {
|
let front_matter = match parse_front_matter(&front_matter_str) {
|
||||||
Ok(front_matter) => front_matter,
|
Ok(front_matter) => front_matter,
|
||||||
Err(e) => return Err(Error::InvalidFrontMatter { msg: e.to_string() }),
|
Err(e) => return Err(Error::InvalidFrontMatter { msg: e.to_string() }),
|
||||||
};
|
};
|
||||||
let hash = calculate_hash_base64(&raw_file_bytes);
|
let hash = calculate_hash_base64(&bytes);
|
||||||
let file_id = match front_matter["id"].as_str() {
|
|
||||||
Some(id) => match Uuid::from_str(id) {
|
|
||||||
Ok(id) => id,
|
|
||||||
Err(e) => return Err(Error::InvalidID { msg: e.to_string() }),
|
|
||||||
},
|
|
||||||
None => return Err(Error::MissingID),
|
|
||||||
};
|
|
||||||
let file_name = Path::new(file_path)
|
|
||||||
.file_name()
|
|
||||||
.unwrap()
|
|
||||||
.to_str()
|
|
||||||
.unwrap()
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
Ok(MdFile {
|
Ok(MdFile {
|
||||||
file_id,
|
|
||||||
filename: file_name,
|
|
||||||
meta: front_matter,
|
meta: front_matter,
|
||||||
content,
|
content,
|
||||||
hash,
|
hash,
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
|
use std::error::Error;
|
||||||
|
|
||||||
use crate::utils::yaml::yaml_to_serde;
|
use crate::utils::yaml::yaml_to_serde;
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::error::Error;
|
|
||||||
use yaml_rust2::YamlLoader;
|
use yaml_rust2::YamlLoader;
|
||||||
|
|
||||||
pub fn split_md_front_matter(raw_file: String) -> (String, String) {
|
pub fn split_md_front_matter(raw_file: &str) -> (String, String) {
|
||||||
let re = Regex::new(r"(?s)^(---\n.*?\n---\n)(.*)").unwrap();
|
let re = Regex::new(r"(?s)^(---\n.*?\n---\n)(.*)").unwrap();
|
||||||
if let Some(captures) = re.captures(raw_file.as_str()) {
|
if let Some(captures) = re.captures(raw_file) {
|
||||||
let front_matter = captures.get(1).map_or("", |m| m.as_str()).to_string();
|
let front_matter = captures.get(1).map_or("", |m| m.as_str()).to_string();
|
||||||
let main_content = captures.get(2).map_or("", |m| m.as_str()).to_string();
|
let main_content = captures.get(2).map_or("", |m| m.as_str()).to_string();
|
||||||
(front_matter, main_content)
|
(front_matter, main_content)
|
||||||
} else {
|
} else {
|
||||||
(String::new(), raw_file)
|
(String::new(), raw_file.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,8 +45,7 @@ tags: [ "test1", "test2" ]
|
|||||||
|
|
||||||
test body
|
test body
|
||||||
|
|
||||||
"##
|
"##,
|
||||||
.to_string(),
|
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
front_matter,
|
front_matter,
|
||||||
@@ -64,7 +65,7 @@ tags: [ "test1", "test2" ]
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_front_matter_parser() {
|
fn test_front_matter_parser() {
|
||||||
dbg!(parse_front_matter(
|
parse_front_matter(
|
||||||
r##"---
|
r##"---
|
||||||
title: test_title
|
title: test_title
|
||||||
date: 0000-00-00+0000
|
date: 0000-00-00+0000
|
||||||
@@ -75,7 +76,7 @@ summary: summary
|
|||||||
tags: [ "test1", "test2" ]
|
tags: [ "test1", "test2" ]
|
||||||
---
|
---
|
||||||
"##,
|
"##,
|
||||||
))
|
)
|
||||||
.expect("Failed to parse front matter");
|
.expect("Failed to parse front matter");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user