Skip to content

Commit

Permalink
fix: some value tweaks in parser (#1058)
Browse files Browse the repository at this point in the history
1. TryFrom<Value> for ConstValue
2. Rename ListValue -> List, ConstListValue -> ConstList
  • Loading branch information
obmarg authored Oct 3, 2024
1 parent 984fbb0 commit 49d23fc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 21 deletions.
6 changes: 3 additions & 3 deletions cynic-parser/src/values/const_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::{common::IdRange, AstLookup, Span};
use super::{const_value::ConstValue, iter::Iter, ConstValueId};

#[derive(Clone, Copy)]
pub struct ConstListValue<'a>(pub(super) super::Cursor<'a, ConstValueId>);
pub struct ConstList<'a>(pub(super) super::Cursor<'a, ConstValueId>);

impl<'a> ConstListValue<'a> {
impl<'a> ConstList<'a> {
pub fn is_empty(&self) -> bool {
self.len() == 0
}
Expand All @@ -33,7 +33,7 @@ impl<'a> ConstListValue<'a> {
}
}

impl fmt::Debug for ConstListValue<'_> {
impl fmt::Debug for ConstList<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.items()).finish()
}
Expand Down
56 changes: 52 additions & 4 deletions cynic-parser/src/values/const_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{AstLookup, Span};

use super::{
const_lists::ConstListValue,
const_lists::ConstList,
const_objects::ConstObject,
enums::EnumValue,
iter::{Iter, ValueStoreReader},
Expand All @@ -18,7 +18,7 @@ pub enum ConstValue<'a> {
Boolean(BooleanValue<'a>),
Null(NullValue<'a>),
Enum(EnumValue<'a>),
List(ConstListValue<'a>),
List(ConstList<'a>),
Object(ConstObject<'a>),
}

Expand Down Expand Up @@ -99,7 +99,7 @@ impl<'a> ConstValue<'a> {
matches!(self, Self::List(_))
}

pub fn as_list(&self) -> Option<ConstListValue<'a>> {
pub fn as_list(&self) -> Option<ConstList<'a>> {
match self {
Self::List(inner) => Some(*inner),
_ => None,
Expand Down Expand Up @@ -176,8 +176,56 @@ impl super::ValueStoreId for ConstValueId {
ValueKind::Boolean(_) => ConstValue::Boolean(BooleanValue(value_cursor)),
ValueKind::Null => ConstValue::Null(NullValue(value_cursor)),
ValueKind::Enum(_) => ConstValue::Enum(EnumValue(value_cursor)),
ValueKind::List(_) => ConstValue::List(ConstListValue(cursor)),
ValueKind::List(_) => ConstValue::List(ConstList(cursor)),
ValueKind::Object(_) => ConstValue::Object(ConstObject(cursor)),
}
}
}

impl<'a> TryFrom<Value<'a>> for ConstValue<'a> {
type Error = ();

fn try_from(value: Value<'a>) -> Result<Self, Self::Error> {
if !const_safe(value) {
return Err(());
}

Ok(match value {
Value::Variable(_) => unreachable!(),
Value::Int(int_value) => ConstValue::Int(int_value),
Value::Float(float_value) => ConstValue::Float(float_value),
Value::String(string_value) => ConstValue::String(string_value),
Value::Boolean(boolean_value) => ConstValue::Boolean(boolean_value),
Value::Null(null_value) => ConstValue::Null(null_value),
Value::Enum(enum_value) => ConstValue::Enum(enum_value),
Value::List(list_value) => {
let id = ConstValueId::new(list_value.0.id.get());
ConstValue::List(ConstList(Cursor {
id,
store: list_value.0.store,
}))
}
Value::Object(object) => {
let id = ConstValueId::new(object.0.id.get());
ConstValue::Object(ConstObject(Cursor {
id,
store: object.0.store,
}))
}
})
}
}

fn const_safe(value: Value<'_>) -> bool {
match value {
Value::Variable(_) => false,
Value::Int(_)
| Value::Float(_)
| Value::String(_)
| Value::Boolean(_)
| Value::Null(_)
| Value::Enum(_) => true,
Value::List(items) => items.items().any(const_safe),
Value::Object(object) => object.fields().any(|field| const_safe(field.value())),
}
}
16 changes: 8 additions & 8 deletions cynic-parser/src/values/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::fmt;

use crate::{AstLookup, Span};

use super::{const_lists::ConstListValue, ids::ValueId, iter::Iter, value::Value, Cursor};
use super::{const_lists::ConstList, ids::ValueId, iter::Iter, value::Value, Cursor};

#[derive(Clone, Copy)]
pub struct ListValue<'a>(pub(super) super::Cursor<'a, ValueId>);
pub struct List<'a>(pub(super) super::Cursor<'a, ValueId>);

impl<'a> ListValue<'a> {
impl<'a> List<'a> {
pub fn is_empty(&self) -> bool {
self.len() == 0
}
Expand All @@ -28,24 +28,24 @@ impl<'a> ListValue<'a> {
}
}

impl PartialEq for ListValue<'_> {
impl PartialEq for List<'_> {
fn eq(&self, other: &Self) -> bool {
self.len() == other.len() && self.items().zip(other.items()).all(|(lhs, rhs)| lhs == rhs)
}
}

impl fmt::Debug for ListValue<'_> {
impl fmt::Debug for List<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.items()).finish()
}
}

impl<'a> From<ConstListValue<'a>> for ListValue<'a> {
fn from(value: ConstListValue<'a>) -> Self {
impl<'a> From<ConstList<'a>> for List<'a> {
fn from(value: ConstList<'a>) -> Self {
let Cursor { id, store } = value.0;

let id = id.into();

ListValue(Cursor { id, store })
List(Cursor { id, store })
}
}
4 changes: 2 additions & 2 deletions cynic-parser/src/values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub mod writer;
use std::sync::Arc;

pub use self::{
const_lists::ConstListValue,
const_lists::ConstList,
const_objects::{ConstObject, ConstObjectField},
const_value::ConstValue,
enums::EnumValue,
lists::ListValue,
lists::List,
objects::{Object, ObjectField},
scalars::{BooleanValue, FloatValue, IntValue, NullValue, StringValue},
value::Value,
Expand Down
8 changes: 4 additions & 4 deletions cynic-parser/src/values/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
enums::EnumValue,
ids::{FieldId, StringId},
iter::{Iter, ValueStoreReader},
lists::ListValue,
lists::List,
objects::Object,
scalars::{BooleanValue, FloatValue, IntValue, NullValue, StringValue},
variables::VariableValue,
Expand All @@ -21,7 +21,7 @@ pub enum Value<'a> {
Boolean(BooleanValue<'a>),
Null(NullValue<'a>),
Enum(EnumValue<'a>),
List(ListValue<'a>),
List(List<'a>),
Object(Object<'a>),
}

Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'a> Value<'a> {
matches!(self, Value::List(_))
}

pub fn as_list(&self) -> Option<ListValue<'a>> {
pub fn as_list(&self) -> Option<List<'a>> {
match self {
Self::List(inner) => Some(*inner),
_ => None,
Expand Down Expand Up @@ -201,7 +201,7 @@ impl super::ValueStoreId for ValueId {
ValueKind::Boolean(_) => Value::Boolean(BooleanValue(cursor)),
ValueKind::Null => Value::Null(NullValue(cursor)),
ValueKind::Enum(_) => Value::Enum(EnumValue(cursor)),
ValueKind::List(_) => Value::List(ListValue(cursor)),
ValueKind::List(_) => Value::List(List(cursor)),
ValueKind::Object(_) => Value::Object(Object(cursor)),
}
}
Expand Down

0 comments on commit 49d23fc

Please sign in to comment.