Skip to content

Commit bed3232

Browse files
maintain datetime structure
1 parent ea5424a commit bed3232

File tree

4 files changed

+65
-19
lines changed

4 files changed

+65
-19
lines changed

src/datetime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,14 @@ impl crate::DateTime {
396396
pub fn try_to_rfc3339_string(self) -> Result<String> {
397397
self.to_time_0_3()
398398
.format(&Rfc3339)
399-
.map_err(Error::bad_conversion)
399+
.map_err(Error::cannot_format_datetime)
400400
}
401401

402402
/// Convert the given RFC 3339 formatted string to a [`DateTime`], truncating it to millisecond
403403
/// precision.
404404
pub fn parse_rfc3339_str(s: impl AsRef<str>) -> Result<Self> {
405-
let odt =
406-
time::OffsetDateTime::parse(s.as_ref(), &Rfc3339).map_err(Error::invalid_value)?;
405+
let odt = time::OffsetDateTime::parse(s.as_ref(), &Rfc3339)
406+
.map_err(Error::invalid_datetime_value)?;
407407
Ok(Self::from_time_0_3(odt))
408408
}
409409

src/datetime/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,16 @@ impl DateTimeBuilder<Year, Month, Day> {
174174
///
175175
/// Note: You cannot call `build()` before setting at least the year, month and day.
176176
pub fn build(self) -> Result<DateTime> {
177-
let month = time::Month::try_from(self.month.0).map_err(Error::invalid_value)?;
177+
let month = time::Month::try_from(self.month.0).map_err(Error::invalid_datetime_value)?;
178178
let dt = Date::from_calendar_date(self.year.0, month, self.day.0)
179-
.map_err(Error::invalid_value)?
179+
.map_err(Error::invalid_datetime_value)?
180180
.with_hms_milli(
181181
self.hour.unwrap_or(0),
182182
self.minute.unwrap_or(0),
183183
self.second.unwrap_or(0),
184184
self.millisecond.unwrap_or(0),
185185
)
186-
.map_err(Error::invalid_value)?;
186+
.map_err(Error::invalid_datetime_value)?;
187187
Ok(DateTime::from_time_private(dt.assume_utc()))
188188
}
189189
}

src/error.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
mod datetime;
12
mod uuid;
23

34
use thiserror::Error;
45

56
use crate::spec::ElementType;
67

8+
pub use datetime::DateTimeErrorKind;
79
pub use uuid::UuidErrorKind;
810

911
pub type Result<T> = std::result::Result<T, Error>;
@@ -38,19 +40,28 @@ impl std::fmt::Display for Error {
3840
#[derive(Clone, Debug, Error)]
3941
#[non_exhaustive]
4042
pub enum ErrorKind {
41-
/// An error occurred when converting a BSON type to an external format.
42-
#[error("Bad conversion: {message}")]
43-
BadConversion { message: String },
43+
/// An error related to the [`DateTime`](crate::DateTime) type occurred.
44+
#[error("A DateTime-related error occurred: {kind}")]
45+
DateTime {
46+
/// The kind of error that occurred.
47+
kind: DateTimeErrorKind,
48+
},
4449

4550
/// An error occurred when attempting to parse a value from an external format to BSON.
4651
#[error("Invalid value: {message}")]
4752
#[non_exhaustive]
48-
InvalidValue { message: String },
53+
InvalidValue {
54+
/// A message describing the error.
55+
message: String,
56+
},
4957

5058
/// Malformed BSON bytes were encountered.
51-
#[error("Malformed BSON: {message}")]
59+
#[error("Malformed BSON bytes: {message}")]
5260
#[non_exhaustive]
53-
MalformedBytes { message: String },
61+
MalformedBytes {
62+
/// A message describing the error.
63+
message: String,
64+
},
5465

5566
/// Invalid UTF-8 bytes were encountered.
5667
#[error("Invalid UTF-8")]
@@ -153,13 +164,6 @@ impl Error {
153164
.into()
154165
}
155166

156-
pub(crate) fn bad_conversion(message: impl ToString) -> Self {
157-
ErrorKind::BadConversion {
158-
message: message.to_string(),
159-
}
160-
.into()
161-
}
162-
163167
#[cfg(test)]
164168
pub(crate) fn is_value_access_not_present(&self) -> bool {
165169
matches!(

src/error/datetime.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use thiserror::Error as ThisError;
2+
3+
use crate::error::{Error, ErrorKind};
4+
5+
#[derive(Clone, Debug, ThisError)]
6+
pub enum DateTimeErrorKind {
7+
/// The `DateTime` could not be formatted.
8+
#[error("{message}")]
9+
#[non_exhaustive]
10+
CannotFormat {
11+
/// A message describing the error.
12+
message: String,
13+
},
14+
15+
/// An invalid value was provided.
16+
#[error("{message}")]
17+
#[non_exhaustive]
18+
InvalidValue {
19+
/// A message describing the error.
20+
message: String,
21+
},
22+
}
23+
24+
impl Error {
25+
pub(crate) fn invalid_datetime_value(message: impl ToString) -> Self {
26+
ErrorKind::DateTime {
27+
kind: DateTimeErrorKind::InvalidValue {
28+
message: message.to_string(),
29+
},
30+
}
31+
.into()
32+
}
33+
34+
pub(crate) fn cannot_format_datetime(message: impl ToString) -> Self {
35+
ErrorKind::DateTime {
36+
kind: DateTimeErrorKind::CannotFormat {
37+
message: message.to_string(),
38+
},
39+
}
40+
.into()
41+
}
42+
}

0 commit comments

Comments
 (0)