From fc38c360ebf87e434a62a4e466b2935519c14b31 Mon Sep 17 00:00:00 2001 From: kitsunecafe <10284516+kitsunecafe@users.noreply.github.com> Date: Wed, 24 Jan 2024 02:13:16 -0500 Subject: [PATCH] i dont remember --- Cargo.lock | 1 + Cargo.toml | 2 +- rust-toolchain.toml | 2 ++ src/main.rs | 54 +++++++++++++++++++++++++++++++-------------- 4 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/Cargo.lock b/Cargo.lock index bac90b3..c372d94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -816,6 +816,7 @@ dependencies = [ [[package]] name = "roxy_core" version = "0.1.0" +source = "git+https://github.com/kitsunecafe/roxy-core.git#42af0529bfc8259930c21b6edec94ba2793b0db1" dependencies = [ "pulldown-cmark", "tera", diff --git a/Cargo.toml b/Cargo.toml index 216b513..6ae4cab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,6 @@ edition = "2021" [dependencies] glob = "0.3.1" syntect = "5.1.0" -roxy_core = { path = "../roxy_core/" } +roxy_core = { git = "https://github.com/kitsunecafe/roxy-core.git" } clap = { version = "4.4.17", features = ["derive"] } diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" diff --git a/src/main.rs b/src/main.rs index 1df7484..662607e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,6 @@ use clap::Parser as Clap; use std::{ - ffi, io::{Error, ErrorKind}, - ops::Deref, path::{Path, PathBuf, StripPrefixError}, }; @@ -24,6 +22,12 @@ struct RoxyError { message: String, } +impl From for RoxyError { + fn from(value: String) -> Self { + Self { message: value } + } +} + impl From for RoxyError { fn from(value: PatternError) -> Self { Self { @@ -46,24 +50,42 @@ impl From for Error { } } -fn get_files(path: &str) -> Result, RoxyError> { - glob(path) - .map(|p| p.filter_map(|x| x.ok()).collect()) - .map_err(RoxyError::from) +fn get_files + std::fmt::Debug>(path: &P) -> Result, RoxyError> { + let path = path + .as_ref() + .to_str() + .ok_or_else(|| RoxyError::from(format!("{path:?} is not a valid path.")))?; + + let files: Vec = glob(path)? + .filter_map(|x| x.ok()) + .filter(|f| Path::is_file(f)) + .collect(); + + Ok(files) } +#[derive(Debug)] struct FilePath<'a, P: AsRef> { - input: &'a P, + input: PathBuf, + root_dir: PathBuf, output: &'a P, } impl<'a, P: AsRef + 'a> FilePath<'a, P> { pub fn new(input: &'a P, output: &'a P) -> Self { - Self { input, output } + Self { + input: Self::make_recursive(input), + root_dir: Self::strip_wildcards(input), + output, + } } - fn has_wildcard(path: &str) -> bool { - path.contains("*") + fn make_recursive(path: &'a P) -> PathBuf { + path.as_ref().join("**/*") + } + + fn has_no_wildcard>(path: &S) -> bool { + !path.as_ref().contains("*") } fn strip_wildcards + ?Sized>(path: &'a P2) -> PathBuf { @@ -71,15 +93,14 @@ impl<'a, P: AsRef + 'a> FilePath<'a, P> { .ancestors() .map(Path::to_str) .flatten() - .inspect(|f| println!("{f}")) - .skip_while(Self::has_wildcard) - .collect() + .find(Self::has_no_wildcard) + .map_or_else(|| PathBuf::new(), PathBuf::from) } pub fn to_output>(&self, value: &'a P2) -> Result { value .as_ref() - .strip_prefix(Self::strip_wildcards(self.input)) + .strip_prefix(&self.root_dir) .map(|path| self.output.as_ref().join(path)) .map_err(RoxyError::from) } @@ -93,8 +114,9 @@ fn main() -> Result<(), RoxyError> { parser.push(html); let file_path = FilePath::new(&opts.input, &opts.output); - for file in get_files(&opts.input)? { - Roxy::process_file(&file, &(&file_path.to_output(&file)?), &mut parser); + for file in get_files(&file_path.input)? { + let file_name = file.with_extension("html"); + let _ = Roxy::process_file(&file, &(&file_path.to_output(&file_name)?), &mut parser); } Ok(())