mirror of
https://github.com/Lumither/nocturne.git
synced 2026-10-17 10:26:21 +00:00
ref, feat(scheduler) global project refactor, add scheduler
This commit is contained in:
47 files changed
+796
-1037
No files matched your search
@@ -4,3 +4,11 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
uuid = { version = "1.10.0", features = ["v4", "fast-rng", "serde"] }
|
||||
chrono = { version = "0.4.38", features = ["serde", "alloc"] }
|
||||
serde = { version = "1.0.209", features = ["derive"] }
|
||||
serde_json = "1.0.128"
|
||||
regex = "1.10.4"
|
||||
yaml-rust2 = "0.8.1"
|
||||
sha2 = "0.10.8"
|
||||
base64 = "0.22.1"
|
||||
@@ -1,14 +1,55 @@
|
||||
pub fn add(left: u64, right: u64) -> u64 {
|
||||
left + right
|
||||
mod utils;
|
||||
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
use serde::Serialize;
|
||||
use serde_json::Value;
|
||||
use sha2::{Digest, Sha256};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::utils::front_matter::{parse_front_matter, split_md_front_matter};
|
||||
|
||||
#[derive(Default, Debug, Serialize)]
|
||||
pub struct MdFile {
|
||||
file_id: Uuid,
|
||||
file_name: String,
|
||||
meta: Value,
|
||||
content: String,
|
||||
hash: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
impl MdFile {
|
||||
pub async fn from_file(file_path: &str) -> Result<MdFile, Box<dyn Error>> {
|
||||
let raw_file = fs::read_to_string(file_path)?;
|
||||
let raw_file_bytes = raw_file.as_bytes().to_vec();
|
||||
let (front_matter_str, content) = split_md_front_matter(raw_file);
|
||||
let front_matter = parse_front_matter(&front_matter_str)?;
|
||||
let hash = calculate_hash_base64(&raw_file_bytes);
|
||||
let file_id = Uuid::from_str(front_matter["id"].to_string().as_str())?;
|
||||
let file_name = Path::new(file_path)
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.to_string();
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
let result = add(2, 2);
|
||||
assert_eq!(result, 4);
|
||||
Ok(MdFile {
|
||||
file_id,
|
||||
file_name,
|
||||
meta: front_matter,
|
||||
content,
|
||||
hash,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn calculate_hash_base64(bytes: &Vec<u8>) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(bytes);
|
||||
let hash = hasher.finalize().to_vec();
|
||||
general_purpose::STANDARD.encode(hash)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
use crate::utils::yaml::yaml_to_serde;
|
||||
use regex::Regex;
|
||||
use serde_json::Value;
|
||||
use std::error::Error;
|
||||
use yaml_rust2::YamlLoader;
|
||||
|
||||
pub fn split_md_front_matter(raw_file: String) -> (String, String) {
|
||||
let re = Regex::new(r"(?s)^(---\n.*?\n---\n)(.*)").unwrap();
|
||||
if let Some(captures) = re.captures(raw_file.as_str()) {
|
||||
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();
|
||||
(front_matter, main_content)
|
||||
} else {
|
||||
(String::new(), raw_file)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_front_matter(front_matter: &str) -> Result<Value, Box<dyn Error>> {
|
||||
let parsed_f_matter = YamlLoader::load_from_str(front_matter)?[0].clone();
|
||||
Ok(Value::from(yaml_to_serde(&parsed_f_matter)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils::front_matter::{parse_front_matter, split_md_front_matter};
|
||||
|
||||
#[test]
|
||||
fn front_matter_to_string() {
|
||||
let (front_matter, main_content) = split_md_front_matter(
|
||||
r##"---
|
||||
title: test_title
|
||||
date: 0000-00-00+0000
|
||||
category: test
|
||||
id: bf03afd8-61b4-4e78-a34a-8b3ee39adf16
|
||||
sub_title: sub_title
|
||||
summary: summary
|
||||
tags: [ "test1", "test2" ]
|
||||
---
|
||||
|
||||
test body
|
||||
|
||||
"##
|
||||
.to_string(),
|
||||
);
|
||||
assert_eq!(
|
||||
front_matter,
|
||||
r##"---
|
||||
title: test_title
|
||||
date: 0000-00-00+0000
|
||||
category: test
|
||||
id: bf03afd8-61b4-4e78-a34a-8b3ee39adf16
|
||||
sub_title: sub_title
|
||||
summary: summary
|
||||
tags: [ "test1", "test2" ]
|
||||
---
|
||||
"##
|
||||
);
|
||||
assert_eq!(main_content, "\ntest body\n\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_front_matter_parser() {
|
||||
parse_front_matter(
|
||||
r##"---
|
||||
title: test_title
|
||||
date: 0000-00-00+0000
|
||||
category: test
|
||||
id: bf03afd8-61b4-4e78-a34a-8b3ee39adf16
|
||||
sub_title: sub_title
|
||||
summary: summary
|
||||
tags: [ "test1", "test2" ]
|
||||
---
|
||||
"##,
|
||||
)
|
||||
.expect("Failed to parse front matter");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod front_matter;
|
||||
mod yaml;
|
||||
@@ -0,0 +1,29 @@
|
||||
use serde_json::Value;
|
||||
use std::str::FromStr;
|
||||
use yaml_rust2::Yaml;
|
||||
|
||||
pub fn yaml_to_serde(yaml: &Yaml) -> Value {
|
||||
match yaml {
|
||||
Yaml::Null => Value::Null,
|
||||
Yaml::Boolean(b) => Value::Bool(*b),
|
||||
Yaml::Integer(i) => Value::Number(serde_json::Number::from(*i)),
|
||||
Yaml::Real(s) => Value::Number(serde_json::Number::from_str(s).unwrap()),
|
||||
Yaml::String(s) => Value::String(s.clone()),
|
||||
Yaml::Array(arr) => Value::Array(arr.iter().map(yaml_to_serde).collect()),
|
||||
Yaml::Hash(hash) => {
|
||||
let obj: serde_json::map::Map<String, Value> = hash
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
let key = match k {
|
||||
Yaml::String(s) => s.clone(),
|
||||
_ => "INVALID_KEY".to_string(),
|
||||
};
|
||||
(key, yaml_to_serde(v))
|
||||
})
|
||||
.collect();
|
||||
Value::Object(obj)
|
||||
}
|
||||
Yaml::Alias(_) => Value::Null,
|
||||
Yaml::BadValue => Value::Null,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user