templar_proxy_oracle_kernel/proxy/aggregator/method/
mod.rs1pub mod median;
2pub mod priority;
3
4use crate::Price;
5
6pub trait Aggregate<S> {
7 fn aggregate<I>(&self, prices: I) -> Result<Price, Error>
8 where
9 I: IntoIterator<Item = Option<Price>>,
10 I::IntoIter: ExactSizeIterator<Item = Option<Price>>;
11}
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14#[repr(u16)]
15pub enum ErrorCode {
16 LengthMismatch = 1,
17 TooFewValidSources = 2,
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum Error {
22 LengthMismatch { expected: usize, actual: usize },
23 TooFewValidSources { expected: usize, actual: usize },
24}
25
26impl Error {
27 #[must_use]
28 pub const fn code(self) -> ErrorCode {
29 match self {
30 Self::LengthMismatch { .. } => ErrorCode::LengthMismatch,
31 Self::TooFewValidSources { .. } => ErrorCode::TooFewValidSources,
32 }
33 }
34}
35
36impl core::fmt::Display for Error {
37 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38 match self {
39 Self::LengthMismatch { expected, actual } => {
40 write!(f, "length mismatch: expected {expected}, actual {actual}")
41 }
42 Self::TooFewValidSources { expected, actual } => {
43 write!(
44 f,
45 "too few valid sources: expected {expected}, actual {actual}"
46 )
47 }
48 }
49 }
50}
51
52#[cfg(feature = "std")]
53impl std::error::Error for Error {}