From 585fade7bbbf36b5bdf2df431dcb7b060cc10815 Mon Sep 17 00:00:00 2001 From: Martin Hoffmann Date: Wed, 30 Nov 2016 16:38:03 +0100 Subject: [PATCH] Give DNameSlice::strip_suffix() a proper error. --- src/bits/name/mod.rs | 3 ++- src/bits/name/plain.rs | 36 +++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/bits/name/mod.rs b/src/bits/name/mod.rs index 63db2f3e..a8fe3935 100644 --- a/src/bits/name/mod.rs +++ b/src/bits/name/mod.rs @@ -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; diff --git a/src/bits/name/plain.rs b/src/bits/name/plain.rs index d11e7387..b59e9ce8 100644 --- a/src/bits/name/plain.rs +++ b/src/bits/name/plain.rs @@ -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, ()> { + -> Result, + 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)]