1#![forbid(unsafe_code)]
2
3use std::error::Error;
4use std::fmt::{self, Display, Formatter};
5use std::panic::Location;
6
7pub use union_error_derive::ErrorUnion;
8
9#[derive(Debug)]
10pub struct Located<E> {
11 source: E,
12 location: &'static Location<'static>,
13}
14
15impl<E> Located<E> {
16 #[track_caller]
17 pub fn new(source: E) -> Self {
18 Self {
19 source,
20 location: Location::caller(),
21 }
22 }
23
24 pub fn source_ref(&self) -> &E {
25 &self.source
26 }
27
28 pub fn into_source(self) -> E {
29 self.source
30 }
31
32 pub fn location(&self) -> &'static Location<'static> {
33 self.location
34 }
35}
36
37impl<E> Display for Located<E>
38where
39 E: Display,
40{
41 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
42 write!(
43 f,
44 "{} (at {}:{}:{})",
45 self.source,
46 self.location.file(),
47 self.location.line(),
48 self.location.column()
49 )
50 }
51}
52
53impl<E> Error for Located<E>
54where
55 E: Error + 'static,
56{
57 fn source(&self) -> Option<&(dyn Error + 'static)> {
58 Some(&self.source)
59 }
60}