From 96daa5ca003a677ac65c52b0b75f89c7287fdd84 Mon Sep 17 00:00:00 2001 From: pantonshire Date: Wed, 14 Sep 2022 19:20:21 +0100 Subject: [PATCH] strings: refactor error types This patch replaces the error type for `FixedString` and removes the re-exports of the various string error types in the `strings` module. --- src/strings/fixed.rs | 45 +++++++++++++++++--------------------------- src/strings/mod.rs | 4 ++-- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/strings/fixed.rs b/src/strings/fixed.rs index db4020c..db67641 100644 --- a/src/strings/fixed.rs +++ b/src/strings/fixed.rs @@ -7,13 +7,25 @@ use core::{ str, }; +#[derive(Debug)] +pub struct LengthError; + +impl fmt::Display for LengthError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "invalid string length for `FixedString`") + } +} + +#[cfg(feature = "std")] +impl std::error::Error for LengthError {} + pub struct FixedString { buf: [u8; N], } impl FixedString { #[inline] - pub fn new(s: &str) -> Result { + pub fn new(s: &str) -> Result { // SAFETY: // A `&str` is always valid UTF-8. unsafe { Self::from_raw_slice(s.as_bytes()) } @@ -22,15 +34,12 @@ impl FixedString { /// # Safety /// The provided byte slice must be valid UTF-8. #[inline] - pub unsafe fn from_raw_slice(bytes: &[u8]) -> Result { + pub unsafe fn from_raw_slice(bytes: &[u8]) -> Result { match bytes.try_into() { // SAFETY: // The caller is reponsible for ensuring that the provided bytes are valid UTF-8. Ok(bytes) => unsafe { Ok(Self::from_raw_array(bytes)) }, - Err(_) => Err(Error { - expected_len: N, - actual_len: bytes.len(), - }), + Err(_) => Err(LengthError), } } @@ -116,7 +125,7 @@ impl borrow::BorrowMut for FixedString { } impl str::FromStr for FixedString { - type Err = Error; + type Err = LengthError; #[inline] fn from_str(s: &str) -> Result { @@ -125,7 +134,7 @@ impl str::FromStr for FixedString { } impl<'a, const N: usize> TryFrom<&'a str> for FixedString { - type Error = Error; + type Error = LengthError; #[inline] fn try_from(value: &'a str) -> Result { @@ -175,26 +184,6 @@ impl fmt::Display for FixedString { } } -#[derive(Debug)] -pub struct Error { - expected_len: usize, - actual_len: usize, -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "expected {} bytes of string data, found {} bytes", - self.expected_len, - self.actual_len - ) - } -} - -#[cfg(feature = "std")] -impl std::error::Error for Error {} - #[cfg(test)] mod tests { use super::FixedString; diff --git a/src/strings/mod.rs b/src/strings/mod.rs index 3825407..50a04df 100644 --- a/src/strings/mod.rs +++ b/src/strings/mod.rs @@ -3,7 +3,7 @@ pub mod capped; #[cfg(feature = "alloc")] pub mod inlining; -pub use fixed::{FixedString, Error as FixedStringError}; -pub use capped::{CappedString, CapacityError as CappedStringError}; +pub use fixed::FixedString; +pub use capped::CappedString; #[cfg(feature = "alloc")] pub use inlining::{InliningString, InliningString23};