Merge branch 'from-str' into 'master'
implement From<Jid> on String See merge request !1
This commit is contained in:
commit
1f592e101a
1 changed files with 21 additions and 0 deletions
21
src/lib.rs
21
src/lib.rs
|
@ -34,6 +34,22 @@ pub struct Jid {
|
||||||
pub resource: Option<String>,
|
pub resource: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<Jid> for String {
|
||||||
|
fn from(jid: Jid) -> String {
|
||||||
|
let mut string = String::new();
|
||||||
|
if let Some(ref node) = jid.node {
|
||||||
|
string.push_str(node);
|
||||||
|
string.push('@');
|
||||||
|
}
|
||||||
|
string.push_str(&jid.domain);
|
||||||
|
if let Some(ref resource) = jid.resource {
|
||||||
|
string.push('/');
|
||||||
|
string.push_str(resource);
|
||||||
|
}
|
||||||
|
string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Jid {
|
impl fmt::Display for Jid {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
// TODO: may need escaping
|
// TODO: may need escaping
|
||||||
|
@ -313,4 +329,9 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(Jid::from_str("a/b@c"), Ok(Jid::domain_with_resource("a", "b@c")));
|
assert_eq!(Jid::from_str("a/b@c"), Ok(Jid::domain_with_resource("a", "b@c")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialise() {
|
||||||
|
assert_eq!(String::from(Jid::full("a", "b", "c")), String::from("a@b/c"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue