use super::*;
#[derive(Debug, Eq, PartialEq)]
pub(crate) struct TypeUrl<'a> {
pub(crate) full_name: &'a str,
}
impl<'a> TypeUrl<'a> {
pub(crate) fn new(s: &'a str) -> core::option::Option<Self> {
let slash_pos = s.rfind('/')?;
let full_name = s.get((slash_pos + 1)..)?;
if full_name.starts_with('.') {
return None;
}
Some(Self { full_name })
}
}
pub(crate) fn type_url_for<T: Name>() -> String {
format!("type.googleapis.com/{}.{}", T::PACKAGE, T::NAME)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn check_type_url_parsing() {
let example_type_name = "google.protobuf.Duration";
let url = TypeUrl::new("type.googleapis.com/google.protobuf.Duration").unwrap();
assert_eq!(url.full_name, example_type_name);
let full_url =
TypeUrl::new("https://type.googleapis.com/google.protobuf.Duration").unwrap();
assert_eq!(full_url.full_name, example_type_name);
let relative_url = TypeUrl::new("/google.protobuf.Duration").unwrap();
assert_eq!(relative_url.full_name, example_type_name);
assert_eq!(TypeUrl::new("/.google.protobuf.Duration"), None);
assert_eq!(TypeUrl::new("google.protobuf.Duration"), None);
}
}