rusty_gql/types/
scalar.rs1use graphql_parser::{
2 schema::{ScalarType as ParserScalarType, Value},
3 Pos,
4};
5
6use super::directive::GqlDirective;
7
8#[derive(Debug, Clone)]
9pub struct ScalarType {
10 pub name: String,
11 pub description: Option<String>,
12 pub position: Pos,
13 pub directives: Vec<GqlDirective>,
14}
15
16impl<'a> From<ParserScalarType<'a, String>> for ScalarType {
17 fn from(scalar_type: ParserScalarType<'a, String>) -> Self {
18 let directives = GqlDirective::from_vec_directive(scalar_type.directives);
19 ScalarType {
20 name: scalar_type.name,
21 description: scalar_type.description,
22 position: scalar_type.position,
23 directives,
24 }
25 }
26}
27
28impl ScalarType {
29 pub fn is_valid_value(&self, value: &Value<'_, String>) -> bool {
30 match value {
31 Value::Variable(_) => false,
32 Value::Int(_) => self.name == *"Int",
33 Value::Float(_) => self.name == *"Float",
34 Value::String(_) => self.name == *"String",
35 Value::Boolean(_) => self.name == *"Boolean",
36 Value::Null => true,
37 Value::Enum(_) => false,
38 Value::List(_) => false,
39 Value::Object(_) => false,
40 }
41 }
42
43 pub fn string_scalar() -> Self {
44 ScalarType {
45 name: "String".to_string(),
46 description: None,
47 position: Pos::default(),
48 directives: vec![],
49 }
50 }
51
52 pub fn int_scalar() -> Self {
53 ScalarType {
54 name: "Int".to_string(),
55 description: None,
56 position: Pos::default(),
57 directives: vec![],
58 }
59 }
60
61 pub fn float_scalar() -> Self {
62 ScalarType {
63 name: "Float".to_string(),
64 description: None,
65 position: Pos::default(),
66 directives: vec![],
67 }
68 }
69
70 pub fn boolean_scalar() -> Self {
71 ScalarType {
72 name: "Boolean".to_string(),
73 description: None,
74 position: Pos::default(),
75 directives: vec![],
76 }
77 }
78
79 pub fn id_scalar() -> Self {
80 ScalarType {
81 name: "ID".to_string(),
82 description: None,
83 position: Pos::default(),
84 directives: vec![],
85 }
86 }
87}