diff options
author | gennyble <gen@nyble.dev> | 2024-10-12 04:31:35 -0500 |
---|---|---|
committer | gennyble <gen@nyble.dev> | 2024-10-12 04:31:35 -0500 |
commit | 958aac71e9130b4f61e2e829dafe1b3ee16caa66 (patch) | |
tree | ec97eea8916f8ec4d7797328bc5c1fd13ece3444 /src | |
parent | c2a15e4447d7535fc3b9f8fabcfacf26947a84d2 (diff) | |
download | awake-958aac71e9130b4f61e2e829dafe1b3ee16caa66.tar.gz awake-958aac71e9130b4f61e2e829dafe1b3ee16caa66.zip |
little bit a Filesystem::reverse_resolve
Diffstat (limited to 'src')
-rwxr-xr-x | src/fs.rs | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/fs.rs b/src/fs.rs index 831aa86..e47889b 100755 --- a/src/fs.rs +++ b/src/fs.rs @@ -1,6 +1,6 @@ use camino::{Utf8Path, Utf8PathBuf}; use core::fmt; -use std::{io, ops::Deref, str::FromStr}; +use std::{fmt, io, ops::Deref, str::FromStr}; use crate::RuntimeError; @@ -154,6 +154,46 @@ impl Filesystem { } } + /// Resolve a file system path to a webpath. + /// + /// Paths are not checked to see if they exist and are not checked to be within the webroot. + /// + /// Paths attempt to resolve to the simplest form. For example, if the path is [`ROOT_INDEX`] + /// it will come out as `/` and if it is a dirfile, it will come out as a directory. + /// + /// **FOOTGUN** currently assumes every path is a file... + pub fn reverse_resolve<P: AsRef<Utf8Path>>(&self, path: P) -> Result<Webpath, RuntimeError> { + let path = path.as_ref(); + let relpath = match path.strip_prefix(&self.webroot) { + Ok(relpath) => relpath, + Err(_e) => { + if path.is_relative() { + path + } else { + //FIXME: gen- This error is not strictly correct, but it'll do for now. + // 2024-10-12 + return Err(RuntimeError::PathTooLow { + path: path.to_string(), + }); + } + } + }; + + if relpath == "" || relpath == ROOT_INDEX { + return Ok(Webpath { + webcanon: Utf8PathBuf::new(), + is_dir: true, + }); + } + + todo!(); + + Ok(Webpath { + webcanon: relpath.into(), + is_dir: false, + }) + } + pub fn metadata<P: AsRef<Utf8Path>>(path: P) -> Result<std::fs::Metadata, RuntimeError> { path.as_ref() .metadata() @@ -248,4 +288,23 @@ mod test { format!("{TESTROOT}/one/one.html") ); } + + #[test] + fn filesystem_reverse_resolves_index() { + let fs = Filesystem::new(TESTROOT); + + assert_eq!(fs.reverse_resolve(TESTROOT).unwrap(), webpath!("/")); + assert_eq!( + fs.reverse_resolve(format!("{TESTROOT}/{ROOT_INDEX}")) + .unwrap(), + webpath!("/") + ) + } + + #[test] + fn filesystem_reverse_resolves_directories() { + let fs = Filesystem::new(TESTROOT); + + assert_eq!(fs.reverse_resolve("test/").unwrap(), webpath!("test/")) + } } |