lucab / libsystemd-rs

A pure-Rust client library to work with systemd
https://crates.io/crates/libsystemd
Other
106 stars 19 forks source link

errors: add internal helpers to simplify error handling #128

Closed dfreese closed 1 year ago

dfreese commented 1 year ago

This adds a Context trait, as well as implementations of it for Result and Optional to help consolidate the map_err(|e| format!(...)) logic that is used to provide propagate error information. This is inspired by anyhow, but returns SdError instead to keep the API surface of the library to a single struct within the library.

lucab commented 1 year ago

Thanks for the PR, but overall I'm inclined to reject this patch. I've left a longer explanation at https://github.com/lucab/libsystemd-rs/issues/127#issuecomment-1354424545, including a possible alternative approach.

dfreese commented 1 year ago

I've updated this to remove anyhow and implement an internal Context that implements the expected formatting. I've added a SdResult helper as well. This is only implemented in logging. Let me know if you'd rather it be adopted throughout the library more broadly in a single PR or have other concerns.

lucab commented 1 year ago

Looks good to me, feel free to squash everything into a single commit and I'll merge it. I'm fine to have it only applied to the logging module for the moment, we can add the rest incrementally and new logic can already start using the context helpers.

dfreese commented 1 year ago

Squashed. Thanks for reviewing.