use crate::error::{NetError, Result};
use serde_json::Value;
pub type ScopeCheck = fn(&Value) -> Result<()>;
#[derive(Debug, Clone, Copy)]
pub struct Profile {
pub name: &'static str,
pub protocol: &'static str,
pub kad: &'static str,
pub relay_key: &'static str,
pub identify: &'static str,
pub offer_prefix: &'static str,
pub answer_prefix: &'static str,
pub token_mark: &'static str,
pub join_link: &'static str,
pub answer_link: &'static str,
pub link_mark: &'static str,
pub offer_domain: &'static str,
pub answer_domain: &'static str,
pub transcript_domain: &'static str,
pub key_info: &'static str,
pub sas_info: &'static str,
pub msg_aad: &'static str,
pub cert: &'static str,
pub revoke: &'static str,
pub sync_key: &'static str,
pub sync_aad: &'static str,
pub names: &'static str,
pub peer_record: &'static str,
pub withdrawal: &'static str,
pub peer_key: &'static str,
pub default_port: u16,
pub thread: &'static str,
pub scopes: ScopeCheck,
}
pub const WEIRDNET: Profile = Profile {
name: "weirdnet",
protocol: "/weirdnet/net/1",
kad: "/weirdnet/kad/1",
relay_key: "/weirdnet/relay/1",
identify: "/weirdnet/id/1",
offer_prefix: "wnet-x1-offer:",
answer_prefix: "wnet-x1-answer:",
token_mark: "wnet-x1-",
join_link: "weirdnet://join/",
answer_link: "weirdnet://answer/",
link_mark: "weirdnet://",
offer_domain: "weirdnet/net/offer/v1",
answer_domain: "weirdnet/net/answer/v1",
transcript_domain: "weirdnet/net/exchange/v1",
key_info: "weirdnet/net/exchange/v1/key",
sas_info: "weirdnet/net/exchange/v1/sas",
msg_aad: "weirdnet/net/msg/v1",
cert: "weirdnet-node-cert/1",
revoke: "weirdnet-node-revoke/1",
sync_key: "weirdnet/sync/v1/key",
sync_aad: "weirdnet/sync/v1",
names: "/weirdnet/names/1",
peer_record: "weirdnet/peer-record/v1",
withdrawal: "weirdnet/withdrawal/v1",
peer_key: "weirdnet/peer/",
default_port: 4747,
thread: "weirdnet-net",
scopes,
};
pub fn node_scope() -> Value {
serde_json::json!({ "kind": "node" })
}
pub fn link_scope() -> Value {
serde_json::json!({ "kind": "link" })
}
pub fn invite_scope(to: &str, role: &str) -> Value {
serde_json::json!({ "kind": "invite", "to": to, "as": role })
}
pub fn scope_kind(scope: &Value) -> &str {
scope.get("kind").and_then(|k| k.as_str()).unwrap_or("")
}
fn field<'a>(scope: &'a Value, k: &str) -> &'a str {
scope.get(k).and_then(|v| v.as_str()).unwrap_or("")
}
pub fn scopes(scope: &Value) -> Result<()> {
match scope_kind(scope) {
"node" | "link" => Ok(()),
"invite" => {
let (to, role) = (field(scope, "to"), field(scope, "as"));
if to.is_empty() || to.len() > 64 || role.is_empty() || role.len() > 32 {
return Err(NetError::invalid("exchange", "an invitation names what it is into (`to`) and the role (`as`)"));
}
Ok(())
}
_ => Err(NetError::invalid("exchange", "unknown scope")),
}
}
impl Profile {
pub fn agent(&self, version: &str, label: &str) -> String {
let mut a = format!("{}/{version}", self.name);
if !label.trim().is_empty() {
a += &format!(" ({})", label.trim());
}
a
}
pub fn link(&self, token: &str) -> String {
if let Some(b) = token.strip_prefix(self.offer_prefix) {
format!("{}{b}", self.join_link)
} else if let Some(b) = token.strip_prefix(self.answer_prefix) {
format!("{}{b}", self.answer_link)
} else {
token.to_string()
}
}
pub fn unlink(&self, input: &str) -> String {
let s: String = input.split_whitespace().collect();
if let Some(b) = s.strip_prefix(self.join_link) {
format!("{}{}", self.offer_prefix, b.trim_end_matches('/'))
} else if let Some(b) = s.strip_prefix(self.answer_link) {
format!("{}{}", self.answer_prefix, b.trim_end_matches('/'))
} else {
s
}
}
pub fn is_token(&self, input: &str) -> bool {
let t = input.trim();
t.starts_with(self.token_mark) || t.starts_with(self.link_mark)
}
}