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 {
type Error = crate::Error;

View File

@ -4,6 +4,7 @@ use std::{
cmp::Ordering,
convert::TryFrom,
fmt::{self, Display, Formatter},
str::FromStr,
};
#[cfg(feature = "serde")]
@ -237,6 +238,14 @@ where
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 {
type Error = crate::Error;

View File

@ -4,6 +4,7 @@ use std::{
convert::TryFrom,
fmt::{self, Display},
mem,
str::FromStr,
};
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> {
type Error = crate::Error;