mas_handlers/graphql/model/
cursor.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7use async_graphql::connection::OpaqueCursor;
8use serde::{Deserialize, Serialize};
9use ulid::Ulid;
10
11pub use super::NodeType;
12
13#[derive(Serialize, Deserialize, PartialEq, Eq)]
14pub struct NodeCursor(pub NodeType, pub Ulid);
15
16impl NodeCursor {
17    pub fn extract_for_types(&self, node_types: &[NodeType]) -> Result<Ulid, async_graphql::Error> {
18        if node_types.contains(&self.0) {
19            Ok(self.1)
20        } else {
21            Err(async_graphql::Error::new("invalid cursor"))
22        }
23    }
24
25    pub fn extract_for_type(&self, node_type: NodeType) -> Result<Ulid, async_graphql::Error> {
26        if self.0 == node_type {
27            Ok(self.1)
28        } else {
29            Err(async_graphql::Error::new("invalid cursor"))
30        }
31    }
32}
33
34pub type Cursor = OpaqueCursor<NodeCursor>;