thegraph_core/
subgraph_id.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use alloy::primitives::B256;

/// Subgraph ID parsing error.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ParseSubgraphIdError {
    /// Invalid string length. The input string is longer than 44 characters.
    #[error("invalid length {length}: {value} (length must be <=44)")]
    InvalidLength { value: String, length: usize },

    /// Invalid base-58 string. The input string contains invalid characters.
    #[error("invalid character \"{value}\": {error}")]
    InvalidCharacter { value: String, error: String },
}

/// A Subgraph ID is a 32-byte identifier for a subgraph.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct SubgraphId(B256);

impl SubgraphId {
    /// The "zero" [`SubgraphId`].
    ///
    /// This is a constant value that represents the zero ID. It is equivalent to parsing a zeroed
    /// 32-byte array.
    pub const ZERO: Self = Self(B256::ZERO);

    /// Create a new [`SubgraphId`].
    pub const fn new(value: B256) -> Self {
        Self(value)
    }
}

impl From<B256> for SubgraphId {
    fn from(bytes: B256) -> Self {
        Self(bytes)
    }
}

impl From<[u8; 32]> for SubgraphId {
    fn from(value: [u8; 32]) -> Self {
        Self(B256::from(value))
    }
}

impl From<SubgraphId> for B256 {
    fn from(id: SubgraphId) -> Self {
        id.0
    }
}

impl From<&SubgraphId> for B256 {
    fn from(id: &SubgraphId) -> Self {
        id.0
    }
}

impl AsRef<B256> for SubgraphId {
    fn as_ref(&self) -> &B256 {
        &self.0
    }
}

impl std::str::FromStr for SubgraphId {
    type Err = ParseSubgraphIdError;

    /// Parse a `SubgraphID` from a base-58 encoded string.
    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let mut buffer = [0_u8; 32];

        // Decode the base58 string into a byte array, and get the number of bytes written
        let len = bs58::decode(value)
            .onto(&mut buffer)
            .map_err(|err| match err {
                bs58::decode::Error::BufferTooSmall => ParseSubgraphIdError::InvalidLength {
                    value: value.to_string(),
                    length: value.len(),
                },
                bs58::decode::Error::InvalidCharacter { .. } => {
                    ParseSubgraphIdError::InvalidCharacter {
                        value: value.to_string(),
                        error: err.to_string(),
                    }
                }
                bs58::decode::Error::NonAsciiCharacter { .. } => {
                    ParseSubgraphIdError::InvalidCharacter {
                        value: value.to_string(),
                        error: err.to_string(),
                    }
                }
                _ => unreachable!(),
            })?;

        // If the decoded hash is not 32 bytes long, rotate it to the right so the zero bytes
        // are at the beginning of the array.
        if len < 32 {
            buffer.rotate_right(32 - len);
        }

        Ok(Self::from(buffer))
    }
}

impl std::fmt::Display for SubgraphId {
    /// Format the `SubgraphId` as a base58-encoded string.
    ///
    /// ```rust
    /// use thegraph_core::{subgraph_id, SubgraphId};
    ///
    /// const ID: SubgraphId = subgraph_id!("DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp");
    ///
    /// assert_eq!(format!("{}", ID), "DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp");
    /// ```
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let leading_zeroes = self.0.iter().take_while(|b| **b == 0).count();
        f.write_str(&bs58::encode(&self.0[leading_zeroes..]).into_string())
    }
}

impl std::fmt::Debug for SubgraphId {
    /// Format the `SubgraphId` as a debug string.
    ///
    /// ```rust
    /// use thegraph_core::{subgraph_id, SubgraphId};
    ///
    /// const ID: SubgraphId = subgraph_id!("DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp");
    ///
    /// assert_eq!(format!("{:?}", ID), "SubgraphId(DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp)");
    /// ```
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "SubgraphId({})", self)
    }
}

/// Converts a sequence of string literals containing 32-bytes Base58-encoded data into a new
/// [`SubgraphId`] at compile time.
///
/// To create an `SubgraphId` from a string literal (Base58) at compile time:
///
/// ```rust
/// use thegraph_core::{subgraph_id, SubgraphId};
///
/// const SUBGRAPH_ID: SubgraphId = subgraph_id!("DZz4kDTdmzWLWsV373w2bSmoar3umKKH9y82SUKr5qmp");
/// ```
///
/// If no argument is provided, the macro will create an `SubgraphId` with the zero ID:
///
/// ```rust
/// use thegraph_core::{subgraph_id, SubgraphId};
///
/// const SUBGRAPH_ID: SubgraphId = subgraph_id!();
///
/// assert_eq!(SUBGRAPH_ID, SubgraphId::ZERO);
/// ```
#[macro_export]
#[doc(hidden)]
macro_rules! __subgraph_id {
    () => {
        $crate::SubgraphId::ZERO
    };
    ($id:tt) => {
        $crate::SubgraphId::new($crate::__parse_subgraph_id_const($id))
    };
}

/// Parse a base58-encoded string into a 32-bytes array at compile time.
#[doc(hidden)]
pub const fn __parse_subgraph_id_const(value: &str) -> B256 {
    let data = value.as_bytes();
    let bytes = bs58::decode(data).into_array_const_unwrap::<32>();
    B256::new(bytes)
}

#[cfg(test)]
mod tests {
    use alloy::primitives::{b256, B256};

    use super::{ParseSubgraphIdError, SubgraphId};
    use crate::subgraph_id;

    const VALID_SUBGRAPH_ID: &str = "7xB3yxxD8okmq4dZPky3eP1nYRgLfZrwMyUQBGo32t4U";

    const EXPECTED_ID_BYTES: B256 =
        b256!("67486e65165b1474898247760a4b852d70d95782c6325960e5b6b4fd82fed1bd");

    const EXPECTED_ID: SubgraphId = subgraph_id!(VALID_SUBGRAPH_ID);

    #[test]
    fn parse_valid_string() {
        //* Given
        let valid_id = VALID_SUBGRAPH_ID;

        //* When
        let result = valid_id.parse::<SubgraphId>();

        //* Then
        let id = result.expect("invalid subgraph ID");
        assert_eq!(id, EXPECTED_ID);
        assert_eq!(id.0, EXPECTED_ID_BYTES);
    }

    #[test]
    fn parse_failure_on_invalid_string() {
        //* Given
        // The following string is not a valid base58 string as it contains the `l` character
        let invalid_id = "invalid";

        //* When
        let result = invalid_id.parse::<SubgraphId>();

        //* Then
        let err = result.expect_err("expected an error");
        assert_eq!(
            err,
            ParseSubgraphIdError::InvalidCharacter {
                value: invalid_id.to_string(),
                error: "provided string contained invalid character 'l' at byte 4".to_string(),
            }
        );
    }

    #[test]
    fn format_subgraph_id_display() {
        //* Given
        let valid_id = EXPECTED_ID;

        //* When
        let result = format!("{}", valid_id);

        //* Then
        assert_eq!(result, VALID_SUBGRAPH_ID);
    }

    #[test]
    fn format_subgraph_id_debug() {
        //* Given
        let expected_debug_repr = format!("SubgraphId({})", VALID_SUBGRAPH_ID);

        let valid_id = EXPECTED_ID;

        //* When
        let result = format!("{:?}", valid_id);

        //* Then
        assert_eq!(result, expected_debug_repr);
    }

    #[test]
    fn serialize_leading_zeroes() {
        let input = "4JruhWH1ZdwvUuMg2xCmtnZQYYHvmEq6cmTcZkpM6pW";
        let output: SubgraphId = input.parse().unwrap();
        assert_eq!(output.to_string(), input.to_string());
    }
}