grafbase_sdk/types/
directive.rs1use serde::Deserialize;
2
3use crate::{SdkError, cbor, wit};
4
5use super::FieldDefinitionDirectiveSite;
6
7pub struct SchemaDirective<'a> {
9 pub(crate) subgraph_name: &'a str,
10 pub(crate) directive: &'a wit::Directive,
11}
12
13impl<'a> SchemaDirective<'a> {
14 #[inline]
16 pub fn name(&self) -> &'a str {
17 &self.directive.name
18 }
19
20 #[inline]
22 pub fn subgraph_name(&self) -> &'a str {
23 self.subgraph_name
24 }
25
26 #[inline]
31 pub fn arguments<T>(&'a self) -> Result<T, SdkError>
32 where
33 T: Deserialize<'a>,
34 {
35 cbor::from_slice(&self.directive.arguments).map_err(Into::into)
36 }
37}
38
39pub struct FieldDefinitionDirective<'a>(&'a wit::FieldDefinitionDirective);
41
42impl<'a> FieldDefinitionDirective<'a> {
43 #[inline]
45 pub fn name(&self) -> &'a str {
46 &self.0.name
47 }
48
49 pub fn arguments<T>(&self) -> Result<T, SdkError>
52 where
53 T: Deserialize<'a>,
54 {
55 minicbor_serde::from_slice(&self.0.arguments).map_err(Into::into)
56 }
57
58 pub fn arguments_seed<T>(&self, seed: T) -> Result<T::Value, SdkError>
60 where
61 T: serde::de::DeserializeSeed<'a>,
62 {
63 let mut deserializer = minicbor_serde::Deserializer::new(&self.0.arguments);
64 seed.deserialize(&mut deserializer).map_err(From::from)
65 }
66
67 pub fn arguments_bytes(&self) -> &[u8] {
69 &self.0.arguments
70 }
71
72 #[inline]
74 pub fn site(&self) -> FieldDefinitionDirectiveSite<'a> {
75 (&self.0.site).into()
76 }
77}
78
79impl<'a> From<&'a wit::FieldDefinitionDirective> for FieldDefinitionDirective<'a> {
80 fn from(value: &'a wit::FieldDefinitionDirective) -> Self {
81 Self(value)
82 }
83}