aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/uri.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoding/uri.rs')
-rw-r--r--src/encoding/uri.rs41
1 files changed, 38 insertions, 3 deletions
diff --git a/src/encoding/uri.rs b/src/encoding/uri.rs
index a56cad4..db20700 100644
--- a/src/encoding/uri.rs
+++ b/src/encoding/uri.rs
@@ -1,21 +1,56 @@
+use anyhow::anyhow;
use std::{fmt::Display, str::FromStr};
#[derive(Debug, Clone)]
pub struct Uri {
- pub content: String,
+ pub protocol: String,
+ pub localpart: Option<String>,
+ pub addr: String,
+ pub params: String,
}
impl Display for Uri {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- write!(f, "{}", self.content)?;
+ let Self {
+ protocol,
+ localpart,
+ addr,
+ params,
+ } = self;
+ write!(
+ f,
+ "{protocol}:{}{addr}{}",
+ if let Some(localpart) = localpart {
+ format!("{localpart}@")
+ } else {
+ "".to_string()
+ },
+ if params.is_empty() {
+ "".to_string()
+ } else {
+ format!(";{params}")
+ }
+ )?;
Ok(())
}
}
impl FromStr for Uri {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
+ eprintln!("{s:?}");
+ let (pr, s) = s.split_once(":").ok_or(anyhow!("protocol sep"))?;
+ let (lp, s) = s.split_once("@").unwrap_or(("", s));
+ let (addr, params) = s.split_once(";").unwrap_or((s, ""));
+
Ok(Self {
- content: s.to_string(),
+ addr: addr.to_owned(),
+ localpart: if lp.is_empty() {
+ None
+ } else {
+ Some(lp.to_string())
+ },
+ params: params.to_string(),
+ protocol: pr.to_string(),
})
}
}