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
GEN-35: error-stack
implement serde
hooks
#1558
Draft
indietyp
wants to merge
55
commits into
main
Choose a base branch
from
bm/es/serde-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github-actions
bot
added
the
area/libs > error-stack
Affects the `error-stack` crate (library)
label
Nov 30, 2022
The latest updates on your projects. Learn more about Vercel for Git βοΈ
|
trunk-io bot
pushed a commit
that referenced
this pull request
Jan 31, 2023
## π What is the purpose of this PR? #1693 separated `fmt::HookContext` into `hook::HookContext` and `fmt::HookContext`, so that it can be used in #1558. While working, the problem is that documentation doesn't render correctly because `hook::HookContext` is private in public. Therefore unreachable rustdoc is unable to navigate to the type, and the documentation is incomplete. This PR moves to a macro-based approach to enable better documentation. Instead of using an unreachable public type, we have a macro that implements the necessary common functions. This is by far not ideal, but I found this to be the best way without compromising #1558 or future hooks.
# Conflicts: # libs/error-stack/Cargo.toml # libs/error-stack/Makefile.toml # libs/error-stack/src/fmt.rs # libs/error-stack/src/fmt/hook.rs # libs/error-stack/src/serde.rs # libs/error-stack/tests/common.rs # libs/error-stack/tests/snapshots/test_debug__full__alt.snap # libs/error-stack/tests/snapshots/test_debug__full__color_mode_color.snap # libs/error-stack/tests/snapshots/test_debug__full__hook.snap # libs/error-stack/tests/snapshots/test_debug__full__hook_decr.snap # libs/error-stack/tests/snapshots/test_debug__full__hook_incr.snap # libs/error-stack/tests/snapshots/test_debug__full__hook_multiple.snap # libs/error-stack/tests/snapshots/test_debug__full__hook_provider.snap # libs/error-stack/tests/snapshots/test_debug__full__multiline.snap # libs/error-stack/tests/snapshots/test_debug__full__multiline_context.snap # libs/error-stack/tests/snapshots/test_debug__full__norm.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested@backtrace-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested@backtrace-pretty-print-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested@hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested@pretty-print-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested@spantrace-backtrace-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested@spantrace-backtrace-pretty-print-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested@spantrace-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested@spantrace-pretty-print-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@backtrace-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@backtrace-pretty-print-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@pretty-print-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@spantrace-backtrace-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@spantrace-backtrace-pretty-print-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@spantrace-hooks.snap # libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@spantrace-pretty-print-hooks.snap # libs/error-stack/tests/snapshots/test_serialize__attachment.snap # libs/error-stack/tests/snapshots/test_serialize__context.snap # libs/error-stack/tests/snapshots/test_serialize__full__attachment.snap # libs/error-stack/tests/snapshots/test_serialize__full__context.snap # libs/error-stack/tests/snapshots/test_serialize__full__hook_custom.snap # libs/error-stack/tests/snapshots/test_serialize__full__hook_custom_owned.snap # libs/error-stack/tests/snapshots/test_serialize__full__multiple_sources.snap # libs/error-stack/tests/snapshots/test_serialize__full__multiple_sources_at_root.snap # libs/error-stack/tests/snapshots/test_serialize__multiple_sources.snap # libs/error-stack/tests/snapshots/test_serialize__multiple_sources_at_root.snap # libs/error-stack/tests/snapshots/test_serialize__sources_nested.snap # libs/error-stack/tests/snapshots/test_serialize__sources_nested@backtrace-hooks.snap # libs/error-stack/tests/snapshots/test_serialize__sources_nested@hooks.snap # libs/error-stack/tests/snapshots/test_serialize__sources_nested@spantrace-backtrace-hooks.snap # libs/error-stack/tests/snapshots/test_serialize__sources_nested@spantrace-hooks.snap # libs/error-stack/tests/test_debug.rs # packages/libs/error-stack/tests/common.rs # packages/libs/error-stack/tests/common/mod.rs # packages/libs/error-stack/tests/snapshots/test_debug__full__complex.snap # packages/libs/error-stack/tests/snapshots/test_debug__full__hook_multiple.snap # packages/libs/error-stack/tests/snapshots/test_debug__full__linear.snap # packages/libs/error-stack/tests/snapshots/test_debug__full__linear_ext.snap # packages/libs/error-stack/tests/snapshots/test_debug__full__sources.snap # packages/libs/error-stack/tests/snapshots/test_debug__full__sources_transparent.snap # packages/libs/error-stack/tests/snapshots/test_debug__sources_nested.snap # packages/libs/error-stack/tests/snapshots/test_debug__sources_nested@pretty-print.snap # packages/libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate.snap # packages/libs/error-stack/tests/snapshots/test_debug__sources_nested_alternate@pretty-print.snap
Converting into draft, because everything is broken right now and the large change in formatting for 0.3 need to be applied to this PR |
nonparibus
changed the title
GEN-35: Aug 8, 2023
error-stack
implement serde
hookserror-stack
implement serde
hooks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area/libs > error-stack
Affects the `error-stack` crate (library)
priority/3 low
Lower priority: nice-to-have
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
π What is the purpose of this PR?
This implements the equivalent of
Debug
hooks, but for the serde output, we're unable to know if a type implementsSerialize directly
; therefore, we need to register them via hooks.π Related links
π« Blocked by
π What does this change?
Report gains 2 new functions:
(potentially, we could also provide a way to change how a type is serialized via:
fn install_serde_custom_hook<T: serde::Serialize>(hook: impl Fn(&T) -> Box<dyn erased_serde::Serialize>)
or something similar).π Does this require a change to the docs?
Yes, we need to update the docs to reflect the new capabilities.
π Next Steps
fmt::HookContext
is currently constructed for every separate invocation. This should instead be persisted (like how we currently carry forward theserde::HookContet
)Context
objects because the current representation does not allow multiple values for aContext
. Hooks can return more than a single serialized object. Further investigation π΅οΈ is needed.std
orhooks
features are missing, behaviour offmt
hooks should be mirroredDynamicFn
is overly restrictive regarding lifetime. Closures need to have the lifetimefor<'a> Fn(&'a T &mut HookContext<T>) -> U + 'a
. This is fine for functions but routinely fails for closures, as we cannot explicitly specify lifetimes on closures. Another possibility would be to relax further the lifetime to:for<'a: 'b, 'b> Fn(&'a T &mut HookContext<T>) -> U + 'b
, but currently, this isn't possible because one cannot set lifetime bounds in HRTB, another approach might - if possible - be necessary.πΉ Demo
hash/packages/libs/error-stack/tests/snapshots/test_serialize__sources_nested@spantrace.snap
Lines 5 to 363 in aaca87f
π‘ Additional Information
DynamicFn
is overly restrictive. This issue can be addressed using the nightly featureclosure_lifetime_binder
, which enables us to specify all lifetimes explicitly. It is more verbose than regular closures (and CLion hates it), but it solves the problem. I provided a test case (showcased below) for more context. This resolves our issue because takingFn(&'a T): U
, Rust infers (for regular closures) that, if references are taken fromT
,U
might have a lifetime less than'a
, using HRTBs we're unable to create a trait that reflects this behavior as far as I know (this is because HRTBs cannot define lifetime bounds). The experimental feature lets us circumvent the issue by explicitly stating thatU
will have a lifetime of'a
. I have attached a screenshot of the error.hash/packages/libs/error-stack/tests/test_serialize.rs
Lines 136 to 145 in 43c600b