Src, move out of ./rust, add missing wip files
Moves file contents out of ./rust since I'm planning to go with rust for the backend and the lexicons will be used for codegen soon anyways. I also added extra files that have been in the works that I have been accidentally sprinkling into main.rs already. God forbid I need to cherry-pick anything from so far back in the history. It will make git blame ugly though.
This commit is contained in:
parent
8363125108
commit
49e7340c19
11 changed files with 846 additions and 21 deletions
70
src/atproto.rs
Normal file
70
src/atproto.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use atrium_api::types::string::{
|
||||
AtIdentifier,
|
||||
RecordKey,
|
||||
};
|
||||
use regex::Regex;
|
||||
|
||||
pub use atrium_api::types::string::{
|
||||
Nsid,
|
||||
Did,
|
||||
Handle,
|
||||
};
|
||||
|
||||
enum Authority {
|
||||
Did(Did),
|
||||
Handle(Handle),
|
||||
}
|
||||
|
||||
impl Authority {
|
||||
pub fn new(authority: String) -> Result<Self, &'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
// This implementation does not support Query or Fragments, and thus follows
|
||||
// the following schema: "at://" AUTHORITY [ "/" COLLECTION [ "/" RKEY ] ]
|
||||
pub struct Uri {
|
||||
authority: Authority,
|
||||
collection: Option<Nsid>,
|
||||
rkey: Option<RecordKey>,
|
||||
}
|
||||
|
||||
// TODO: Replace super basic URI regex with real uri parsing
|
||||
const URI_REGEX: Regex = Regex::new(
|
||||
r"/^at:\/\/([\w\.\-_~:]+)(?:\/([\w\.\-_~:]+)(?:)\/([\w\.\-_~:]+))?$/i"
|
||||
).expect("valid regex");
|
||||
|
||||
impl Uri {
|
||||
pub fn new(uri: String) -> Result<Self, &'static str> {
|
||||
let Some(captures) = URI_REGEX.captures(&uri) else {
|
||||
return Err("Invalid Uri");
|
||||
};
|
||||
// TODO: Convert authority if its a did or a handle
|
||||
let Some(Ok(authority)) = captures.get(1).map(|mtch| {
|
||||
Authority::new(mtch.as_str().to_string())
|
||||
}) else {
|
||||
return Err("Invalid Authority")
|
||||
};
|
||||
let collection = captures.get(2).map(|mtch| {
|
||||
Nsid::new(mtch.as_str().to_string())
|
||||
});
|
||||
let rkey = captures.get(3).map(|mtch| {
|
||||
RecordKey::new(mtch.as_str().to_string())
|
||||
});
|
||||
Ok(Uri { authority, collection, rkey })
|
||||
}
|
||||
|
||||
pub fn as_string(&self) -> String {
|
||||
let mut uri = String::from("at://");
|
||||
uri.push_str(match &self.authority {
|
||||
Authority::Handle(h) => &*h,
|
||||
Authority::Did(d) => &*d,
|
||||
});
|
||||
if let Some(nsid) = &self.collection {
|
||||
uri.push_str(&*nsid);
|
||||
}
|
||||
if let Some(rkey) = &self.rkey {
|
||||
uri.push_str(&*rkey);
|
||||
}
|
||||
uri
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue