Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace thiserror with displaydoc #863

Merged
merged 15 commits into from
Jul 16, 2021
35 changes: 23 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ litemap = { version = "0.2", path = "../../utils/litemap", features = ["serde"]
tinystr = { version = "0.4.5" }
serde = { version = "1.0", features = ["derive"], optional = true }
smallvec = "1.6"
thiserror = "1.0"
displaydoc = { version = "0.2.3", default-features = false }

[dev-dependencies]
criterion = "0.3"
Expand Down
22 changes: 15 additions & 7 deletions components/datetime/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use displaydoc::Display;
use icu_locid::Locale;
use std::convert::TryFrom;
use std::ops::{Add, Sub};
use std::str::FromStr;
use thiserror::Error;
use tinystr::TinyStr8;

#[derive(Error, Debug)]
#[derive(Display, Debug)]
pub enum DateTimeError {
#[error(transparent)]
Parse(#[from] std::num::ParseIntError),
#[error("{field} must be between 0-{max}")]
#[displaydoc("{0}")]
Parse(std::num::ParseIntError),
#[displaydoc("{field} must be between 0-{max}")]
Overflow { field: &'static str, max: usize },
#[error("{field} must be between {min}-0")]
#[displaydoc("{field} must be between {min}-0")]
Underflow { field: &'static str, min: isize },
#[error("Failed to parse time-zone offset")]
#[displaydoc("Failed to parse time-zone offset")]
InvalidTimeZoneOffset,
}

impl std::error::Error for DateTimeError {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: when #[error(transparent)] or #[from] is used, I think thiserror properly implements the source function of std::error::Error. Should we do that here, too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not using .source() and it's not super important (this is, like I said, some of the "old error handling" stuff that will eventually be replaced)


impl From<std::num::ParseIntError> for DateTimeError {
fn from(e: std::num::ParseIntError) -> Self {
DateTimeError::Parse(e)
}
}

/// Representation of a formattable calendar date. Supports dates in any calendar system that uses
/// solar days indexed by an era, year, month, and day.
///
Expand Down
50 changes: 38 additions & 12 deletions components/datetime/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,55 @@
use crate::fields::FieldSymbol;
use crate::pattern;
use crate::skeleton::SkeletonError;
use displaydoc::Display;
use icu_provider::prelude::DataError;
use thiserror::Error;

/// A list of possible error outcomes for the [`DateTimeFormat`](crate::DateTimeFormat) struct.
#[derive(Error, Debug)]
#[derive(Display, Debug)]
pub enum DateTimeFormatError {
/// An error originating from parsing a pattern.
#[error(transparent)]
Pattern(#[from] pattern::Error),
#[displaydoc("{0}")]
Pattern(pattern::Error),
/// An error originating from the [`Write`](std::fmt::Write) trait.
#[error(transparent)]
Format(#[from] std::fmt::Error),
#[displaydoc("{0}")]
Format(std::fmt::Error),
/// An error originating inside of the [`DataProvider`](icu_provider::DataProvider).
#[error(transparent)]
DataProvider(#[from] DataError),
#[displaydoc("{0}")]
DataProvider(DataError),
/// An error originating from a missing field in datetime input.
/// TODO: How can we return which field was missing?
#[error("Missing input field")]
#[displaydoc("Missing input field")]
MissingInputField,
/// An error originating from skeleton matching.
#[error(transparent)]
Skeleton(#[from] SkeletonError),
#[displaydoc("{0}")]
Skeleton(SkeletonError),
/// An error originating from an unsupported field in a datetime format.
#[error("Unsupported field: {0:?}")]
#[displaydoc("Unsupported field: {0:?}")]
UnsupportedField(FieldSymbol),
}

impl std::error::Error for DateTimeFormatError {}

impl From<pattern::Error> for DateTimeFormatError {
fn from(e: pattern::Error) -> Self {
DateTimeFormatError::Pattern(e)
}
}

impl From<DataError> for DateTimeFormatError {
fn from(e: DataError) -> Self {
DateTimeFormatError::DataProvider(e)
}
}

impl From<std::fmt::Error> for DateTimeFormatError {
fn from(e: std::fmt::Error) -> Self {
DateTimeFormatError::Format(e)
}
}

impl From<SkeletonError> for DateTimeFormatError {
fn from(e: SkeletonError) -> Self {
DateTimeFormatError::Skeleton(e)
}
}
8 changes: 5 additions & 3 deletions components/datetime/src/fields/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use displaydoc::Display;
use std::{
cmp::{Ord, PartialOrd},
convert::TryFrom,
};
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
#[derive(Display, Debug, PartialEq)]
pub enum LengthError {
#[error("Invalid length")]
#[displaydoc("Invalid length")]
InvalidLength,
}

impl std::error::Error for LengthError {}

#[derive(Debug, Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]
#[cfg_attr(
feature = "provider_serde",
Expand Down
8 changes: 5 additions & 3 deletions components/datetime/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
mod length;
pub(crate) mod symbols;

use displaydoc::Display;
pub use length::{FieldLength, LengthError};
pub use symbols::*;
use thiserror::Error;

use std::{
cmp::{Ord, PartialOrd},
convert::{TryFrom, TryInto},
};

#[derive(Error, Debug)]
#[derive(Display, Debug)]
pub enum Error {
#[error("Field {0:?} is not a valid length")]
#[displaydoc("Field {0:?} is not a valid length")]
InvalidLength(FieldSymbol),
}

impl std::error::Error for Error {}

#[derive(Debug, Eq, PartialEq, Clone, Copy, Ord, PartialOrd)]
#[cfg_attr(
feature = "provider_serde",
Expand Down
10 changes: 6 additions & 4 deletions components/datetime/src/fields/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::fields::FieldLength;
use displaydoc::Display;
use std::{cmp::Ordering, convert::TryFrom};
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
#[derive(Display, Debug, PartialEq)]
pub enum SymbolError {
/// Unknown field symbol.
#[error("Unknown field symbol: {0}")]
#[displaydoc("Unknown field symbol: {0}")]
Unknown(u8),
/// Invalid character for a field symbol.
#[error("Invalid character for a field symbol: {0}")]
#[displaydoc("Invalid character for a field symbol: {0}")]
Invalid(char),
}

impl std::error::Error for SymbolError {}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(
feature = "provider_serde",
Expand Down
Loading