graphql_federated_graph/federated_graph/
ids.rs

1use super::*;
2
3macro_rules! id_newtypes {
4    ($($storage:ident [ $name:ident ] -> $out:ident,)*) => {
5        $(
6            #[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
7            pub struct $name(usize);
8
9            impl $name {
10                pub const fn const_from_usize(i: usize) -> Self {
11                    $name(i)
12                }
13            }
14
15            impl From<$name> for usize {
16              fn from(value: $name) -> usize {
17                value.0
18              }
19            }
20
21            impl From<usize> for $name {
22              fn from(value: usize) -> $name {
23                $name(value)
24              }
25            }
26
27            impl std::ops::Index<$name> for FederatedGraph {
28                type Output = $out;
29
30                fn index(&self, index: $name) -> &$out {
31                    &self.$storage[index.0]
32                }
33            }
34
35            impl std::ops::IndexMut<$name> for FederatedGraph {
36                fn index_mut(&mut self, index: $name) -> &mut $out {
37                    &mut self.$storage[index.0]
38                }
39            }
40        )*
41    }
42}
43
44id_newtypes! {
45    enum_values[EnumValueId] -> EnumValueRecord,
46    enum_definitions[EnumDefinitionId] -> EnumDefinitionRecord,
47    directive_definitions[DirectiveDefinitionId] -> DirectiveDefinitionRecord,
48    fields[FieldId] -> Field,
49    input_objects[InputObjectId] -> InputObject,
50    input_value_definitions[InputValueDefinitionId] -> InputValueDefinition,
51    interfaces[InterfaceId] -> Interface,
52    objects[ObjectId] -> Object,
53    strings[StringId] -> String,
54    subgraphs[SubgraphId] -> Subgraph,
55    extensions[ExtensionId] -> Extension,
56    scalar_definitions[ScalarDefinitionId] -> ScalarDefinitionRecord,
57    unions[UnionId] -> Union,
58}