fuel_core/schema/scalars/
tx_pointer.rs

1use async_graphql::{
2    InputValueError,
3    InputValueResult,
4    Scalar,
5    ScalarType,
6    Value,
7    connection::CursorType,
8};
9use fuel_core_types::fuel_tx;
10use std::{
11    fmt::{
12        Display,
13        Formatter,
14    },
15    str::FromStr,
16};
17
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct TxPointer(pub(crate) fuel_tx::TxPointer);
20
21#[Scalar(name = "TxPointer")]
22impl ScalarType for TxPointer {
23    fn parse(value: Value) -> InputValueResult<Self> {
24        match &value {
25            Value::String(value) => {
26                TxPointer::from_str(value.as_str()).map_err(Into::into)
27            }
28            _ => Err(InputValueError::expected_type(value)),
29        }
30    }
31
32    fn to_value(&self) -> Value {
33        Value::String(self.to_string())
34    }
35}
36
37impl FromStr for TxPointer {
38    type Err = String;
39
40    fn from_str(s: &str) -> Result<Self, Self::Err> {
41        fuel_tx::TxPointer::from_str(s)
42            .map(Self)
43            .map_err(str::to_owned)
44    }
45}
46
47impl Display for TxPointer {
48    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49        let s = format!("{:#x}", self.0);
50        s.fmt(f)
51    }
52}
53
54impl From<TxPointer> for fuel_tx::TxPointer {
55    fn from(s: TxPointer) -> Self {
56        s.0
57    }
58}
59
60impl From<fuel_tx::TxPointer> for TxPointer {
61    fn from(tx_pointer: fuel_tx::TxPointer) -> Self {
62        Self(tx_pointer)
63    }
64}
65
66impl CursorType for TxPointer {
67    type Error = String;
68
69    fn decode_cursor(s: &str) -> Result<Self, Self::Error> {
70        Self::from_str(s)
71    }
72
73    fn encode_cursor(&self) -> String {
74        self.to_string()
75    }
76}