borrow checker doesnt let me have fun

This commit is contained in:
KitsuneCafe 2024-02-05 05:35:12 -05:00
parent 1be561f6bd
commit 6f8a28575d
4 changed files with 58 additions and 35 deletions

20
Cargo.lock generated
View file

@ -598,9 +598,9 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]] [[package]]
name = "pest" name = "pest"
version = "2.7.6" version = "2.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f200d8d83c44a45b21764d1916299752ca035d15ecd46faca3e9a2a2bf6ad06" checksum = "219c0dcc30b6a27553f9cc242972b67f75b60eb0db71f0b5462f38b058c41546"
dependencies = [ dependencies = [
"memchr", "memchr",
"thiserror", "thiserror",
@ -609,9 +609,9 @@ dependencies = [
[[package]] [[package]]
name = "pest_derive" name = "pest_derive"
version = "2.7.6" version = "2.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcd6ab1236bbdb3a49027e920e693192ebfe8913f6d60e294de57463a493cfde" checksum = "22e1288dbd7786462961e69bfd4df7848c1e37e8b74303dbdab82c3a9cdd2809"
dependencies = [ dependencies = [
"pest", "pest",
"pest_generator", "pest_generator",
@ -619,9 +619,9 @@ dependencies = [
[[package]] [[package]]
name = "pest_generator" name = "pest_generator"
version = "2.7.6" version = "2.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a31940305ffc96863a735bef7c7994a00b325a7138fdbc5bda0f1a0476d3275" checksum = "1381c29a877c6d34b8c176e734f35d7f7f5b3adaefe940cb4d1bb7af94678e2e"
dependencies = [ dependencies = [
"pest", "pest",
"pest_meta", "pest_meta",
@ -632,9 +632,9 @@ dependencies = [
[[package]] [[package]]
name = "pest_meta" name = "pest_meta"
version = "2.7.6" version = "2.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7ff62f5259e53b78d1af898941cdcdccfae7385cf7d793a6e55de5d05bb4b7d" checksum = "d0934d6907f148c22a3acbda520c7eed243ad7487a30f51f6ce52b58b7077a8a"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"pest", "pest",
@ -835,7 +835,7 @@ dependencies = [
[[package]] [[package]]
name = "roxy_core" name = "roxy_core"
version = "0.1.0" version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-core.git#3059a3b77492be751cba36d012557bd9622c62a7" source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-core.git#7839db8b062698adfe81a86d2a0cf041a6711456"
[[package]] [[package]]
name = "roxy_markdown_parser" name = "roxy_markdown_parser"
@ -859,7 +859,7 @@ dependencies = [
[[package]] [[package]]
name = "roxy_syntect" name = "roxy_syntect"
version = "0.1.0" version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-syntect.git#39c3a9e4b2f294936df5141b717efcbbe22deea7" source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-syntect.git#48601fc5e6e0ee0e753c892f0eb42a9b0b48be99"
dependencies = [ dependencies = [
"once_cell", "once_cell",
"regex", "regex",

View file

@ -15,6 +15,8 @@ Roxy will read each file from `INPUT` for valid files. It will look for `toml` f
Currently, Roxy only has two configuration keys Currently, Roxy only has two configuration keys
```toml ```toml
# config.toml
[syntect]
theme = "base16-ocean.dark" # the name of the theme for syntax highlighting theme = "base16-ocean.dark" # the name of the theme for syntax highlighting
themes = ["./themes/base16-ocean.dark.tmTheme"] themes = ["./themes/base16-ocean.dark.tmTheme"]
``` ```

View file

@ -2,23 +2,13 @@ use std::str::FromStr;
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Default, Deserialize)] pub(crate) trait Merge {
pub(crate) struct Config { fn merge(self, other: Self) -> Self;
pub theme: Option<String>,
pub themes: Vec<String>,
} }
impl Config { #[derive(Debug, Default, Deserialize)]
pub fn merge(self, other: Config) -> Self { pub(crate) struct Config {
Self { pub syntect: Syntect,
theme: self.theme.or(other.theme),
themes: if self.themes.is_empty() {
other.themes
} else {
self.themes
},
}
}
} }
impl FromStr for Config { impl FromStr for Config {
@ -29,3 +19,26 @@ impl FromStr for Config {
} }
} }
impl Merge for Config {
fn merge(self, other: Self) -> Self {
Self {
syntect: self.syntect.merge(other.syntect),
}
}
}
#[derive(Debug, Default, Deserialize)]
pub(crate) struct Syntect {
pub theme: Option<String>,
pub theme_dir: Option<String>,
}
impl Merge for Syntect {
fn merge(self, other: Syntect) -> Self {
Self {
theme: self.theme.or(other.theme),
theme_dir: self.theme_dir.or(other.theme_dir)
}
}
}

View file

@ -2,7 +2,7 @@ pub mod config;
pub mod context; pub mod context;
mod file_path; mod file_path;
use config::Config; use config::{Config, Merge};
use context::Context; use context::Context;
use file_path::FilePath; use file_path::FilePath;
use roxy_core::error::Error; use roxy_core::error::Error;
@ -19,6 +19,7 @@ use std::{
io::{BufReader, Read}, io::{BufReader, Read},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use syntect::{highlighting::ThemeSet, parsing::SyntaxSet};
use toml::Table; use toml::Table;
use glob::glob; use glob::glob;
@ -87,7 +88,10 @@ fn context_from_meta_files<'a, T: AsRef<Path>>(
Ok(context) Ok(context)
} }
fn copy_static<T: AsRef<Path>>(files: &Vec<&PathBuf>, file_path: &FilePath<T>) -> Result<(), Error> { fn copy_static<T: AsRef<Path>>(
files: &Vec<&PathBuf>,
file_path: &FilePath<T>,
) -> Result<(), Error> {
for file in files { for file in files {
let output = file_path.to_output(file)?; let output = file_path.to_output(file)?;
fs::create_dir_all(output.parent().unwrap())?; fs::create_dir_all(output.parent().unwrap())?;
@ -112,16 +116,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let (meta, files): (Vec<&PathBuf>, Vec<&PathBuf>) = let (meta, files): (Vec<&PathBuf>, Vec<&PathBuf>) =
files.iter().partition(|f| f.extension().unwrap() == "toml"); files.iter().partition(|f| f.extension().unwrap() == "toml");
let (content, files): (Vec<&PathBuf>, Vec<&PathBuf>) = files let (content, files): (Vec<&PathBuf>, Vec<&PathBuf>) = files.iter().partition(|f| {
.iter() let ext = f.extension().and_then(ffi::OsStr::to_str).unwrap();
.partition(|f| { CONTENT_EXT.contains(&ext)
let ext = f.extension().and_then(ffi::OsStr::to_str).unwrap(); });
CONTENT_EXT.contains(&ext)
});
let mut context = context_from_meta_files(&meta, &file_path)?; let mut context = context_from_meta_files(&meta, &file_path)?;
let theme = config.theme.unwrap_or(DEFAULT_THEME.to_string()); let theme = config.syntect.theme.unwrap_or(DEFAULT_THEME.to_string());
let syntax_set = SyntaxSet::load_defaults_newlines();
let theme_set = if let Some(dir) = config.syntect.theme_dir {
ThemeSet::load_from_folder(dir)?
} else {
ThemeSet::load_defaults()
};
for file in content { for file in content {
let file_name = file.with_extension("html"); let file_name = file.with_extension("html");
@ -131,7 +139,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut preformatter = MarkdownTeraPreformatter::new(); let mut preformatter = MarkdownTeraPreformatter::new();
parser.push(&mut preformatter); parser.push(&mut preformatter);
let mut syntect = SyntectParser::new(theme.as_str()); let mut syntect = SyntectParser::new(&syntax_set, &theme_set, theme.as_str());
parser.push(&mut syntect); parser.push(&mut syntect);
let mut md = MarkdownParser::new(); let mut md = MarkdownParser::new();