Give DNameSlice::strip_suffix() a proper error.

This commit is contained in:
Martin Hoffmann
2016-11-30 16:38:03 +01:00
parent 572fd92ddf
commit 585fade7bb
2 changed files with 31 additions and 8 deletions
+2 -1
View File
@@ -63,7 +63,8 @@ pub use self::dname::DName;
pub use self::iter::{NameLabels, NameLabelettes};
pub use self::label::{Label, LabelBuf, LabelContent, Labelette, LabelIter};
pub use self::parsed::ParsedDName;
pub use self::plain::{DNameBuf, DNameSlice, FromStrError, PushError};
pub use self::plain::{DNameBuf, DNameSlice, FromStrError, PushError,
StripSuffixError};
mod builder;
mod dname;
+29 -7
View File
@@ -197,7 +197,8 @@ impl DNameSlice {
///
/// This fails if `base` isn’t a suffix of `self`.
pub fn strip_suffix<'a, N: DName>(&'a self, base: &'a N)
-> Result<Cow<'a, Self>, ()> {
-> Result<Cow<'a, Self>,
StripSuffixError> {
let mut self_iter = self.labelettes();
let mut base_iter = base.labelettes();
loop {
@@ -210,11 +211,11 @@ impl DNameSlice {
let self_ltte = match self_iter.next_back() {
Some(ltte) => ltte,
None => {
return Err(()) // XXX Not a suffix
return Err(StripSuffixError)
}
};
if base_ltte != self_ltte {
return Err(()) // XXX Not a suffix
return Err(StripSuffixError)
}
}
}
@@ -951,10 +952,6 @@ impl error::Error for PushError {
fn description(&self) -> &str {
"adding a label would exceed the size limit"
}
fn cause(&self) -> Option<&error::Error> {
None
}
}
impl fmt::Debug for PushError {
@@ -970,6 +967,31 @@ impl fmt::Display for PushError {
}
//------------ StripSuffixError ----------------------------------------------
/// An attempt was made to strip a suffix that wasn’t actually a suffix.
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct StripSuffixError;
impl error::Error for StripSuffixError {
fn description(&self) -> &str {
"suffix not found"
}
}
impl fmt::Debug for StripSuffixError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
"StripSuffixError".fmt(f)
}
}
impl fmt::Display for StripSuffixError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
"suffix not found".fmt(f)
}
}
//============ Testing ======================================================
#[cfg(test)]