af_price_updates/
parse_error.rs1#[derive(Clone, Debug, Eq, PartialEq)]
6pub struct ParseError(Box<str>);
7
8impl ParseError {
9 pub(crate) fn new(message: impl Into<Box<str>>) -> Self {
10 Self(message.into())
11 }
12}
13
14impl std::fmt::Display for ParseError {
15 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16 f.write_str(&self.0)
17 }
18}
19
20impl std::error::Error for ParseError {}
21
22#[cfg(test)]
23mod tests {
24 use crate::{
25 U256, pyth, pyth_lazer::FeedProperty, sorted_set::SortedSet, stork::EncodedAssetId,
26 };
27
28 fn boxed<E: std::error::Error + Send + Sync + 'static>(
30 err: E,
31 ) -> Box<dyn std::error::Error + Send + Sync> {
32 err.into()
33 }
34
35 #[test]
36 fn hex_parse_errors() {
37 for (input, message) in [
38 ("", "expected 1 to 64 hex digits"),
39 ("0x", "expected 1 to 64 hex digits"),
40 (&"1".repeat(65), "expected 1 to 64 hex digits"),
41 ("0xabg", "invalid hex character 'g'"),
42 ] {
43 assert_eq!(input.parse::<U256>().unwrap_err().to_string(), message);
44 assert_eq!(
45 boxed(input.parse::<pyth::PriceFeedId>().unwrap_err()).to_string(),
46 message
47 );
48 assert_eq!(
49 input.parse::<EncodedAssetId>().unwrap_err().to_string(),
50 message
51 );
52 }
53 }
54
55 #[test]
56 fn list_and_name_parse_errors() {
57 assert_eq!(
58 "1,2".parse::<SortedSet<u32>>().unwrap_err().to_string(),
59 "expected [id,...]: 1,2"
60 );
61 assert_eq!(
62 "[1,x]".parse::<SortedSet<u32>>().unwrap_err().to_string(),
63 "invalid digit found in string"
64 );
65 assert_eq!(
66 boxed("bid".parse::<FeedProperty>().unwrap_err()).to_string(),
67 "unknown feed property: bid"
68 );
69 }
70}