MCPcopy Create free account
hub / github.com/hashintel/hash / current_contexts

Method current_contexts

libs/error-stack/src/report.rs:876–915  ·  view source on GitHub ↗

Returns an iterator over the current contexts of the `Report`. This method is similar to [`current_context`], but instead of returning a single context, it returns an iterator over all contexts in the `Report`. The order of the contexts should not be relied upon, as it is not guaranteed to be stable. ## Example ```rust # use std::{fs, path::Path}; # use error_stack::Report; use std::io; fn re

(&self)

Source from the content-addressed store, hash-verified

874 ///
875 /// [`current_context`]: Self::current_context
876 pub fn current_contexts(&self) -> impl Iterator<Item = &C>
877 where
878 C: Send + Sync + 'static,
879 {
880 // this needs a manual traveral implementation, why?
881 // We know that each arm has a current context, but we don't know where that context is,
882 // therefore we need to search for it on each branch, but stop once we found it, that way
883 // we're able to return the current context, even if it is "buried" underneath a bunch of
884 // attachments.
885 let mut output = Vec::new();
886
887 // this implementation does some "weaving" in a sense, it goes L->R for the frames, then
888 // R->L for the sources, which means that some sources might be out of order, but this
889 // simplifies implementation.
890 let mut stack = vec![self.current_frames()];
891 while let Some(frames) = stack.pop() {
892 for frame in frames {
893 // check if the frame is the current context, in that case we don't need to follow
894 // the tree anymore
895 if let Some(context) = frame.downcast_ref::<C>() {
896 output.push(context);
897 continue;
898 }
899
900 // descend into the tree
901 let sources = frame.sources();
902 match sources {
903 [] => unreachable!(
904 "Report does not contain a context. This is considered a bug and should be \
905 reported to https://github.com/hashintel/hash/issues/new/choose"
906 ),
907 sources => {
908 stack.push(sources);
909 }
910 }
911 }
912 }
913
914 output.into_iter()
915 }
916}
917
918impl<C: 'static> From<Report<C>> for Box<dyn Error> {

Callers 15

add_singleFunction · 0.80
add_multipleFunction · 0.80
capture_singleFunction · 0.80
capture_multipleFunction · 0.80
try_singleFunction · 0.80
try_multipleFunction · 0.80
try_arbitrary_returnFunction · 0.80
finishFunction · 0.80
finish_withFunction · 0.80
finish_defaultFunction · 0.80
finish_with_valueFunction · 0.80

Calls 4

sourcesMethod · 0.80
popMethod · 0.65
pushMethod · 0.65
into_iterMethod · 0.45

Tested by 15

add_singleFunction · 0.64
add_multipleFunction · 0.64
capture_singleFunction · 0.64
capture_multipleFunction · 0.64
try_singleFunction · 0.64
try_multipleFunction · 0.64
try_arbitrary_returnFunction · 0.64
finishFunction · 0.64
finish_withFunction · 0.64
finish_defaultFunction · 0.64
finish_with_valueFunction · 0.64