specfold_core/corpus/
scan.rs1use crate::markdown::{body, frontmatter};
4use crate::model::doc::Frontmatter;
5use regex::Regex;
6use serde::{Deserialize, Serialize};
7use sha2::{Digest, Sha256};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct FileScan {
11 pub frontmatter: Frontmatter,
12 pub body: body::Body,
13 pub body_hash: String,
14}
15
16pub fn hex(bytes: &[u8]) -> String {
17 bytes.iter().map(|b| format!("{b:02x}")).collect()
18}
19
20pub fn scan(text: &str, cite_re: &Regex) -> FileScan {
22 let split = frontmatter::split(text);
23 let fm = match frontmatter::parse(split.raw) {
24 frontmatter::Parsed::None => Frontmatter::default(),
25 frontmatter::Parsed::Map(m) => Frontmatter { map: Some(m), error: None },
26 frontmatter::Parsed::Invalid(e) => Frontmatter { map: None, error: Some(e) },
27 };
28 FileScan {
29 frontmatter: fm,
30 body: body::scan(split.body, split.body_line, cite_re),
31 body_hash: hex(&Sha256::digest(split.body.as_bytes())),
32 }
33}