Skip to content

Commit

Permalink
100%, minus Infallable, instrument coverage of new error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grinkers committed Feb 20, 2024
1 parent 1a299fd commit 7f23cc0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 72 deletions.
46 changes: 23 additions & 23 deletions src/deserialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,20 +164,20 @@ where
{
fn deserialize(edn: &Edn) -> Result<Self, Error> {
match edn {
Edn::Vector(_) => Ok(edn
.iter_some()
.ok_or_else(Error::iter)?
Edn::Vector(v) => Ok(v
.0
.iter()
.map(|e| Deserialize::deserialize(e))
.collect::<Result<Self, Error>>()?),
Edn::List(_) => Ok(edn
.iter_some()
.ok_or_else(Error::iter)?
Edn::List(l) => Ok(l
.0
.iter()
.map(|e| Deserialize::deserialize(e))
.collect::<Result<Self, Error>>()?),
#[cfg(feature = "sets")]
Edn::Set(_) => Ok(edn
.iter_some()
.ok_or_else(Error::iter)?
Edn::Set(s) => Ok(s
.0
.iter()
.map(|e| Deserialize::deserialize(e))
.collect::<Result<Self, Error>>()?),
_ => Err(build_deserialize_error(any::type_name::<Self>())),
Expand All @@ -193,9 +193,9 @@ where
{
fn deserialize(edn: &Edn) -> Result<Self, Error> {
match edn {
Edn::Map(_) => edn
.map_iter()
.ok_or_else(Error::iter)?
Edn::Map(m) => m
.0
.iter()
.map(|(key, e)| {
Ok((
key.to_string(),
Expand All @@ -214,9 +214,9 @@ where
{
fn deserialize(edn: &Edn) -> Result<Self, Error> {
match edn {
Edn::Map(_) => edn
.map_iter()
.ok_or_else(Error::iter)?
Edn::Map(m) => m
.0
.iter()
.map(|(key, e)| {
Ok((
key.to_string(),
Expand All @@ -237,11 +237,11 @@ where
{
fn deserialize(edn: &Edn) -> Result<Self, Error> {
match edn {
Edn::Set(_) => edn
.set_iter()
.ok_or_else(Error::iter)?
.map(|e| Deserialize::deserialize(e).map_err(|_| Error::deserialize("HashSet")))
.collect::<Result<Self, Error>>(),
Edn::Set(s) => {
s.0.iter()
.map(|e| Deserialize::deserialize(e).map_err(|_| Error::deserialize("HashSet")))
.collect::<Result<Self, Error>>()
}
_ => Err(build_deserialize_error(any::type_name::<Self>())),
}
}
Expand All @@ -254,9 +254,9 @@ where
{
fn deserialize(edn: &Edn) -> Result<Self, Error> {
match edn {
Edn::Set(_) => edn
.set_iter()
.ok_or_else(Error::iter)?
Edn::Set(s) => s
.0
.iter()
.map(|e| Deserialize::deserialize(e).map_err(|_| Error::deserialize("BTreeSet")))
.collect::<Result<Self, Error>>(),
_ => Err(build_deserialize_error(any::type_name::<Self>())),
Expand Down
27 changes: 13 additions & 14 deletions src/deserialize/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::edn::{Edn, List, Map, Vector};

const DELIMITERS: [char; 8] = [',', ']', '}', ')', ';', '(', '[', '{'];

#[derive(Debug)]
struct Walker<'w> {
slice: &'w str,
ptr: usize,
Expand Down Expand Up @@ -517,20 +516,20 @@ fn parse_number(lit: &str) -> Result<Edn, Code> {
}
};

match number {
n if (n.contains('E') || n.contains('e')) && n.parse::<f64>().is_ok() => {
Ok(Edn::Double(n.parse::<f64>()?.into()))
}
n if u64::from_str_radix(&n, radix.into()).is_ok() => {
Ok(Edn::UInt(u64::from_str_radix(&n, radix.into())?))
}
n if i64::from_str_radix(&n, radix.into()).is_ok() => {
Ok(Edn::Int(i64::from_str_radix(&n, radix.into())?))
}
n if n.parse::<f64>().is_ok() => Ok(Edn::Double(n.parse::<f64>()?.into())),
n if num_den_from_slice(&n).is_some() => Ok(Edn::Rational(num_den_from_slice(n).unwrap())),
_ => Err(Code::InvalidNumber),
if let Ok(n) = u64::from_str_radix(&number, radix.into()) {
return Ok(Edn::UInt(n));
}
if let Ok(n) = i64::from_str_radix(&number, radix.into()) {
return Ok(Edn::Int(n));
}
if let Ok(n) = number.parse::<f64>() {
return Ok(Edn::Double(n.into()));
}
if let Some((n, d)) = num_den_from_slice(&number) {
return Ok(Edn::Rational((n, d)));
}

Err(Code::InvalidNumber)
}

#[inline]
Expand Down
31 changes: 0 additions & 31 deletions src/edn/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub enum Code {
InvalidKeyword,
InvalidNumber,
InvalidRadix(Option<u8>),
ParseNumber(ParseNumber),
UnexpectedEOF,
UnmatchedDelimiter(char),

Expand All @@ -31,22 +30,12 @@ pub enum Code {
/// Deserialize errors
Convert(&'static str),

/// Navigation errors
Iter,

/// Type conversion errors
TryFromInt(num::TryFromIntError),
#[doc(hidden)]
Infallable(), // Makes the compiler happy for converting u64 to u64 and i64 to i64
}

#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ParseNumber {
ParseIntError(num::ParseIntError),
ParseFloatError(num::ParseFloatError),
}

impl Error {
pub(crate) const fn deserialize(conv_type: &'static str) -> Self {
Self {
Expand All @@ -56,14 +45,6 @@ impl Error {
ptr: None,
}
}
pub(crate) const fn iter() -> Self {
Self {
code: Code::Iter,
line: None,
column: None,
ptr: None,
}
}
}

impl Debug for Error {
Expand All @@ -76,18 +57,6 @@ impl Debug for Error {
}
}

impl From<num::ParseIntError> for Code {
fn from(e: num::ParseIntError) -> Self {
Self::ParseNumber(ParseNumber::ParseIntError(e))
}
}

impl From<num::ParseFloatError> for Code {
fn from(e: num::ParseFloatError) -> Self {
Self::ParseNumber(ParseNumber::ParseFloatError(e))
}
}

impl From<convert::Infallible> for Error {
fn from(_: convert::Infallible) -> Self {
Self {
Expand Down
8 changes: 4 additions & 4 deletions src/edn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl From<f64> for Double {

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "sets", derive(Eq, PartialOrd, Ord))]
pub struct Vector(Vec<Edn>);
pub struct Vector(pub(crate) Vec<Edn>);
impl Vector {
#[must_use]
pub const fn new(v: Vec<Edn>) -> Self {
Expand All @@ -110,7 +110,7 @@ impl Vector {

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "sets", derive(Eq, PartialOrd, Ord))]
pub struct List(Vec<Edn>);
pub struct List(pub(crate) Vec<Edn>);
impl List {
#[must_use]
pub fn new(v: Vec<Edn>) -> Self {
Expand All @@ -130,7 +130,7 @@ impl List {

#[cfg(feature = "sets")]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Set(BTreeSet<Edn>);
pub struct Set(pub(crate) BTreeSet<Edn>);

#[cfg(feature = "sets")]
impl Set {
Expand All @@ -152,7 +152,7 @@ impl Set {

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "sets", derive(Eq, PartialOrd, Ord))]
pub struct Map(BTreeMap<String, Edn>);
pub struct Map(pub(crate) BTreeMap<String, Edn>);
impl Map {
#[must_use]
pub fn new(m: BTreeMap<String, Edn>) -> Self {
Expand Down

0 comments on commit 7f23cc0

Please sign in to comment.