sui_adapter_latest/static_programmable_transactions/
spanned.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! This module provides a shared API amongst the ASTs for original command and argument locations.
5//! Some translations might reorder or move commands/arguments, and as such we need to annotate
6//! the values with the original location
7
8use std::{
9    cmp::Ordering,
10    fmt,
11    hash::{Hash, Hasher},
12};
13
14#[derive(Copy, Clone)]
15pub struct Spanned<T> {
16    pub idx: u16,
17    pub value: T,
18}
19
20impl<T: PartialEq> PartialEq for Spanned<T> {
21    fn eq(&self, other: &Spanned<T>) -> bool {
22        self.value == other.value
23    }
24}
25
26impl<T: Eq> Eq for Spanned<T> {}
27
28impl<T: Hash> Hash for Spanned<T> {
29    fn hash<H: Hasher>(&self, state: &mut H) {
30        self.value.hash(state);
31    }
32}
33
34impl<T: PartialOrd> PartialOrd for Spanned<T> {
35    fn partial_cmp(&self, other: &Spanned<T>) -> Option<Ordering> {
36        self.value.partial_cmp(&other.value)
37    }
38}
39
40impl<T: Ord> Ord for Spanned<T> {
41    fn cmp(&self, other: &Spanned<T>) -> Ordering {
42        self.value.cmp(&other.value)
43    }
44}
45
46impl<T: fmt::Display> fmt::Display for Spanned<T> {
47    fn fmt(&self, f: &mut fmt::Formatter) -> std::fmt::Result {
48        write!(f, "{}", &self.value)
49    }
50}
51
52impl<T: fmt::Debug> fmt::Debug for Spanned<T> {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        write!(f, "{:?}", &self.value)
55    }
56}
57
58/// Function used to have nearly tuple-like syntax for creating a Spanned
59pub const fn sp<T>(idx: u16, value: T) -> Spanned<T> {
60    Spanned { idx, value }
61}
62
63/// Macro used to create a tuple-like pattern match for Spanned
64#[macro_export]
65macro_rules! sp {
66    (_, $value:pat) => {
67        $crate::static_programmable_transactions::spanned::Spanned { value: $value, .. }
68    };
69    ($idx:pat, _) => {
70        $crate::static_programmable_transactions::spanned::Spanned { idx: $idx, .. }
71    };
72    ($idx:pat, $value:pat) => {
73        $crate::static_programmable_transactions::spanned::Spanned {
74            idx: $idx,
75            value: $value,
76        }
77    };
78}