Feature request: enabling grammar typing for sequences via deserialize

Aperta
#578 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
backend

Direzione di ricerca

Start by reading the deserializer’s sequence handling, especially SeqItemDeserializer::deserialize_seq, and compare it with the serializer’s flattening behavior. The issue describes two changes: trying None for remaining tuple elements at sequence end and recursively flattening nested sequences; investigate the infinite-loop risk for vectors of Option and other design effects before proposing an approach. Done means the example round-trips while sequence termination and nested sequences behave safely.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement serde

This issue is to gauge interest in being able to validate the variadic layout of a sequence via tuple elements that are Option

I have a use case to generate schema definitions with sequences defined by a grammar. Given that it's already possible to enforce order, and serializing has builtin flattening of options and sequences, there are two key features for deserializing missing that would fully enable building types to validate a sequence grammar.

Deserializing end of sequence for a tuple should try to deserialize as visiting None for each remaining element of the tuple sequence.

Deserializing a sequence of sequences (of sequences...*) should recursively flatten.

Both these features are already present implicitly in the serializer, this would just be making the deserializer match.

This would allow for examples like below to work

use quick_xml::{de::from_str, se::to_string};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
enum ABChoice {
    A,
    B,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
enum CDChoice {
    C,
    D,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
struct Root {
    #[serde(rename = "$value")]
    content: (Vec<ABChoice>, Vec<CDChoice>, Option<Vec<ABChoice>>),
}
let root = Root {
    content: (
        vec![ABChoice::A, ABChoice::B],
        vec![CDChoice::C],
        Some(vec![ABChoice::A]),
    ),
};
let serialized = to_string(&root).unwrap();
dbg!(&serialized);
let deserialized = from_str(&serialized).unwrap();
assert_eq!(root, deserialized);

Currently serializing this produces the string

<Root><A/><B/><C/><A/></Root>

Mirroring this in the deserializer is desirable.

I've managed to get a hacky implementation of this to work sending all cases that would stop iteration to first try and deserialize as the DeserializeSeed with a deserializer that can only call visit_none, however this would lead to the case where a vector of Options produces an infinite loop so there's still some design work to be done there.

With regards to recursive sequence flattening, I suspect the offender is <SeqItemDeserializer as de::Deserializer>::deserialize_seq, however just making this recursive seems to have other potential ramifications associated. As such, design input would be appreciated there as well.

I'm very happy to do the work myself. However, I would like to consult, firstly, if this would be something that can be upstreamed, and secondly if there are any pitfalls in this area I should be aware of from a design standpoint, particularly that would prevent it getting upstreamed.

Lingua principale
Rust
Stelle
1.6k
Fork
299
Merge medio
2g 4h
PR unite (30g)
9

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tafia/quick-xml

Tutte le issue di tafia/quick-xml

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.