55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
use lazy_regex::regex_captures;
|
|
use core::str::FromStr;
|
|
|
|
pub use atrium_api::types::{
|
|
Collection,
|
|
string::{
|
|
Nsid,
|
|
RecordKey,
|
|
AtIdentifier as Authority,
|
|
}
|
|
};
|
|
|
|
pub struct Uri {
|
|
whole: String,
|
|
// These fields could be useful in the future,
|
|
// so I'm leaving the code for them.
|
|
// authority: Authority,
|
|
// collection: Option<Nsid>,
|
|
// rkey: Option<RecordKey>,
|
|
}
|
|
|
|
impl Uri {
|
|
pub fn as_str(&self) -> &str {
|
|
self.whole.as_str()
|
|
}
|
|
|
|
pub fn from_str(uri: String) -> Result<Self, &'static str> {
|
|
if uri.len() > 8000 {
|
|
return Err("Uri too long")
|
|
}
|
|
|
|
let Some((
|
|
whole, unchecked_authority, unchecked_collection, unchecked_rkey
|
|
)) = regex_captures!(
|
|
r"/^at:\/\/([\w\.\-_~:]+)(?:\/([\w\.\-_~:]+)(?:)\/([\w\.\-_~:]+))?$/i",
|
|
&uri,
|
|
) else {
|
|
return Err("Invalid Uri");
|
|
};
|
|
|
|
// This parsing is required, but the values don't need to be used yet.
|
|
// No compute cost to use them, just storage cost
|
|
let _authority = Authority::from_str(unchecked_authority)?;
|
|
|
|
let _collection = if unchecked_collection.is_empty() { None }
|
|
else { Some(Nsid::new(unchecked_collection.to_string())?) };
|
|
|
|
let _rkey = if unchecked_rkey.is_empty() { None }
|
|
else { Some(RecordKey::new(unchecked_rkey.to_string())?) };
|
|
|
|
// Ok(Uri{ whole: whole.to_string(), authority, collection, rkey })
|
|
Ok(Uri { whole: whole.to_string() })
|
|
}
|
|
}
|
|
|