grafbase_sdk/types/
contract.rs1use crate::{types::Directive, wit};
2
3pub struct GraphqlSubgraph(wit::GraphqlSubgraph);
5
6impl From<wit::GraphqlSubgraph> for GraphqlSubgraph {
7 fn from(subgraph: wit::GraphqlSubgraph) -> Self {
8 Self(subgraph)
9 }
10}
11
12impl GraphqlSubgraph {
13 pub fn name(&self) -> &str {
15 self.0.name.as_str()
16 }
17
18 pub fn url(&self) -> &str {
20 self.0.url.as_str()
21 }
22
23 pub fn url_mut(&mut self) -> &mut String {
25 &mut self.0.url
26 }
27}
28
29#[derive(Clone, Copy)]
31pub struct ContractDirective<'a> {
32 index: u32,
33 directive: Directive<'a>,
34}
35
36impl<'a> From<(usize, &'a wit::Directive)> for ContractDirective<'a> {
37 fn from((index, directive): (usize, &'a wit::Directive)) -> Self {
38 Self {
39 index: index as u32,
40 directive: directive.into(),
41 }
42 }
43}
44
45impl<'a> std::ops::Deref for ContractDirective<'a> {
46 type Target = Directive<'a>;
47 fn deref(&self) -> &Self::Target {
48 &self.directive
49 }
50}
51
52pub struct Contract(wit::Contract);
54
55impl Contract {
56 pub fn new(directives: &[ContractDirective<'_>], accessible_by_default: bool) -> Self {
59 Self(wit::Contract {
60 accessible: vec![accessible_by_default as i8 - 1; directives.len()],
61 accessible_by_default,
62 hide_unreachable_types: true,
63 subgraphs: Vec::new(),
64 })
65 }
66
67 pub fn accessible(&mut self, directive: ContractDirective<'_>, accessible: bool) -> &mut Self {
69 let inaccessible_mask = accessible as i8 - 1; self.accessible_with_priority(directive, (inaccessible_mask & -2) | (!inaccessible_mask & 1))
71 }
72
73 pub fn override_accessible(&mut self, directive: ContractDirective<'_>, accessible: bool) -> &mut Self {
75 self.accessible_with_priority(directive, i8::MAX.wrapping_add(!accessible as i8))
76 }
77
78 pub fn accessible_with_priority(&mut self, directive: ContractDirective<'_>, accessible: i8) -> &mut Self {
89 self.0.accessible[directive.index as usize] = accessible;
90 self
91 }
92
93 pub fn hide_unreachable_types(&mut self, hide: bool) -> &mut Self {
95 self.0.hide_unreachable_types = hide;
96 self
97 }
98
99 pub fn subgraph(&mut self, subgraph: GraphqlSubgraph) -> &mut Self {
102 self.0.subgraphs.push(subgraph.0);
103 self
104 }
105}
106
107impl From<Contract> for wit::Contract {
108 fn from(contract: Contract) -> Self {
109 contract.0
110 }
111}