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)
| 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 | |
| 918 | impl<C: 'static> From<Report<C>> for Box<dyn Error> { |