chore (backend-rs): impl FromStr and Display for Acct
This commit is contained in:
parent
95fd20a46f
commit
612ce48f44
1 changed files with 53 additions and 28 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
use std::fmt;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
#[crate::export(object)]
|
#[crate::export(object)]
|
||||||
pub struct Acct {
|
pub struct Acct {
|
||||||
|
@ -5,38 +8,57 @@ pub struct Acct {
|
||||||
pub host: Option<String>,
|
pub host: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[crate::export]
|
impl FromStr for Acct {
|
||||||
pub fn string_to_acct(acct: &str) -> Acct {
|
type Err = ();
|
||||||
let split: Vec<&str> = if let Some(stripped) = acct.strip_prefix('@') {
|
|
||||||
stripped
|
|
||||||
} else {
|
|
||||||
acct
|
|
||||||
}
|
|
||||||
.split('@')
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Acct {
|
/// This never throw errors. Feel free to `.unwrap()` the result.
|
||||||
username: split[0].to_string(),
|
fn from_str(value: &str) -> Result<Self, Self::Err> {
|
||||||
host: if split.len() == 1 {
|
let split: Vec<&str> = if let Some(stripped) = value.strip_prefix('@') {
|
||||||
None
|
stripped
|
||||||
} else {
|
} else {
|
||||||
Some(split[1].to_string())
|
value
|
||||||
},
|
}
|
||||||
|
.split('@')
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
username: split[0].to_string(),
|
||||||
|
host: if split.len() == 1 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(split[1].to_string())
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Acct {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
let result = match &self.host {
|
||||||
|
Some(host) => format!("{}@{}", self.username, host),
|
||||||
|
None => self.username.clone(),
|
||||||
|
};
|
||||||
|
write!(f, "{result}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[crate::ts_only_warn("Use `acct.parse().unwrap()` or `Acct::from_str(acct).unwrap()` instead.")]
|
||||||
|
#[crate::export]
|
||||||
|
pub fn string_to_acct(acct: &str) -> Acct {
|
||||||
|
Acct::from_str(acct).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[crate::ts_only_warn("Use `acct.to_string()` instead.")]
|
||||||
#[crate::export]
|
#[crate::export]
|
||||||
pub fn acct_to_string(acct: &Acct) -> String {
|
pub fn acct_to_string(acct: &Acct) -> String {
|
||||||
match &acct.host {
|
acct.to_string()
|
||||||
Some(host) => format!("{}@{}", acct.username, host),
|
|
||||||
None => acct.username.clone(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod unit_test {
|
mod unit_test {
|
||||||
use super::{acct_to_string, string_to_acct, Acct};
|
use super::Acct;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_acct_to_string() {
|
fn test_acct_to_string() {
|
||||||
|
@ -49,10 +71,10 @@ mod unit_test {
|
||||||
host: None,
|
host: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(acct_to_string(&remote_acct), "firefish@example.com");
|
assert_eq!(remote_acct.to_string(), "firefish@example.com");
|
||||||
assert_ne!(acct_to_string(&remote_acct), "mastodon@example.com");
|
assert_ne!(remote_acct.to_string(), "mastodon@example.com");
|
||||||
assert_eq!(acct_to_string(&local_acct), "MisakaMikoto");
|
assert_eq!(local_acct.to_string(), "MisakaMikoto");
|
||||||
assert_ne!(acct_to_string(&local_acct), "ShiraiKuroko");
|
assert_ne!(local_acct.to_string(), "ShiraiKuroko");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -66,9 +88,12 @@ mod unit_test {
|
||||||
host: None,
|
host: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(string_to_acct("@firefish@example.com"), remote_acct);
|
assert_eq!(
|
||||||
assert_eq!(string_to_acct("firefish@example.com"), remote_acct);
|
Acct::from_str("@firefish@example.com").unwrap(),
|
||||||
assert_eq!(string_to_acct("@MisakaMikoto"), local_acct);
|
remote_acct
|
||||||
assert_eq!(string_to_acct("MisakaMikoto"), local_acct);
|
);
|
||||||
|
assert_eq!(Acct::from_str("firefish@example.com").unwrap(), remote_acct);
|
||||||
|
assert_eq!(Acct::from_str("@MisakaMikoto").unwrap(), local_acct);
|
||||||
|
assert_eq!(Acct::from_str("MisakaMikoto").unwrap(), local_acct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue