format paths
This commit is contained in:
parent
7839db8b06
commit
8a89f841ab
41
src/roxy.rs
41
src/roxy.rs
|
@ -1,7 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::{BufRead, BufReader, Read, Write},
|
io::{BufRead, BufReader, Read, Write},
|
||||||
path::Path,
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
|
@ -136,36 +136,37 @@ impl Roxy {
|
||||||
File::create(path)
|
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>>(
|
pub fn process_file<P: AsRef<Path>>(
|
||||||
input: &P,
|
input: &P,
|
||||||
output: P,
|
output: P,
|
||||||
parser: &mut Parser,
|
parser: &mut Parser,
|
||||||
) -> std::io::Result<()> {
|
) -> Result<(), Error> {
|
||||||
let buf = Self::parse(input, parser)?;
|
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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{Parse, Parser};
|
use super::Roxy;
|
||||||
|
|
||||||
struct TestParser;
|
#[test]
|
||||||
impl TestParser {
|
fn formatted_path() {
|
||||||
pub fn new() -> Self {
|
let idx = Roxy::to_formatted_path(&"/test/index.html").unwrap();
|
||||||
Self
|
let name = Roxy::to_formatted_path(&"/i-love/cats.html").unwrap();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse for TestParser {
|
assert_eq!(idx.to_str(), Some("/test/index.html"));
|
||||||
fn parse(
|
assert_eq!(name.to_str(), Some("/i-love/cats/index.html"));
|
||||||
&mut self,
|
|
||||||
path: &str,
|
|
||||||
src: &[u8],
|
|
||||||
dst: &mut Vec<u8>,
|
|
||||||
) -> Result<(), crate::error::Error> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue