oxidecomputer / oxidecomputer/typify

Support existing types with custom conversions

Open
#751 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
898
Forks
114
Avg merge
4h 18m
Merged PRs (30d)
14

Description

I'm trying to use fields of type std::time::Duration and initialize them with strings like "500 ms". It doesn't appear to be possible right now.

I converted my code to build.rs so I can explore all documented functions. The import_types! documentation is unclear and insufficient. Actually, there is no example of build.rs in the rustdoc documentation, but I found an example in the typify sources.

Only with_replacement lets me replace a generated type with the existing one (std::time::Duration). The documentation for with_conversion appears to promise that behavior, but a new type is always generated. Combinations of with_replacement and other with_* don't seem to work.

Unfortunately, with_replacement doesn't let me specify custom serde for the field. Look e.g. for #[serde(with = "humantime_serde")] at https://docs.rs/humantime-serde/1.1.1/humantime_serde/ - it's specified on the field, not on the whole struct.

I tried some things with x-rust-type without any success. In any case, I don't see it as a clean approach. It should be possible for the schema to be Rust-agnostic and still support readable strings for duration.

Likewise, I don't want to use numbers for duration. It's too easy to confuse seconds with milliseconds.

Currently supported formats ("uuid", "date", "ip" etc) all support conversion from string. The problem with std::time::Duration is that it's standard (i.e. many std functions use it) but cannot be constructed from a string. So a different mechanism is needed.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the import_types! rustdoc and the build.rs example in the typify sources. Compare the documented behavior of with_conversion and with_replacement, including their combinations, and trace how generated fields receive serde attributes. Done should support an existing std::time::Duration field with custom string conversion without requiring schema-specific Rust types.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.