Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nicolas-guichard opened this issue Oct 23, 2024 · 2 comments

Comments

@nicolas-guichard
Copy link

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
Copy link
Collaborator

gbj commented Oct 23, 2024

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

@benwis
Copy link
Contributor

benwis commented Nov 17, 2024

This one also conflicts with an existing trait, this time in convert

   Compiling server_fn v0.7.0-rc1 (/celCluster/projects/leptos/server_fn)
error[E0119]: conflicting implementations of trait `std::convert::From<ServerFnError<_>>` for type `ServerFnError<_>`
   --> server_fn/src/error.rs:197:1
    |
197 | impl<OldCustErr, NewCustErr: From<OldCustErr>> From<ServerFnError<OldCustErr>> for ServerFnError<NewCustErr...
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: conflicting implementation in crate `core`:
            - impl<T> std::convert::From<T> for T;

@benwis benwis closed this as completed Nov 17, 2024
nicolas-guichard added a commit to nicolas-guichard/leptos that referenced this issue Nov 17, 2024
This adds 3 new traits:
- `ConvertServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<SourceCustErr>>` to
  `Result<T, ServerFnError<TargetCustError>>` when `TargetCustError`
  implements `From<SourceCustErr>`

- `ConvertDefaultServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<NoCustomError>` to
  `Result<T, ServerFnError<TargetCustError>>`

- `IntoServerFnResult` provides easy conversion from `Result<T, E>` to
  `Result<T, ServerFnError::ServerError>` when `E` implements
  `std::error::Error`

Fixes leptos-rs#3153 and leptos-rs#3155
nicolas-guichard added a commit to nicolas-guichard/leptos that referenced this issue Nov 17, 2024
This adds 3 new traits:
- `ConvertServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<SourceCustErr>>` to
  `Result<T, ServerFnError<TargetCustError>>` when `TargetCustError`
  implements `From<SourceCustErr>`

- `ConvertDefaultServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<NoCustomError>` to
  `Result<T, ServerFnError<TargetCustError>>`

- `IntoServerFnResult` provides easy conversion from `Result<T, E>` to
  `Result<T, ServerFnError::ServerError>` when `E` implements
  `std::error::Error`

Fixes leptos-rs#3153 and leptos-rs#3155
nicolas-guichard added a commit to nicolas-guichard/leptos that referenced this issue Nov 17, 2024
This adds 3 new traits:
- `ConvertServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<SourceCustErr>>` to
  `Result<T, ServerFnError<TargetCustError>>` when `TargetCustError`
  implements `From<SourceCustErr>`

- `ConvertDefaultServerFnResult` provides easy conversion from
  `Result<T, ServerFnError<NoCustomError>` to
  `Result<T, ServerFnError<TargetCustError>>`

- `IntoServerFnResult` provides easy conversion from `Result<T, E>` to
  `Result<T, ServerFnError::ServerError>` when `E` implements
  `std::error::Error`

Fixes leptos-rs#3153 and leptos-rs#3155
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants