From 8a89f841abaf1e23e64aa605621605a293a7985b Mon Sep 17 00:00:00 2001 From: KitsuneCafe <10284516+kitsunecafe@users.noreply.github.com> Date: Sat, 17 Feb 2024 06:56:30 -0500 Subject: [PATCH] format paths --- src/roxy.rs | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/roxy.rs b/src/roxy.rs index e977a5d..e9a9dea 100644 --- a/src/roxy.rs +++ b/src/roxy.rs @@ -1,7 +1,7 @@ use std::{ fs::{self, File}, io::{BufRead, BufReader, Read, Write}, - path::Path, + path::{Path, PathBuf}, }; use crate::error::Error; @@ -136,36 +136,37 @@ impl Roxy { File::create(path) } + fn to_formatted_path>(path: &P) -> Result { + let path = path.as_ref(); + match path.with_extension("").file_name() { + Some(name) if name == "index" => Ok(path.to_path_buf()), + Some(_) => Ok(path.with_extension("").join("index.html")), + None => Err(Error::from("invalid path \"{path:?}\"".to_string())), + } + } + pub fn process_file>( input: &P, output: P, parser: &mut Parser, - ) -> std::io::Result<()> { + ) -> Result<(), Error> { let buf = Self::parse(input, parser)?; - Self::mkdir_and_open(&output).and_then(|mut f| f.write_all(&buf)) + Self::mkdir_and_open(&Self::to_formatted_path(&output)?) + .and_then(|mut f| f.write_all(&buf)) + .map_err(Error::from) } } #[cfg(test)] mod tests { - use super::{Parse, Parser}; + use super::Roxy; - struct TestParser; - impl TestParser { - pub fn new() -> Self { - Self - } - } + #[test] + fn formatted_path() { + let idx = Roxy::to_formatted_path(&"/test/index.html").unwrap(); + let name = Roxy::to_formatted_path(&"/i-love/cats.html").unwrap(); - impl Parse for TestParser { - fn parse( - &mut self, - path: &str, - src: &[u8], - dst: &mut Vec, - ) -> Result<(), crate::error::Error> { - Ok(()) - } + assert_eq!(idx.to_str(), Some("/test/index.html")); + assert_eq!(name.to_str(), Some("/i-love/cats/index.html")); } } -