-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
325 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[build] | ||
rustc-wrapper = '/Users/joep/.cargo/bin/sccache' | ||
[target.x86_64-unknown-linux-gnu] | ||
rustflags = [ | ||
'-Clink-arg=-fuse-ld=lld', | ||
'-Zshare-generics=y', | ||
] | ||
linker = '/usr/bin/clang' | ||
|
||
[target.x86_64-pc-windows-msvc] | ||
rustflags = ['-Zshare-generics=y'] | ||
linker = 'rust-lld.exe' | ||
|
||
[target.x86_64-apple-darwin] | ||
rustflags = [ | ||
'-C', | ||
'link-arg=-fuse-ld=/usr/local/bin/zld', | ||
'-Zshare-generics=y', | ||
'-Csplit-debuginfo=unpacked', | ||
] | ||
[profile.dev] | ||
opt-level = 0 | ||
debug = 2 | ||
incremental = true | ||
codegen-units = 512 | ||
|
||
[profile.release] | ||
opt-level = 3 | ||
debug = 0 | ||
incremental = false | ||
codegen-units = 256 | ||
split-debuginfo = '...' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use serde::{Deserialize, Serialize, Serializer}; | ||
use url::Url; | ||
|
||
use crate::{ | ||
errors::AtomicResult, | ||
urls::{PATH_COMMITS, PATH_IMPORT, PATH_PATH, PATH_TPF}, | ||
utils::random_string, | ||
}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
/// Wrapper for URLs / subjects. | ||
/// Has a bunch of methods for finding or creating commonly used paths. | ||
pub struct AtomicUrl { | ||
url: Url, | ||
} | ||
|
||
impl AtomicUrl { | ||
pub fn new(url: Url) -> Self { | ||
Self { url } | ||
} | ||
|
||
pub fn as_str(&self) -> &str { | ||
self.url.as_str() | ||
} | ||
|
||
/// Returns the URL to the `/tpf` endpoint | ||
pub fn path_tpf(&self) -> Self { | ||
let mut url = self.url.clone(); | ||
url.set_path(PATH_TPF); | ||
Self { url } | ||
} | ||
|
||
/// Returns the URL to the `/import` endpoint | ||
pub fn path_import(&self) -> Self { | ||
let mut url = self.url.clone(); | ||
url.set_path(PATH_IMPORT); | ||
Self { url } | ||
} | ||
/// Returns the URL to the `/commits` endpoint | ||
pub fn path_commits(&self) -> Self { | ||
let mut url = self.url.clone(); | ||
url.set_path(PATH_COMMITS); | ||
Self { url } | ||
} | ||
|
||
/// Returns the URL to the `/path` endpoint | ||
pub fn path_path(&self) -> Self { | ||
let mut url = self.url.clone(); | ||
url.set_path(PATH_PATH); | ||
Self { url } | ||
} | ||
|
||
/// Returns a new URL generated from the provided path_shortname and a random string. | ||
/// ``` | ||
/// let url = AtomicUrl::new(Url::parse("https://example.com").unwrap()); | ||
/// let generated = url.generate("my-type"); | ||
/// assert!(generated.to_string().starts_with("https://example.com/my-type/")); | ||
/// ``` | ||
pub fn generate_random(&self, path_shortname: &str) -> Self { | ||
let mut url = self.url.clone(); | ||
let path = format!("{path_shortname}/{}", random_string(10)); | ||
url.set_path(&path); | ||
Self { url } | ||
} | ||
|
||
/// Adds a path to a URL | ||
pub fn join(mut self, path: &str) -> AtomicResult<Self> { | ||
self.url = self.url.join(path)?; | ||
Ok(self) | ||
} | ||
|
||
pub fn subdomain(&self) -> Option<String> { | ||
let url = self.url.clone(); | ||
let host = url.host_str().unwrap(); | ||
let parts: Vec<&str> = host.split('.').collect(); | ||
if parts.len() > 2 { | ||
Some(parts[0].to_string()) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Returns the inner {url::Url} struct that has a bunch of regular URL methods | ||
/// Useful if you need the host or something. | ||
pub fn url(&self) -> Url { | ||
self.url.clone() | ||
} | ||
} | ||
|
||
impl TryFrom<&str> for AtomicUrl { | ||
type Error = url::ParseError; | ||
|
||
fn try_from(value: &str) -> Result<Self, Self::Error> { | ||
let url = Url::parse(value)?; | ||
Ok(Self { url }) | ||
} | ||
} | ||
|
||
impl Serialize for AtomicUrl { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(self.url.as_str()) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for AtomicUrl { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
let url = Url::parse(&s).map_err(serde::de::Error::custom)?; | ||
Ok(Self { url }) | ||
} | ||
} | ||
|
||
impl std::fmt::Display for AtomicUrl { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.url) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_url() { | ||
let _should_fail = AtomicUrl::try_from("nonsense").unwrap_err(); | ||
let _should_succeed = AtomicUrl::try_from("http://localhost/someUrl").unwrap(); | ||
} | ||
|
||
#[test] | ||
fn join_url() { | ||
let start = "http://localhost/someUrl"; | ||
let mut url = AtomicUrl::try_from(start).unwrap(); | ||
|
||
assert_eq!(url.to_string(), start); | ||
url = url.join("/123").unwrap(); | ||
assert_eq!(url.to_string(), "http://localhost/someUrl/123") | ||
} | ||
|
||
#[test] | ||
fn subdomain() { | ||
let sub = "http://test.example.com"; | ||
assert_eq!( | ||
AtomicUrl::try_from(sub).unwrap().subdomain(), | ||
Some("test".to_string()) | ||
); | ||
let no_sub = "http://example.com"; | ||
assert_eq!(AtomicUrl::try_from(no_sub).unwrap().subdomain(), None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.