Skip to content

Commit

Permalink
Permit exprs in serde::format_description!
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Dec 3, 2024
1 parent 6b43b44 commit 4404638
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
10 changes: 10 additions & 0 deletions tests/serde/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ use time::{serde, Date, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
const ISO_FORMAT: Iso8601<{ iso8601::Config::DEFAULT.encode() }> =
Iso8601::<{ iso8601::Config::DEFAULT.encode() }>;
time::serde::format_description!(my_format, OffsetDateTime, ISO_FORMAT);
time::serde::format_description!(
my_format2,
OffsetDateTime,
Iso8601::<{ iso8601::Config::DEFAULT.encode() }>
);

serde::format_description!(
offset_dt_format,
Expand All @@ -37,6 +42,11 @@ serde::format_description!(
const TIME_FORMAT_ALT: &[BorrowedFormatItem<'_>] =
time::macros::format_description!("[hour]:[minute]");
serde::format_description!(time_format_alt, Time, TIME_FORMAT_ALT);
serde::format_description!(
time_format_alt2,
Time,
time::macros::format_description!("[hour]:[minute]")
);

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct TestCustomFormat {
Expand Down
17 changes: 1 addition & 16 deletions time-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,7 @@ pub fn serde_format_description(input: TokenStream) -> TokenStream {
Some(_) => {
let tokens = tokens.collect::<TokenStream>();
let tokens_string = tokens.to_string();
(
quote! {{
// We can't just do `super::path` because the path could be an absolute
// path. In that case, we'd be generating `super::::path`, which is invalid.
// Even if we took that into account, it's not possible to know if it's an
// external crate, which would just require emitting `path` directly. By
// taking this approach, we can leave it to the compiler to do the actual
// resolution.
mod __path_hack {
pub(super) use super::super::*;
pub(super) use #S(tokens) as FORMAT;
}
__path_hack::FORMAT
}},
tokens_string,
)
(tokens, tokens_string)
}
None => return Err(Error::UnexpectedEndOfInput),
};
Expand Down
2 changes: 2 additions & 0 deletions time-macros/src/serde_format_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ pub(crate) fn build(

quote! {
mod #(mod_name) {
use super::*;
// TODO Remove the prefix, forcing the user to import the type themself.
use ::time::#(ty) as __TimeSerdeType;

const fn description() -> impl #S(fd_traits) {
Expand Down

0 comments on commit 4404638

Please sign in to comment.