iota_graphql_rpc/types/
uint53.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::fmt;
6
7use async_graphql::*;
8use iota_types::{base_types::SequenceNumber, iota_serde::BigInt};
9
10#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
11pub(crate) struct UInt53(u64);
12
13/// An unsigned integer that can hold values up to 2^53 - 1. This can be treated
14/// similarly to `Int`, but it is guaranteed to be non-negative, and it may be
15/// larger than 2^32 - 1.
16#[Scalar(name = "UInt53")]
17impl ScalarType for UInt53 {
18    fn parse(value: Value) -> InputValueResult<Self> {
19        let Value::Number(n) = value else {
20            return Err(InputValueError::expected_type(value));
21        };
22
23        let Some(n) = n.as_u64() else {
24            return Err(InputValueError::custom("Expected an unsigned integer."));
25        };
26
27        Ok(UInt53(n))
28    }
29
30    fn to_value(&self) -> Value {
31        Value::Number(self.0.into())
32    }
33}
34
35impl fmt::Display for UInt53 {
36    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37        write!(f, "{}", self.0)
38    }
39}
40
41impl From<u64> for UInt53 {
42    fn from(value: u64) -> Self {
43        Self(value)
44    }
45}
46
47impl From<UInt53> for SequenceNumber {
48    fn from(value: UInt53) -> Self {
49        SequenceNumber::from(value.0)
50    }
51}
52
53impl From<UInt53> for BigInt<u64> {
54    fn from(value: UInt53) -> Self {
55        BigInt::from(value.0)
56    }
57}
58
59impl From<UInt53> for u64 {
60    fn from(value: UInt53) -> Self {
61        value.0
62    }
63}
64
65impl From<UInt53> for i64 {
66    fn from(value: UInt53) -> Self {
67        value.0 as i64
68    }
69}