fzyzcjy / flutter_rust_bridge

Flutter/Dart <-> Rust binding generator, feature-rich, but seamless and simple.
https://fzyzcjy.github.io/flutter_rust_bridge/
MIT License
3.61k stars 254 forks source link

Fix warning use of deprecated associated function `chrono::NaiveDateTime::from_timestamp_micros` #1886

Closed VladTheJunior closed 14 hours ago

VladTheJunior commented 1 month ago

When generating code for struct which contains chrono::NaiveDateTime compiler get warnings about conversion to NaiveDateTime. It should use DateTime::from_timestamp_micros instead

impl SseDecode for chrono::NaiveDateTime {
    // Codec=Sse (Serialization based), see doc to use other codecs
    fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
        let mut inner = <i64>::sse_decode(deserializer);
        return chrono::NaiveDateTime::from_timestamp_micros(inner) // <-- this line
            .expect("invalid or out-of-range datetime");
    }
}
fzyzcjy commented 1 month ago

Looks reasonable to avoid this warning. But I wonder, if someone is really using the type NaiveDateTime (e.g. the code you show), I guess it is not possible to call DateTime's constructor, since that one may return a datetime instead of naivedatetime?

VladTheJunior commented 1 month ago

Yes, but DateTime has these methods:

pub const fn naive_utc(&self) -> NaiveDateTime
pub fn naive_local(&self) -> NaiveDateTime

https://docs.rs/chrono/latest/chrono/struct.DateTime.html#method.naive_utc https://docs.rs/chrono/latest/chrono/struct.DateTime.html#method.naive_local

Btw deprecated method is also used here

impl SseDecode for chrono::DateTime<chrono::Local> {
    // Codec=Sse (Serialization based), see doc to use other codecs
    fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
        let mut inner = <i64>::sse_decode(deserializer);
        return chrono::DateTime::<chrono::Local>::from(
            chrono::DateTime::<chrono::Utc>::from_naive_utc_and_offset(
                chrono::NaiveDateTime::from_timestamp_micros(inner)
                    .expect("invalid or out-of-range datetime"),
                chrono::Utc,
            ),
        );
    }
}
fzyzcjy commented 1 month ago

I see. Feel free to PR, alternatively I will fix it in the next batch!

VladTheJunior commented 1 month ago

I will wait the next version, ty

fzyzcjy commented 3 days ago

Oops I somehow missed it... Will fix it now