leptos-rs / leptos

Build fast web applications with Rust.
https://leptos.dev
MIT License
16.17k stars 636 forks source link

`ServerFnError<TargetErr>` should impl `From<ServerFnError<SourceErr>>` where `TargetErr: From<SourceErr>` #3155

Open nicolas-guichard opened 4 hours ago

nicolas-guichard commented 4 hours ago

Last one in my series of proposed improvements to ServerFnError: we could provide a conversion from ServerFnError<OldCustErr> to ServerFnError<NewCustErr> if NewCustErr: From<OldCustErr>. Something like:

impl<OldCustErr, NewCustErr: OldCustErr> From<ServerFnError<OldCustErr>> for ServerFnError<NewCustErr> {
    fn from(value: ServerFnError<NoCustomError>) -> Self {
        match self {
            ServerFnError::<OldCustErr>::WrappedServerError(o) => ServerFnError::<NewCustErr>::WrappedServerError(o.into()),
            ServerFnError::<OldCustErr>::Registration(s) => ServerFnError::<NewCustErr>::Registration(s),
            ServerFnError::<OldCustErr>::Request(s) => ServerFnError::<NewCustErr>::Request(s),
            ServerFnError::<OldCustErr>::Response(s) => ServerFnError::<NewCustErr>::Response(s),
            ServerFnError::<OldCustErr>::ServerError(s) => ServerFnError::<NewCustErr>::ServerError(s),
            ServerFnError::<OldCustErr>::Deserialization(s) => ServerFnError::<NewCustErr>::Deserialization(s),
            ServerFnError::<OldCustErr>::Serialization(s) => ServerFnError::<NewCustErr>::Serialization(s),
            ServerFnError::<OldCustErr>::Args(s) => ServerFnError::<NewCustErr>::Args(s),
            ServerFnError::<OldCustErr>::MissingArg(s) => ServerFnError::<NewCustErr>::MissingArg(s),
        }
    }
}

The typical use-case I have is helper functions that can fail in one or two ways, shared by multiple server functions that can also fail in other ways.

gbj commented 3 hours ago

Again, feel free to make a PR if the compiler will actually allow this.