Support existing types with custom conversions

Offen
#751 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Anfängerfreundlichkeit
30/100
Issue-Typ
Feature
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
rust
Bereich
compilers, tooling

Rechercherichtung

Beginne mit dem rustdoc von import_types! und dem build.rs-Beispiel in den typify-Quellen. Vergleiche das dokumentierte Verhalten von with_conversion und with_replacement, einschließlich ihrer Kombinationen, und verfolge, wie generierte Felder serde-Attribute erhalten. Done sollte ein vorhandenes std::time::Duration-Feld mit benutzerdefinierter String-Konvertierung unterstützen, ohne schema-spezifische Rust-Typen zu erfordern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Rust
Sterne
898
Forks
114
Ø Merge
4 Std. 18 Min.
Gemergte PRs (30 T.)
14

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus oxidecomputer/typify

Alle Issues in oxidecomputer/typify

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.