time-rs / time

The most used Rust library for date and time handling.
https://time-rs.github.io
Apache License 2.0
1.06k stars 261 forks source link

Allow custom visibility modifier for generated module #597

Closed nui closed 1 year ago

nui commented 1 year ago

time::serde::format_description always generate module without visibility modifier. Would it be possible to add support for this functionality? I see no reason why it have to be private module only.

I want to declare module in private module and publicly re export it but Rust doesn't allow re-exporting private items.

related link: https://docs.rs/time/latest/time/serde/macro.format_description.html

jhpratt commented 1 year ago

Why is it desired to re-export the module?

nui commented 1 year ago

Why is it desired to re-export the module?

I actually don't need re-exporting.

Let say if this module is under crate::serde I couldn't use it from crate::app::handler because this module is private inside crate::serde module by current generated output.

Is this an intended usage of this macro? If so, please add some note on macro documentation.

jhpratt commented 1 year ago

If you want to be able to use it anywhere in the crate, it needs to be at the root level of the crate. That's the same as any other module, and is trivially done with the existing macro.

nui commented 1 year ago

That would pollute root level of crate.

Anyway I found a workaround.

const DATE_TIME_FORMAT: &[FormatItem<'_>] = time::macros::format_description!(
    "hour=[hour], minute=[minute]"
);

// Makes a module `mod my_format { ... }`.
serde::format_description!(my_format_impl, OffsetDateTime, DATE_TIME_FORMAT);

// A hack module to exports method generated by above macro
pub mod my_format {
    pub use super::my_format_impl::{deserialize, serialize};
}

Thanks for making this macro, it is pretty useful.

I will keep this issue as is, but feel free to close it.