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

draft proposal for how we might add the x-rust-type extension #47

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ahl
Copy link

@ahl ahl commented May 11, 2024

@sunshowers I'm posting this as an artifact for us to discuss next week as your time permits.

It depends on the version of typify that's still unreleased so obviously would need to wait for that.

@ahl ahl requested a review from sunshowers May 11, 2024 03:19
Comment on lines +16 to +31
"format": "uuid",
"x-rust-type": {
"crate": "newtype-uuid",
"parameters": [
{
"not": true,
"x-rust-type": {
"crate": "my-crate",
"path": "my_crate::MyKind",
"version": "0.1.0"
}
}
],
"path": "newtype_uuid::TypedUuid",
"version": "1.2.0"
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this might be the most relevant portion to review

Copy link
Collaborator

@sunshowers sunshowers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome!

Comment on lines +23 to +46
fn json_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::SchemaObject {
subschemas: Some(
schemars::schema::SubschemaValidation {
not: Some(schemars::schema::Schema::Bool(true).into()),
..Default::default()
}
.into(),
),
extensions: [(
"x-rust-type".to_string(),
serde_json::json!({
"crate": "my-crate",
"version": "0.1.0",
"path": "my_crate::MyKind",
}),
)]
.into_iter()
.collect(),
..Default::default()
}
.into()
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. Is there a plan to make this easier to define? Wonder if we should revive @hawkw's proc macro.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's something I wanted to talk about. I could imagine a derive macro, but it might be less rope for consumers to instead export something like the impl_typed_uuid_kind macro you suggest people write. As you noted, the derive macro requires a few inputs; this change would require more (crate name, path to the type)--I think that as derive macros grow mandatory attributes they start feeling like they should be function-style macros instead.

@ahl
Copy link
Author

ahl commented Jun 28, 2024

Updating this:

Rain's suggestion for a macro to define these types was something along these lines:

impl_typed_uuid_kind! {
    json_schema_settings = {
        crate = "my-crate",
        path = "my_crate",
    };

    Collection => { version = "0.1.0", tag = "collection", kind = CollectionKind, short = CollectionUuid },
    Downstairs => { version = "0.1.0", tag = "downstairs", kind = DownstairsKind, short = DownstairsUuid },
    // ...
}

We might simplify that a bit to make it easier to parse with serde_tokenstream, but that's the general idea.

@sunshowers
Copy link
Collaborator

This would still be wonderful.

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

Successfully merging this pull request may close these issues.

2 participants