use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct EntryLocator(String);
impl serde::Serialize for EntryLocator {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&self.0)
}
}
impl EntryLocator {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn memory() -> Self {
Self::new("memory://")
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for EntryLocator {
fn default() -> Self {
Self::memory()
}
}
impl fmt::Display for EntryLocator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for EntryLocator {
fn from(value: &str) -> Self {
Self::new(value)
}
}
impl From<String> for EntryLocator {
fn from(value: String) -> Self {
Self::new(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn memory_yields_the_memory_scheme() {
assert_eq!(EntryLocator::memory().as_str(), "memory://");
}
#[test]
fn default_is_memory() {
assert_eq!(EntryLocator::default(), EntryLocator::memory());
}
#[test]
fn new_preserves_the_input_verbatim() {
let loc = EntryLocator::new("file:///docs/issues/0042/index.md");
assert_eq!(loc.as_str(), "file:///docs/issues/0042/index.md");
}
#[test]
fn display_round_trips_the_inner_value() {
let loc = EntryLocator::new("git://main@abc:adr/0001.md");
assert_eq!(loc.to_string(), "git://main@abc:adr/0001.md");
}
}