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

Conversion override to usize ignored #743

Closed
proski opened this issue Jan 20, 2025 · 2 comments
Closed

Conversion override to usize ignored #743

proski opened this issue Jan 20, 2025 · 2 comments

Comments

@proski
Copy link

proski commented Jan 20, 2025

I'm using typify for a configuration file, and the configuration it produces is used all over the place. I don't want to deal with conversion errors everywhere, I want to deal with them when the configuration is loaded.

Some of the fields are expected to be of type usize. So I tried the convert keyword:

typify::import_types!(
    schema = "./config/encoder_schema.json",
    convert = {
        {
            type = "integer",
            format = "usize",
        } = usize,
    }
);

The schema has:

        "log_channel_size": {
            "type": "integer",
            "format": "usize",
            "minimum": 0
        },

And yet the generated type is u64, as seen in the output of cargo expand.

I'm using typify 0.3.0.

@proski
Copy link
Author

proski commented Jan 25, 2025

I've just noticed on https://docs.rs/typify/0.3.0/typify/macro.import_types.html

convert: optional map from a JSON schema type defined in $defs to a replacement type

I didn't use $defs. There is no mention of $defs on https://docs.rs/typify/0.3.0/typify/

I'll post an update if I figure out how to do it. I believe the documentation needs improvement.

@proski
Copy link
Author

proski commented Jan 26, 2025

I was confused by the documentation. I was able to have usize fields by using "$ref": "#/definitions/usize" in the schema and Usize = usize inside replace. $defs or definitions are interchangeable, both work as long as there is just one section with definitions.

The documentation needs improvement, but that would be separate issue. Closing this issue.

@proski proski closed this as completed Jan 26, 2025
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

1 participant