format paths

This commit is contained in:
KitsuneCafe 2024-02-17 06:56:30 -05:00
parent 7839db8b06
commit 8a89f841ab

View file

@ -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<P: AsRef<Path>>(path: &P) -> Result<PathBuf, Error> {
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<P: AsRef<Path>>(
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<u8>,
) -> 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"));
}
}