|
1 | | -use thiserror::Error; |
2 | | - |
3 | 1 | mod macros; |
4 | 2 |
|
5 | 3 | #[allow(dead_code)] |
@@ -47,52 +45,76 @@ pub use treeseq::TreeSequence; |
47 | 45 |
|
48 | 46 | use traits::TskTeardown; |
49 | 47 |
|
50 | | -#[derive(Error, Debug)] |
51 | 48 | #[non_exhaustive] |
| 49 | +#[derive(Debug)] |
52 | 50 | pub enum TskitError { |
53 | 51 | /// Returned when conversion attempts fail |
54 | | - #[error("range error: {}", *.0)] |
55 | 52 | RangeError(String), |
56 | 53 | /// Used when bad input is encountered. |
57 | | - #[error("we received {} but expected {}",*got, *expected)] |
58 | 54 | ValueError { got: String, expected: String }, |
59 | 55 | /// Used when array access is out of range. |
60 | 56 | /// Typically, this is used when accessing |
61 | 57 | /// arrays allocated on the C side. |
62 | | - #[error("Invalid index")] |
63 | 58 | IndexError, |
64 | 59 | /// Raised when samples are requested from |
65 | 60 | /// [`crate::Tree`] objects, but sample lists are |
66 | 61 | /// not being updated. |
67 | | - #[error("Not tracking samples in Trees")] |
68 | 62 | NotTrackingSamples, |
69 | 63 | /// Wrapper around tskit C API error codes. |
70 | | - #[error("{}", get_tskit_error_message(*code))] |
71 | 64 | ErrorCode { code: i32 }, |
72 | 65 | /// A redirection of [``crate::metadata::MetadataError``] |
73 | | - #[error("{value:?}")] |
74 | 66 | MetadataError { |
75 | 67 | /// The redirected error |
76 | | - #[from] |
77 | 68 | value: MetadataError, |
78 | 69 | }, |
79 | 70 | /// General error variant |
80 | | - #[error("{}", *.0)] |
81 | 71 | LibraryError(String), |
82 | 72 | } |
83 | 73 |
|
84 | | -#[derive(Error, Debug)] |
| 74 | +impl std::fmt::Display for TskitError { |
| 75 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 76 | + match self { |
| 77 | + Self::RangeError(msg) => write!(f, "range error: {}", msg), |
| 78 | + Self::ValueError { got, expected } => { |
| 79 | + write!(f, "we received {} but expected {}", got, expected) |
| 80 | + } |
| 81 | + Self::IndexError => write!(f, "Invalid index"), |
| 82 | + Self::NotTrackingSamples => write!(f, "Not tracking samples in Trees"), |
| 83 | + Self::ErrorCode { code } => write!(f, "{}", get_tskit_error_message(*code)), |
| 84 | + Self::MetadataError { value } => write!(f, "meta data error: {}", value), |
| 85 | + Self::LibraryError(msg) => write!(f, "library error: {msg}"), |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl From<MetadataError> for TskitError { |
| 91 | + fn from(value: MetadataError) -> Self { |
| 92 | + Self::MetadataError { value } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl std::error::Error for TskitError {} |
| 97 | + |
| 98 | +#[derive(Debug)] |
85 | 99 | #[non_exhaustive] |
86 | 100 | pub enum MetadataError { |
87 | 101 | /// Error related to types implementing |
88 | 102 | /// metadata serialization. |
89 | | - #[error("{}", *value)] |
90 | 103 | RoundtripError { |
91 | | - #[from] |
92 | 104 | value: Box<dyn std::error::Error + Send + Sync>, |
93 | 105 | }, |
94 | 106 | } |
95 | 107 |
|
| 108 | +impl std::fmt::Display for MetadataError { |
| 109 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 110 | + match self { |
| 111 | + Self::RoundtripError { value } => write!(f, "metadata round trip error: {value:?}"), |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl std::error::Error for MetadataError {} |
| 117 | + |
96 | 118 | //#[non_exhaustive] |
97 | 119 | //#[derive(Error, Debug)] |
98 | 120 | //pub enum Error { |
|
0 commit comments