use crate::error::Error;
use serde::{Deserialize, Deserializer, Serialize};
use std::fmt;
pub fn is_internal_path(path: &str) -> bool {
path == ".notedthat" || path.starts_with(".notedthat/")
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize)]
pub struct ObjectPath(String);
impl ObjectPath {
pub fn try_from_str(input: &str) -> Result<Self, Error> {
let s = input.strip_prefix('/').unwrap_or(input);
if s.is_empty() {
return Err(Error::InvalidInput {
message: "path must not be empty".into(),
});
}
if s.contains('\\') {
return Err(Error::InvalidInput {
message: "path must not contain backslash".into(),
});
}
if s.contains('\0') {
return Err(Error::InvalidInput {
message: "path must not contain NUL byte".into(),
});
}
for segment in s.split('/') {
if segment.is_empty() {
return Err(Error::InvalidInput {
message:
"path must not contain empty segments (double slashes or trailing slash)"
.into(),
});
}
if segment == "." || segment == ".." {
return Err(Error::InvalidInput {
message: "path must not contain '.' or '..' segments".into(),
});
}
}
Ok(Self(s.to_string()))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl TryFrom<&str> for ObjectPath {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::try_from_str(value)
}
}
impl TryFrom<String> for ObjectPath {
type Error = Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::try_from_str(&value)
}
}
impl AsRef<str> for ObjectPath {
fn as_ref(&self) -> &str {
&self.0
}
}
impl fmt::Display for ObjectPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<'de> Deserialize<'de> for ObjectPath {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
Self::try_from_str(&s).map_err(serde::de::Error::custom)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn internal_path_when_root_namespace_or_descendant() {
for path in [
".notedthat",
".notedthat/",
".notedthat/config.json",
".notedthat/nested/file",
] {
assert!(is_internal_path(path), "{path}");
}
}
#[test]
fn public_path_when_outside_root_namespace() {
for path in [
"",
"/",
".notedthat-other",
".notedthat.md",
"notes/.notedthat/file",
".NotedThat/config",
"notes/readme.md",
] {
assert!(!is_internal_path(path), "{path}");
}
}
#[test]
fn test_try_from_simple_no_leading_slash() {
let p = ObjectPath::try_from("foo/bar.md").unwrap();
assert_eq!(p.as_ref(), "foo/bar.md");
}
#[test]
fn test_try_from_strips_one_leading_slash() {
let p = ObjectPath::try_from("/foo/bar.md").unwrap();
assert_eq!(p.as_ref(), "foo/bar.md");
}
#[test]
fn test_try_from_case_preserved() {
let p = ObjectPath::try_from("FooBar/BAZ.md").unwrap();
assert_eq!(p.as_ref(), "FooBar/BAZ.md");
}
#[test]
fn test_try_from_unicode_preserved() {
let p = ObjectPath::try_from("русский.md").unwrap();
assert_eq!(p.as_ref(), "русский.md");
}
#[test]
fn test_try_from_spaces_valid() {
let p = ObjectPath::try_from("hello world.md").unwrap();
assert_eq!(p.as_ref(), "hello world.md");
}
#[test]
fn test_try_from_err_double_leading_slash() {
assert!(ObjectPath::try_from("//foo/bar.md").is_err());
}
#[test]
fn test_try_from_err_empty() {
assert!(ObjectPath::try_from("").is_err());
}
#[test]
fn test_try_from_err_slash_only_empty_after_strip() {
assert!(ObjectPath::try_from("/").is_err());
}
#[test]
fn test_try_from_err_trailing_slash_empty_segment() {
assert!(ObjectPath::try_from("foo/").is_err());
}
#[test]
fn test_try_from_err_double_slash_middle() {
assert!(ObjectPath::try_from("foo//bar").is_err());
}
#[test]
fn test_try_from_err_dot_segment_single() {
assert!(ObjectPath::try_from(".").is_err());
}
#[test]
fn test_try_from_err_dot_segment_prefix() {
assert!(ObjectPath::try_from("./foo").is_err());
}
#[test]
fn test_try_from_err_double_dot_segment() {
assert!(ObjectPath::try_from("..").is_err());
}
#[test]
fn test_try_from_err_double_dot_prefix() {
assert!(ObjectPath::try_from("../foo").is_err());
}
#[test]
fn test_try_from_err_double_dot_middle() {
assert!(ObjectPath::try_from("foo/../bar").is_err());
}
#[test]
fn test_try_from_err_backslash() {
assert!(ObjectPath::try_from("foo\\bar").is_err());
}
#[test]
fn test_try_from_err_nul_byte() {
assert!(ObjectPath::try_from("foo\x00bar").is_err());
}
#[test]
fn test_as_ref_gives_normalized_no_slash() {
let p = ObjectPath::try_from("/some/path.md").unwrap();
let s: &str = p.as_ref();
assert!(!s.starts_with('/'));
assert_eq!(s, "some/path.md");
}
#[test]
fn test_display_gives_normalized_form() {
let p = ObjectPath::try_from("/foo/bar.md").unwrap();
assert_eq!(p.to_string(), "foo/bar.md");
}
#[test]
fn test_try_from_owned_string() {
let s = String::from("foo/bar.md");
let p = ObjectPath::try_from(s).unwrap();
assert_eq!(p.as_ref(), "foo/bar.md");
}
}