[Feature request] Enabling support for transitive "#[from]' handling

Aperta
#435 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Non è indicato alcun file o punto di ingresso per i test. Parti dagli esempi annidati di ConversionDbError e MyError ed esamina il comportamento esistente di #[from] del repository; definisci come devono funzionare le conversioni transitive e aggiungi una copertura che dimostri che le varianti annidate vengono convertite nell'errore esterno.

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

Descrizione

I will explain myself with a very simple example :

Say I have this huge error type on my codebase :

#[derive(Error, Debug)]
pub enum MyError {
    #[error("Network error: {0}")]
    Network(String),

    #[error("Conversion Error: {0}")]
    ConversionError(#[from] ConversionDbError),

    // Around 30 other variants

And with


#[derive(Error, Debug)]
pub enum ConversionDbError {
    #[error("UUID conversion error: {0}")]
    Uuid(#[from] uuid::Error),

    #[error("UTF-8 conversion error: {0}")]
    FromUtf8(#[from] std::string::FromUtf8Error),

    #[error("TryFromIntError conversion error: {0}")]
    TryFromInt(#[from] std::num::TryFromIntError),
}

I still need to add the manual conversions in order to forward the error down my enum :

impl From<uuid::Error> for DbError {
    fn from(e: uuid::Error) -> Self {
        Self::ConversionError(ConversionDbError::Uuid(e))
    }
}
// and the 2 others

This kindof takes away my goal from importing the thiserror crate, which is to reduce boilerplate redundant code to the maximum. Of course, the code above isn't my production code. My code is so huge that this kind of feature will make it way more human-friendly.

Am I doing smth wrong or this is a feature that can be implemented ?

Lingua principale
Rust
Stelle
5.6k
Fork
220
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 dtolnay/thiserror

Tutte le issue di dtolnay/thiserror

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.