grafbase_sdk/types/
directive_site.rs

1use serde::Deserialize;
2
3use crate::{cbor, wit, SdkError};
4
5/// The site where a directive is applied in the GraphQL schema.
6pub enum DirectiveSite<'a> {
7    /// Directive applied to an object type
8    Object(ObjectDirectiveSite<'a>),
9    /// Directive applied to a field definition
10    FieldDefinition(FieldDefinitionDirectiveSite<'a>),
11    /// Directive applied to an interface
12    Interface(InterfaceDirectiveSite<'a>),
13    /// Directive applied to a union
14    Union(UnionDirectiveSite<'a>),
15    /// Directive applied to an enum
16    Enum(EnumDirectiveSite<'a>),
17    /// Directive applied to a scalar
18    Scalar(ScalarDirectiveSite<'a>),
19}
20
21impl<'a> From<&'a wit::DirectiveSite> for DirectiveSite<'a> {
22    fn from(value: &'a wit::DirectiveSite) -> Self {
23        match value {
24            wit::DirectiveSite::Object(site) => DirectiveSite::Object(site.into()),
25            wit::DirectiveSite::FieldDefinition(site) => DirectiveSite::FieldDefinition(site.into()),
26            wit::DirectiveSite::Interface(site) => DirectiveSite::Interface(site.into()),
27            wit::DirectiveSite::Union(site) => DirectiveSite::Union(site.into()),
28            wit::DirectiveSite::Enum(site) => DirectiveSite::Enum(site.into()),
29            wit::DirectiveSite::Scalar(site) => DirectiveSite::Scalar(site.into()),
30        }
31    }
32}
33
34impl<'a> DirectiveSite<'a> {
35    /// Arguments of the directive with any query data injected. Any argument that depends on
36    /// response data will not be present here and be provided separately.
37    pub fn arguments<T>(&self) -> Result<T, SdkError>
38    where
39        T: Deserialize<'a>,
40    {
41        cbor::from_slice(match self {
42            DirectiveSite::Object(site) => &site.0.arguments,
43            DirectiveSite::FieldDefinition(site) => &site.0.arguments,
44            DirectiveSite::Interface(site) => &site.0.arguments,
45            DirectiveSite::Union(site) => &site.0.arguments,
46            DirectiveSite::Enum(site) => &site.0.arguments,
47            DirectiveSite::Scalar(site) => &site.0.arguments,
48        })
49        .map_err(Into::into)
50    }
51}
52
53/// A directive site for object types
54pub struct ObjectDirectiveSite<'a>(&'a wit::ObjectDirectiveSite);
55
56impl<'a> ObjectDirectiveSite<'a> {
57    /// The name of the object type
58    #[inline]
59    pub fn object_name(&self) -> &str {
60        &self.0.object_name
61    }
62
63    /// Arguments of the directive with any query data injected. Any argument that depends on
64    /// response data will not be present here and be provided separately.
65    pub fn arguments<T>(&self) -> Result<T, SdkError>
66    where
67        T: Deserialize<'a>,
68    {
69        cbor::from_slice(&self.0.arguments).map_err(Into::into)
70    }
71}
72
73impl<'a> From<&'a wit::ObjectDirectiveSite> for ObjectDirectiveSite<'a> {
74    fn from(value: &'a wit::ObjectDirectiveSite) -> Self {
75        Self(value)
76    }
77}
78
79/// A directive site for field definitions
80pub struct FieldDefinitionDirectiveSite<'a>(&'a wit::FieldDefinitionDirectiveSite);
81
82impl<'a> FieldDefinitionDirectiveSite<'a> {
83    /// The name of the parent type containing this field
84    #[inline]
85    pub fn parent_type_name(&self) -> &str {
86        &self.0.parent_type_name
87    }
88
89    /// The name of the field
90    #[inline]
91    pub fn field_name(&self) -> &str {
92        &self.0.field_name
93    }
94
95    /// Arguments of the directive with any query data injected. Any argument that depends on
96    /// response data will not be present here and be provided separately.
97    pub fn arguments<T>(&self) -> Result<T, SdkError>
98    where
99        T: Deserialize<'a>,
100    {
101        cbor::from_slice(&self.0.arguments).map_err(Into::into)
102    }
103}
104
105impl<'a> From<&'a wit::FieldDefinitionDirectiveSite> for FieldDefinitionDirectiveSite<'a> {
106    fn from(value: &'a wit::FieldDefinitionDirectiveSite) -> Self {
107        Self(value)
108    }
109}
110
111/// A directive site for union types
112pub struct UnionDirectiveSite<'a>(&'a wit::UnionDirectiveSite);
113
114impl<'a> UnionDirectiveSite<'a> {
115    /// The name of the union type
116    #[inline]
117    pub fn union_name(&self) -> &str {
118        &self.0.union_name
119    }
120
121    /// Arguments of the directive with any query data injected. Any argument that depends on
122    /// response data will not be present here and be provided separately.
123    pub fn arguments<T>(&self) -> Result<T, SdkError>
124    where
125        T: Deserialize<'a>,
126    {
127        cbor::from_slice(&self.0.arguments).map_err(Into::into)
128    }
129}
130
131impl<'a> From<&'a wit::UnionDirectiveSite> for UnionDirectiveSite<'a> {
132    fn from(value: &'a wit::UnionDirectiveSite) -> Self {
133        Self(value)
134    }
135}
136
137/// A directive site for interfaces
138pub struct InterfaceDirectiveSite<'a>(&'a wit::InterfaceDirectiveSite);
139
140impl<'a> InterfaceDirectiveSite<'a> {
141    /// The name of the interface type
142    #[inline]
143    pub fn interface_name(&self) -> &str {
144        &self.0.interface_name
145    }
146
147    /// Arguments of the directive with any query data injected. Any argument that depends on
148    /// response data will not be present here and be provided separately.
149    pub fn arguments<T>(&self) -> Result<T, SdkError>
150    where
151        T: Deserialize<'a>,
152    {
153        cbor::from_slice(&self.0.arguments).map_err(Into::into)
154    }
155}
156
157impl<'a> From<&'a wit::InterfaceDirectiveSite> for InterfaceDirectiveSite<'a> {
158    fn from(value: &'a wit::InterfaceDirectiveSite) -> Self {
159        Self(value)
160    }
161}
162
163/// A directive site for scalars
164pub struct ScalarDirectiveSite<'a>(&'a wit::ScalarDirectiveSite);
165
166impl<'a> ScalarDirectiveSite<'a> {
167    /// The name of the interface type
168    #[inline]
169    pub fn interface_name(&self) -> &str {
170        &self.0.scalar_name
171    }
172
173    /// Arguments of the directive with any query data injected. Any argument that depends on
174    /// response data will not be present here and be provided separately.
175    pub fn arguments<T>(&self) -> Result<T, SdkError>
176    where
177        T: Deserialize<'a>,
178    {
179        cbor::from_slice(&self.0.arguments).map_err(Into::into)
180    }
181}
182
183impl<'a> From<&'a wit::ScalarDirectiveSite> for ScalarDirectiveSite<'a> {
184    fn from(value: &'a wit::ScalarDirectiveSite) -> Self {
185        Self(value)
186    }
187}
188
189/// A directive site for enums
190pub struct EnumDirectiveSite<'a>(&'a wit::EnumDirectiveSite);
191
192impl<'a> EnumDirectiveSite<'a> {
193    /// The name of the interface type
194    #[inline]
195    pub fn interface_name(&self) -> &str {
196        &self.0.enum_name
197    }
198
199    /// Arguments of the directive with any query data injected. Any argument that depends on
200    /// response data will not be present here and be provided separately.
201    pub fn arguments<T>(&self) -> Result<T, SdkError>
202    where
203        T: Deserialize<'a>,
204    {
205        cbor::from_slice(&self.0.arguments).map_err(Into::into)
206    }
207}
208
209impl<'a> From<&'a wit::EnumDirectiveSite> for EnumDirectiveSite<'a> {
210    fn from(value: &'a wit::EnumDirectiveSite) -> Self {
211        Self(value)
212    }
213}