|
| 1 | +use serde::{Deserialize, Serialize, Serializer}; |
| 2 | +use url::Url; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + errors::AtomicResult, |
| 6 | + urls::{PATH_COMMITS, PATH_IMPORT, PATH_PATH, PATH_TPF}, |
| 7 | + utils::random_string, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 11 | +/// Wrapper for URLs / subjects. |
| 12 | +/// Has a bunch of methods for finding or creating commonly used paths. |
| 13 | +pub struct AtomicUrl { |
| 14 | + url: Url, |
| 15 | +} |
| 16 | + |
| 17 | +impl AtomicUrl { |
| 18 | + pub fn new(url: Url) -> Self { |
| 19 | + Self { url } |
| 20 | + } |
| 21 | + |
| 22 | + pub fn as_str(&self) -> &str { |
| 23 | + self.url.as_str() |
| 24 | + } |
| 25 | + |
| 26 | + /// Returns the URL to the `/tpf` endpoint |
| 27 | + pub fn path_tpf(&self) -> Self { |
| 28 | + let mut url = self.url.clone(); |
| 29 | + url.set_path(PATH_TPF); |
| 30 | + Self { url } |
| 31 | + } |
| 32 | + |
| 33 | + /// Returns the URL to the `/import` endpoint |
| 34 | + pub fn path_import(&self) -> Self { |
| 35 | + let mut url = self.url.clone(); |
| 36 | + url.set_path(PATH_IMPORT); |
| 37 | + Self { url } |
| 38 | + } |
| 39 | + /// Returns the URL to the `/commits` endpoint |
| 40 | + pub fn path_commits(&self) -> Self { |
| 41 | + let mut url = self.url.clone(); |
| 42 | + url.set_path(PATH_COMMITS); |
| 43 | + Self { url } |
| 44 | + } |
| 45 | + |
| 46 | + /// Returns the URL to the `/path` endpoint |
| 47 | + pub fn path_path(&self) -> Self { |
| 48 | + let mut url = self.url.clone(); |
| 49 | + url.set_path(PATH_PATH); |
| 50 | + Self { url } |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns a new URL generated from the provided path_shortname and a random string. |
| 54 | + /// ``` |
| 55 | + /// let url = AtomicUrl::new(Url::parse("https://example.com").unwrap()); |
| 56 | + /// let generated = url.generate("my-type"); |
| 57 | + /// assert!(generated.to_string().starts_with("https://example.com/my-type/")); |
| 58 | + /// ``` |
| 59 | + pub fn generate_random(&self, path_shortname: &str) -> Self { |
| 60 | + let mut url = self.url.clone(); |
| 61 | + let path = format!("{path_shortname}/{}", random_string(10)); |
| 62 | + url.set_path(&path); |
| 63 | + Self { url } |
| 64 | + } |
| 65 | + |
| 66 | + /// Adds a path to a URL |
| 67 | + pub fn join(mut self, path: &str) -> AtomicResult<Self> { |
| 68 | + self.url = self.url.join(path)?; |
| 69 | + Ok(self) |
| 70 | + } |
| 71 | + |
| 72 | + pub fn subdomain(&self) -> Option<String> { |
| 73 | + let url = self.url.clone(); |
| 74 | + let host = url.host_str().unwrap(); |
| 75 | + let parts: Vec<&str> = host.split('.').collect(); |
| 76 | + if parts.len() > 2 { |
| 77 | + Some(parts[0].to_string()) |
| 78 | + } else { |
| 79 | + None |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// Returns the inner {url::Url} struct that has a bunch of regular URL methods |
| 84 | + /// Useful if you need the host or something. |
| 85 | + pub fn url(&self) -> Url { |
| 86 | + self.url.clone() |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl TryFrom<&str> for AtomicUrl { |
| 91 | + type Error = url::ParseError; |
| 92 | + |
| 93 | + fn try_from(value: &str) -> Result<Self, Self::Error> { |
| 94 | + let url = Url::parse(value)?; |
| 95 | + Ok(Self { url }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl Serialize for AtomicUrl { |
| 100 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 101 | + where |
| 102 | + S: Serializer, |
| 103 | + { |
| 104 | + serializer.serialize_str(self.url.as_str()) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +impl<'de> Deserialize<'de> for AtomicUrl { |
| 109 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 110 | + where |
| 111 | + D: serde::Deserializer<'de>, |
| 112 | + { |
| 113 | + let s = String::deserialize(deserializer)?; |
| 114 | + let url = Url::parse(&s).map_err(serde::de::Error::custom)?; |
| 115 | + Ok(Self { url }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl std::fmt::Display for AtomicUrl { |
| 120 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 121 | + write!(f, "{}", self.url) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#[cfg(test)] |
| 126 | +mod test { |
| 127 | + use super::*; |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn test_url() { |
| 131 | + let _should_fail = AtomicUrl::try_from("nonsense").unwrap_err(); |
| 132 | + let _should_succeed = AtomicUrl::try_from("http://localhost/someUrl").unwrap(); |
| 133 | + } |
| 134 | + |
| 135 | + #[test] |
| 136 | + fn join_url() { |
| 137 | + let start = "http://localhost/someUrl"; |
| 138 | + let mut url = AtomicUrl::try_from(start).unwrap(); |
| 139 | + |
| 140 | + assert_eq!(url.to_string(), start); |
| 141 | + url = url.join("/123").unwrap(); |
| 142 | + assert_eq!(url.to_string(), "http://localhost/someUrl/123") |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn subdomain() { |
| 147 | + let sub = "http://test.example.com"; |
| 148 | + assert_eq!( |
| 149 | + AtomicUrl::try_from(sub).unwrap().subdomain(), |
| 150 | + Some("test".to_string()) |
| 151 | + ); |
| 152 | + let no_sub = "http://example.com"; |
| 153 | + assert_eq!(AtomicUrl::try_from(no_sub).unwrap().subdomain(), None); |
| 154 | + } |
| 155 | +} |
0 commit comments