Skip to main content

libgraphql_core/operation/
variable.rs

1use crate::loc;
2use crate::named_ref::DerefByName;
3use crate::named_ref::DerefByNameError;
4use crate::named_ref::NamedRef;
5use crate::types::TypeAnnotation;
6use crate::Value;
7use std::collections::HashMap;
8
9#[derive(Clone, Debug, PartialEq)]
10pub struct Variable {
11    pub(crate) def_location: loc::SourceLocation,
12    pub(crate) default_value: Option<Value>,
13    pub(crate) name: String,
14    pub(crate) type_annotation: TypeAnnotation,
15}
16impl Variable {
17    pub fn def_location(&self) -> &loc::SourceLocation {
18        &self.def_location
19    }
20
21    pub fn default_value(&self) -> Option<&Value> {
22        self.default_value.as_ref()
23    }
24
25    pub fn name(&self) -> &str {
26        self.name.as_str()
27    }
28
29    pub fn type_annotation(&self) -> &TypeAnnotation {
30        &self.type_annotation
31    }
32}
33impl DerefByName for Variable {
34    type Source = HashMap<String, Variable>;
35    type RefLocation = loc::SourceLocation;
36
37    fn deref_name<'a>(
38        vardef_map: &'a Self::Source,
39        name: &str,
40    ) -> Result<&'a Self, DerefByNameError> {
41        vardef_map.get(name).ok_or_else(
42            || DerefByNameError::DanglingReference(name.to_string()),
43        )
44    }
45}
46
47pub type NamedVariableRef = NamedRef<
48    /* TSource = */ HashMap<String, Variable>,
49    /* TRefLocation = */ loc::SourceLocation,
50    /* TResource = */ Variable,
51>;