use std::str::FromStr;
use serde::{Deserialize, Serialize};
use super::StoragePath;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct WebDavPathAxum(StoragePath);
impl WebDavPathAxum {
pub fn inner(&self) -> &StoragePath {
&self.0
}
}
impl std::fmt::Display for WebDavPathAxum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.as_str())
}
}
impl FromStr for WebDavPathAxum {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let with_slash = format!("/{}", s);
let inner = StoragePath::normalize(&with_slash)?;
Ok(Self(inner))
}
}
impl Serialize for WebDavPathAxum {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.0.as_str())
}
}
impl<'de> Deserialize<'de> for WebDavPathAxum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Self::from_str(&s).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct WebDavFilePathAxum(WebDavPathAxum);
impl WebDavFilePathAxum {
pub fn inner(&self) -> &StoragePath {
self.0.inner()
}
}
impl std::fmt::Display for WebDavFilePathAxum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for WebDavFilePathAxum {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
WebDavPathAxum::from_str(s)?.try_into()
}
}
impl TryFrom<WebDavPathAxum> for WebDavFilePathAxum {
type Error = anyhow::Error;
fn try_from(path: WebDavPathAxum) -> Result<Self, Self::Error> {
anyhow::ensure!(path.inner().is_file(), "target path must be a file");
Ok(Self(path))
}
}
impl Serialize for WebDavFilePathAxum {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}
impl<'de> Deserialize<'de> for WebDavFilePathAxum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let path = WebDavPathAxum::deserialize(deserializer)?;
Self::try_from(path).map_err(serde::de::Error::custom)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn adds_leading_slash() {
let path = WebDavPathAxum::from_str("foo/bar").unwrap();
assert_eq!(path.inner().as_str(), "/foo/bar");
}
#[test]
fn accepts_non_pub_paths() {
WebDavPathAxum::from_str("priv/file.txt").expect("Should be valid");
WebDavPathAxum::from_str("pub/file.txt").expect("Should be valid");
}
#[test]
fn file_path_adds_leading_slash() {
let path = WebDavFilePathAxum::from_str("foo/bar.txt").unwrap();
assert_eq!(path.inner().as_str(), "/foo/bar.txt");
}
#[test]
fn file_path_rejects_directory_paths() {
WebDavFilePathAxum::from_str("foo/bar/").expect_err("directory path should be rejected");
}
#[test]
fn file_path_accepts_non_pub_paths() {
WebDavFilePathAxum::from_str("priv/file.txt").expect("priv file path should be valid");
WebDavFilePathAxum::from_str("other/file.txt")
.expect("root validation should remain an authorization concern");
}
}