> [snip]
> > > @@ -166,76 +73,128 @@ pub enum CommonInterfaceNameError {
> > > ///
> > > /// FRR itself doesn't enforce any limits, but the kernel does. Linux
> > > only allows interface names
> > > /// to be a maximum of 16 bytes. This is enforced by this struct.
> > > -#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
> > > -pub struct CommonInterfaceName(String);
> > > +#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize,
> > > Deserialize)]
> > > +pub struct InterfaceName(String);
> > >
> > > -impl TryFrom<&str> for CommonInterfaceName {
> > > - type Error = CommonInterfaceNameError;
> > > +impl TryFrom<&str> for InterfaceName {
> > > + type Error = InterfaceNameError;
> > >
> > > fn try_from(value: &str) -> Result<Self, Self::Error> {
> > > Self::new(value)
> > > }
> > > }
> > >
> > > -impl TryFrom<String> for CommonInterfaceName {
> > > - type Error = CommonInterfaceNameError;
> > > +impl TryFrom<String> for InterfaceName {
> > > + type Error = InterfaceNameError;
> > >
> > > fn try_from(value: String) -> Result<Self, Self::Error> {
> > > Self::new(value)
> > > }
> > > }
> > >
> > > -impl CommonInterfaceName {
> > > - pub fn new<T: AsRef<str> + Into<String>>(s: T) -> Result<Self,
> > > CommonInterfaceNameError> {
> > > +impl InterfaceName {
> > > + pub fn new<T: AsRef<str> + Into<String>>(s: T) -> Result<Self,
> > > InterfaceNameError> {
> >
> > Is Into<String> as bound necessary here? Anything we can access as str
> > reference can be converted into a string anyway?
>
> Umm I think so because we need a owned String? This way we don't need to
> force a
> clone.
I now have this overengineered api type :) :
/// Name of a interface, which is common between all protocols.
///
/// FRR itself doesn't enforce any limits, but the kernel does. Linux only
allows interface names
/// to be a maximum of 16 bytes. This is enforced by this struct.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize,
Deserialize)]
pub struct InterfaceName(String);
impl TryFrom<&str> for InterfaceName {
type Error = InterfaceNameError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::validate(s).map(Self::from_str_unchecked)
}
}
impl TryFrom<String> for InterfaceName {
type Error = InterfaceNameError;
fn try_from(value: String) -> Result<Self, Self::Error> {
if Self::validate(&value).is_ok() {
Ok(Self::from_string_unchecked(value))
} else {
Err(InterfaceNameError::TooLong)
}
}
}
impl InterfaceName {
fn validate(s: &str) -> Result<&str, InterfaceNameError> {
if s.len() <= 15 {
Ok(s)
} else {
Err(InterfaceNameError::TooLong)
}
}
fn from_string_unchecked(s: String) -> InterfaceName {
Self(s)
}
fn from_str_unchecked(s: &str) -> InterfaceName {
Self::from_string_unchecked(s.to_string())
}
}
>
> [snip]