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

2.x.x: chrono types shouldn't be just strings #293

Closed
cognivore opened this issue Nov 22, 2024 · 4 comments
Closed

2.x.x: chrono types shouldn't be just strings #293

cognivore opened this issue Nov 22, 2024 · 4 comments

Comments

@cognivore
Copy link

Why?

When I enabled chrono feature flag, I expected something more fine-grained than string as the output type.
At least some type which denotes the original intent of time-related code, perhaps something wrapped in newtype even!

Observed outcome was that every chrono type I can fathom translates into string.

What?

  • Have a discussion about the most reasonable and portable type representation for time-related types
  • Have it implemented
  • Have "stringly-typed" chrono available as an escape-hatch.
@cognivore
Copy link
Author

This will all be solved after #193 is implemented.

@oscartbeaumont
Copy link
Member

oscartbeaumont commented Nov 22, 2024

What does Serde serialize them as? Last I checked they are all treated as strings.

We have no choice but to copy Serde because Specta only handles types not runtime behavior. Just cause we say the type is Date doesn't actually make it one. If you notice we don't match Serde behavior on any types then that's definitely a bug.

I am hoping to get rspc/Tauri Specta working with Date's and large integer types at some point but it's going to require runtime code so I don't think it could just done with the chrono types, maybe it would be like rspc::Date<chrono::...> or something but i'm not even sure how I would get that working at this stage. It would also require both Rust and Typescript runtime integration which I think is too heavy for being a Specta built-in.

Type brands would be helpful but we don't need two tracking issues so i'm inclined to close this.

@cognivore
Copy link
Author

Oh how cringe! I'll open this in serde then I guess? :D

It's really crazy that we have all these great facilities for distinguishing different types of date semantics and we lose them all hoping that the users will parse out the semantics based on the contents of ISO string.

FWIW, I have made the following hack:

#[derive(Debug, Clone, Copy, Serialize, Deserialize, specta::Type)]
pub struct WrappedNaiveDate {
    pub naive_date: NaiveDate,
}

impl From<DateTime<Utc>> for WrappedUtcDateTime {
    fn from(dt: DateTime<Utc>) -> Self {
        Self { utc_date_time: dt }
    }
}

impl From<WrappedUtcDateTime> for DateTime<Utc> {
    fn from(wrapped: WrappedUtcDateTime) -> Self {
        wrapped.utc_date_time
    }
}
#[derive(Debug, Clone, Serialize, Deserialize, specta::Type)]
pub struct Task {
    pub name: String,
    pub technology: Technology,
    pub task_type: TaskType,
    pub description: String,
    #[specta(type = crate::wrapped_chrono::WrappedUtcDateTime)]
    pub last_updated: DateTime<Utc>,
}

I hope that it will be enough to transparently interoperate hither and thither, if it won't be, I'll report in this issue.

Thank you for your feedback, this issue is indeed misplaced, so I'll close it, but I'll post my findings here so that people looking up for the reasons for this design will be able to find it and read about things they can do about it.

@oscartbeaumont
Copy link
Member

I'll open this in serde then I guess?

It probally won't get anywhere sadly because Serde copies the JSON spec. If the JSON spec had Date's I think this whole thing would be much nicer but atlas it does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants