Implement FromStr for identifier types

This commit is contained in:
Jonas Platte 2020-08-11 02:15:14 +02:00
parent dffa60d70f
commit 3454a0e750
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
3 changed files with 26 additions and 0 deletions

View File

@ -54,6 +54,14 @@ macro_rules! common_impls {
} }
} }
impl ::std::str::FromStr for $id {
type Err = crate::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
$try_from(s)
}
}
impl ::std::convert::TryFrom<&str> for $id { impl ::std::convert::TryFrom<&str> for $id {
type Error = crate::Error; type Error = crate::Error;

View File

@ -4,6 +4,7 @@ use std::{
cmp::Ordering, cmp::Ordering,
convert::TryFrom, convert::TryFrom,
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
str::FromStr,
}; };
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
@ -237,6 +238,14 @@ where
Ok(version) Ok(version)
} }
impl FromStr for RoomVersionId {
type Err = crate::Error;
fn from_str(s: &str) -> Result<Self, Error> {
try_from(s)
}
}
impl TryFrom<&str> for RoomVersionId { impl TryFrom<&str> for RoomVersionId {
type Error = crate::Error; type Error = crate::Error;

View File

@ -4,6 +4,7 @@ use std::{
convert::TryFrom, convert::TryFrom,
fmt::{self, Display}, fmt::{self, Display},
mem, mem,
str::FromStr,
}; };
use ruma_identifiers_validation::server_name::validate; use ruma_identifiers_validation::server_name::validate;
@ -96,6 +97,14 @@ impl<'a> TryFrom<&'a str> for &'a ServerName {
} }
} }
impl FromStr for Box<ServerName> {
type Err = crate::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
try_from(s)
}
}
impl TryFrom<&str> for Box<ServerName> { impl TryFrom<&str> for Box<ServerName> {
type Error = crate::Error; type Error = crate::Error;