-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Merge branch 'main' into umblock-tests
- Loading branch information
Showing
19 changed files
with
229 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use std::fs; | ||
use std::path::{Path, PathBuf}; | ||
|
||
use sha3::{Digest, Sha3_256}; | ||
|
||
use crate::middleend::{AsIrLines, MetadataIrLine, WriteToIr}; | ||
use crate::um_error::UmError; | ||
|
||
/// Represents a Unimarkup metadata | ||
#[derive(Debug, Default, Clone)] | ||
pub struct Metadata { | ||
/// Unimarkup file this metadata is from | ||
pub file: PathBuf, | ||
/// Preamble of the Unimarkup file | ||
pub preamble: String, | ||
/// Kind of the Unimarkup file | ||
pub kind: MetadataKind, | ||
/// Namespace of the Unimarkup file | ||
pub namespace: String, | ||
} | ||
|
||
/// The kind of a Unimarkup file | ||
#[derive(Debug, Clone, Copy)] | ||
pub enum MetadataKind { | ||
/// Identifies the Unimarkup file as the root of this document | ||
/// | ||
/// **Note:** Only one metadata entry of an IR may be `Root` | ||
Root, | ||
/// The Unimarkup file must be considered as a theme file | ||
Theme, | ||
/// The Unimarkup file is inserted inside an element of the root file | ||
Insert, | ||
} | ||
|
||
impl Default for MetadataKind { | ||
fn default() -> Self { | ||
Self::Insert | ||
} | ||
} | ||
|
||
impl AsIrLines<MetadataIrLine> for Metadata { | ||
fn as_ir_lines(&self) -> Vec<MetadataIrLine> { | ||
let filepath = self.file.to_string_lossy().into_owned(); | ||
let err_filehash_calc = format!("Could not calculate hash for file `{}`!", &filepath); | ||
let err_filename_conversion = | ||
format!("Given file `{}` is not a valid metadata file!", &filepath); | ||
|
||
let metadata = MetadataIrLine { | ||
filehash: get_filehash(&self.file).expect(&err_filehash_calc), | ||
filename: self | ||
.file | ||
.file_name() | ||
.expect(&err_filename_conversion) | ||
.to_string_lossy() | ||
.into_owned(), | ||
path: self.file.to_string_lossy().into_owned(), | ||
preamble: self.preamble.clone(), | ||
fallback_preamble: String::new(), | ||
root: true, | ||
}; | ||
|
||
vec![metadata] | ||
} | ||
} | ||
|
||
impl From<Metadata> for MetadataIrLine { | ||
fn from(metadata: Metadata) -> Self { | ||
metadata.as_ir_lines().pop().unwrap() | ||
} | ||
} | ||
|
||
impl WriteToIr for Metadata { | ||
fn write_to_ir(&self, ir_transaction: &rusqlite::Transaction) -> Result<(), UmError> { | ||
let ir_metadata: MetadataIrLine = self.as_ir_lines().pop().unwrap(); | ||
ir_metadata.write_to_ir(ir_transaction) | ||
} | ||
} | ||
|
||
/// Calculates the sha3-256 hash of a given file | ||
fn get_filehash(file: &Path) -> Result<Vec<u8>, UmError> { | ||
let mut hasher = Sha3_256::new(); | ||
let source = fs::read_to_string(file).map_err(|err| UmError::General { | ||
msg: String::from("Could not read file."), | ||
error: Box::new(err), | ||
})?; | ||
|
||
hasher.update(source); | ||
|
||
let hash = hasher.finalize(); | ||
Ok(hash.to_vec()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
//! Available elements for a Unimarkup document. | ||
mod heading_block; | ||
mod metadata; | ||
mod paragraph_block; | ||
mod verbatim_block; | ||
|
||
pub mod types; | ||
|
||
pub use heading_block::*; | ||
pub use metadata::*; | ||
pub use paragraph_block::*; | ||
pub use verbatim_block::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.