Something went wrong. Try again.
[WIP] A (somewhat barebones) atproto app for creating custom sites without hosting!
Something went wrong. Try again.
2.1 kB · 76 lines
Rust
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677use regex::Regex;
#[allow(dead_code)]pub fn rkey_to_url(rkey: String) -> Option<String> { let regex = Regex::new( // symbols A-Za-z0-9 -._~: are all valid rkey characters // however we use a subset of record keys where : is an escape character // allow any rkey character except colons // or any of the valid escape sequences // from start of string to end between 0 and unlimited times r"^(?:[A-Za-z0-9.\-_~]|(?:::)|(?::~)|(?::21)|(?::24)|(?::26)|(?::27)|(?::28)|(?::29)|(?::2A)|(?::2B)|(?::2C)|(?::3A)|(?::3B)|(?::3D)|(?::40))*$", ) .expect("Regex failed to generate");
if !regex.is_match(&rkey) { return None; };
let res = rkey .replace("::", "/") .replace(":~", "%") .replace(":21", "!") .replace(":24", "$") .replace(":26", "&") .replace(":27", "'") .replace(":28", "(") .replace(":29", ")") .replace(":2A", "*") .replace(":2B", "+") .replace(":2C", ",") .replace(":3A", ":") .replace(":3B", ";") .replace(":3D", "=") .replace(":40", "@");
Some(res)}
pub fn url_to_rkey(url: String) -> Option<String> { let regex = Regex::new(r"^(?:[a-zA-Z0-9/\-._~!$&'()*+,;=:@]|(?:%[0-9a-fA-F]{2}))*$") .expect("Regex failed to generate"); if !regex.is_match(&url) { return None; }
let res = url // : replace is hoisted so it doesnt replace colons from elsewhere .replace(":", ":3A") .replace("/", "::") .replace("%", ":~") .replace("!", ":21") .replace("$", ":24") .replace("&", ":26") .replace("'", ":27") .replace("(", ":28") .replace(")", ":29") .replace("*", ":2A") .replace("+", ":2B") .replace(",", ":2C") .replace(";", ":3B") .replace("=", ":3D") .replace("@", ":40");
Some(res)}
pub fn site_handle(user: String) -> String { if user.contains(":") { let user = user.split(":").collect::<Vec<_>>(); let method = user[1]; let did = user[2]; format!("https://{did}.did-{method}.atcities.dev/") } else { format!("https://{user}.atcities.dev/") }}