Skip to content

Commit 7b6bf74

Browse files
authored
Merge pull request #19 from zaeleus/id
Implement traits for ID to make it usable outside its module
2 parents 907d78d + b61281b commit 7b6bf74

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/types/scalars.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use std::convert::From;
12
use std::marker::PhantomData;
3+
use std::ops::Deref;
24

35
use ast::{InputValue, Selection, FromInputValue, ToInputValue};
46
use value::Value;
@@ -11,8 +13,23 @@ use types::base::GraphQLType;
1113
/// An ID as defined by the GraphQL specification
1214
///
1315
/// Represented as a string, but can be converted _to_ from an integer as well.
16+
#[derive(Clone, Debug, Eq, PartialEq)]
1417
pub struct ID(String);
1518

19+
impl From<String> for ID {
20+
fn from(s: String) -> ID {
21+
ID(s)
22+
}
23+
}
24+
25+
impl Deref for ID {
26+
type Target = str;
27+
28+
fn deref(&self) -> &str {
29+
&self.0
30+
}
31+
}
32+
1633
graphql_scalar!(ID as "ID" {
1734
resolve(&self) -> Value {
1835
Value::string(&self.0)
@@ -156,3 +173,21 @@ impl<T> GraphQLType for EmptyMutation<T> {
156173
registry.build_object_type::<Self>(&[]).into_meta()
157174
}
158175
}
176+
177+
#[cfg(test)]
178+
mod tests {
179+
use super::ID;
180+
181+
#[test]
182+
fn test_id_from_string() {
183+
let actual = ID::from(String::from("foo"));
184+
let expected = ID(String::from("foo"));
185+
assert_eq!(actual, expected);
186+
}
187+
188+
#[test]
189+
fn test_id_deref() {
190+
let id = ID(String::from("foo"));
191+
assert_eq!(id.len(), 3);
192+
}
193+
}

0 commit comments

Comments
 (0)