Skip to content

Commit

Permalink
cynic-parser: implement PartialEq for Type and Value (#924)
Browse files Browse the repository at this point in the history
I am conflicted on whether this should be PartialEq or a method, but
since there is only one useful way to compare {types,values}, I went
with PartialEq, as well as Eq for types (Value has a Float variant).
  • Loading branch information
tomhoule authored Apr 16, 2024
1 parent 980ddd0 commit 9de383b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cynic-parser/src/executable/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ pub struct TypeRecord {
#[derive(Clone, Copy)]
pub struct Type<'a>(ReadContext<'a, TypeId>);

impl PartialEq for Type<'_> {
fn eq(&self, other: &Self) -> bool {
self.name() == other.name() && self.wrappers().eq(other.wrappers())
}
}

impl Eq for Type<'_> {}

impl<'a> Type<'a> {
pub fn name(&self) -> &'a str {
self.0
Expand Down
26 changes: 26 additions & 0 deletions cynic-parser/src/executable/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ pub enum Value<'a> {
Object(Vec<(&'a str, Value<'a>)>),
}

impl PartialEq for Value<'_> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Value::Int(a), Value::Int(b)) => a == b,
(Value::Variable(a), Value::Variable(b)) => a == b,
(Value::Float(a), Value::Float(b)) => a == b,
(Value::String(a), Value::String(b)) => a == b,
(Value::Boolean(a), Value::Boolean(b)) => a == b,
(Value::Null, Value::Null) => true,
(Value::Enum(a), Value::Enum(b)) => a == b,
(Value::List(a), Value::List(b)) => a == b,
(Value::Object(a), Value::Object(b)) => {
a.len() == b.len()
&& a.iter().all(|(name, value)| {
let Some((_, b_value)) = b.iter().find(|(b_name, _)| b_name == name) else {
return false;
};

value == b_value
})
}
_ => false,
}
}
}

impl ExecutableId for ValueId {
type Reader<'a> = Value<'a>;
}
Expand Down
8 changes: 8 additions & 0 deletions cynic-parser/src/type_system/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ pub struct TypeRecord {
#[derive(Clone, Copy)]
pub struct Type<'a>(ReadContext<'a, TypeId>);

impl PartialEq for Type<'_> {
fn eq(&self, other: &Self) -> bool {
self.name() == other.name() && self.wrappers().eq(other.wrappers())
}
}

impl Eq for Type<'_> {}

impl<'a> Type<'a> {
pub fn name(&self) -> &'a str {
self.0
Expand Down
29 changes: 29 additions & 0 deletions cynic-parser/src/type_system/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,35 @@ pub enum Value<'a> {
Object(Vec<(&'a str, Value<'a>)>),
}

impl PartialEq for Value<'_> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Value::Int(a), Value::Int(b)) => a == b,
(Value::Variable(a), Value::Variable(b)) => a == b,
(Value::Float(a), Value::Float(b)) => a == b,
(Value::String(a), Value::BlockString(b))
| (Value::String(a), Value::String(b))
| (Value::BlockString(a), Value::String(b))
| (Value::BlockString(a), Value::BlockString(b)) => a == b,
(Value::Boolean(a), Value::Boolean(b)) => a == b,
(Value::Null, Value::Null) => true,
(Value::Enum(a), Value::Enum(b)) => a == b,
(Value::List(a), Value::List(b)) => a == b,
(Value::Object(a), Value::Object(b)) => {
a.len() == b.len()
&& a.iter().all(|(name, value)| {
let Some((_, b_value)) = b.iter().find(|(b_name, _)| b_name == name) else {
return false;
};

value == b_value
})
}
_ => false,
}
}
}

impl<'a> Value<'a> {
pub fn as_str(&self) -> Option<&'a str> {
match self {
Expand Down

0 comments on commit 9de383b

Please sign in to comment.