use std::fmt; use log::trace; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Uri(String); impl fmt::Display for Uri { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } impl AsRef<[u8]> for Uri { fn as_ref(&self) -> &[u8] { self.0.as_bytes() } } impl Uri { pub fn new(protocol: impl fmt::Display, base_url: impl fmt::Display) -> Self { Self(format!("{protocol}://{base_url}")) } fn endpoint(&self, path: fmt::Arguments) -> String { let uri = format!("{}/api/v2/{path}", self.0); trace!("endpoint: {uri:?}"); uri } pub fn share_create(&self) -> String { self.endpoint(format_args!("alias/upload/new")) } pub fn share_notify(&self, share_id: &super::ShareID) -> String { self.endpoint(format_args!("alias/mail/notify/{share_id}")) } pub fn file_create(&self, share_id: &super::ShareID) -> String { self.endpoint(format_args!("alias/upload/{share_id}/files/tus")) } pub fn file_patch(&self, share_id: &super::ShareID, file_id: &super::FileID) -> String { self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}")) } } #[cfg(test)] mod tests { use super::*; #[test] fn basic_tests() { let cases = vec![ ("http", "example.com", "http://example.com"), ("https", "my-host:8080", "https://my-host:8080"), ("custom+scheme", "host", "custom+scheme://host"), ]; for (protocol, base_url, display) in cases { let uri = Uri::new(protocol, base_url); assert_eq!( uri.to_string(), display, "`impl Display for Uri` expected: {:?}, got {:?}", display, uri.to_string(), ); assert_eq!( uri.as_ref(), display.as_bytes(), "`impl AsRef<[u8]> for Uri` expected: {:?}, got {:?}", display.as_bytes(), uri.as_ref(), ); } } }