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 def_location: loc::SourceLocation,
12    pub default_value: Option<Value>,
13    pub name: String,
14    pub type_: TypeAnnotation,
15}
16impl Variable {
17    pub fn def_location(&self) -> &loc::SourceLocation {
18        &self.def_location
19    }
20}
21impl DerefByName for Variable {
22    type Source = HashMap<String, Variable>;
23    type RefLocation = loc::SourceLocation;
24
25    fn deref_name<'a>(
26        vardef_map: &'a Self::Source,
27        name: &str,
28    ) -> Result<&'a Self, DerefByNameError> {
29        vardef_map.get(name).ok_or_else(
30            || DerefByNameError::DanglingReference(name.to_string()),
31        )
32    }
33}
34
35pub type NamedVariableRef = NamedRef<
36    /* TSource = */ HashMap<String, Variable>,
37    /* TRefLocation = */ loc::SourceLocation,
38    /* TResource = */ Variable,
39>;