Skip to content

Commit

Permalink
More on value_set_VALUE
Browse files Browse the repository at this point in the history
  • Loading branch information
labra committed Nov 22, 2023
1 parent 09ca021 commit 958cb76
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 167 deletions.
30 changes: 15 additions & 15 deletions shex_ast/src/ast/node_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::{
use super::ValueSetValue;
use crate::{
Deref, DerefError, IriRef, NodeKind, NumericFacet, NumericLiteral, Pattern, StringFacet,
ValueSetValueWrapper, XsFacet,
XsFacet,
};
use serde::ser::SerializeMap;

Expand All @@ -26,7 +26,7 @@ pub struct NodeConstraint {
xs_facet: Option<Vec<XsFacet>>,

// #[serde(default, skip_serializing_if = "Option::is_none")]
values: Option<Vec<ValueSetValueWrapper>>,
values: Option<Vec<ValueSetValue>>,
}

impl NodeConstraint {
Expand Down Expand Up @@ -131,28 +131,28 @@ impl NodeConstraint {
}

pub fn with_values(mut self, values: Vec<ValueSetValue>) -> Self {
let mut vs: Vec<ValueSetValueWrapper> = Vec::with_capacity(values.len());
let mut vs: Vec<ValueSetValue> = Vec::with_capacity(values.len());
for v in values {
vs.push(ValueSetValueWrapper::new(v));
vs.push(v);
}
self.values = Some(vs);
self
}

pub fn with_values_wrapped(mut self, values: Vec<ValueSetValueWrapper>) -> Self {
/* pub fn with_values_wrapped(mut self, values: Vec<ValueSetValueWrapper>) -> Self {
self.values = Some(values);
self
}
} */

pub fn values(&self) -> Option<Vec<ValueSetValue>> {
match &self.values {
None => None,
Some(values) => {
let mut vs: Vec<ValueSetValue> = Vec::with_capacity(values.len());
for v in values {
vs.push(v.value());
Some(vs) => {
let mut r = Vec::new();
for v in vs {
r.push((*v).clone())
}
Some(vs)
Some(r)
}
}
}
Expand All @@ -168,7 +168,7 @@ impl Deref for NodeConstraint {
Self: Sized,
{
let datatype = <IriRef as Deref>::deref_opt(&self.datatype, base, prefixmap)?;
let values = <ValueSetValueWrapper as Deref>::deref_opt_vec(&self.values, base, prefixmap)?;
let values = <ValueSetValue as Deref>::deref_opt_vec(&self.values, base, prefixmap)?;
Ok(NodeConstraint {
node_kind: self.node_kind.clone(),
datatype,
Expand Down Expand Up @@ -363,7 +363,7 @@ impl<'de> Deserialize<'de> for NodeConstraint {
let mut totaldigits: Option<usize> = None;
let mut fractiondigits: Option<usize> = None;
let mut flags: Option<String> = None;
let mut values: Option<Vec<ValueSetValueWrapper>> = None;
let mut values: Option<Vec<ValueSetValue>> = None;
while let Some(key) = map.next_key()? {
match key {
Field::NodeKind => {
Expand Down Expand Up @@ -394,7 +394,7 @@ impl<'de> Deserialize<'de> for NodeConstraint {
if values.is_some() {
return Err(de::Error::duplicate_field("values"));
}
let vs: Vec<ValueSetValueWrapper> = map.next_value()?;
let vs: Vec<ValueSetValue> = map.next_value()?;
values = Some(vs)
}
Field::Pattern => {
Expand Down Expand Up @@ -495,7 +495,7 @@ impl<'de> Deserialize<'de> for NodeConstraint {
nc = nc.with_datatype(datatype)
}
if let Some(values) = values {
nc = nc.with_values_wrapped(values)
nc = nc.with_values(values)
}
if let Some(minlength) = minlength {
nc = nc.with_minlength(minlength)
Expand Down
18 changes: 18 additions & 0 deletions shex_ast/src/ast/object_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ pub enum ObjectValue {

NumericLiteral(NumericLiteral),

#[serde(serialize_with = "serialize_boolean_literal")]
BooleanLiteral {
value: bool,
},

ObjectLiteral {
value: String,

Expand Down Expand Up @@ -65,6 +70,9 @@ impl Deref for ObjectValue {
Ok(ObjectValue::IriRef(new_iri_ref))
}
ObjectValue::NumericLiteral(n) => Ok(ObjectValue::NumericLiteral(n.clone())),
ObjectValue::BooleanLiteral { value } => Ok(ObjectValue::BooleanLiteral {
value: value.clone(),
}),
ObjectValue::ObjectLiteral {
value,
language,
Expand Down Expand Up @@ -128,3 +136,13 @@ impl Deref for ObjectValueWrapper {
Ok(ObjectValueWrapper { ov })
}
}

fn serialize_boolean_literal<S>(value: &bool, serializer: S) -> result::Result<S::Ok, S::Error>
where
S: Serializer,
{
match value {
false => serializer.serialize_str("false"),
true => serializer.serialize_str("true"),
}
}
5 changes: 4 additions & 1 deletion shex_ast/src/ast/schema_json_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ fn cnv_value(v: &ast::ValueSetValue) -> CResult<ValueSetValue> {
Ok(ValueSetValue::IriStem { stem: cnv_stem })
}
ast::ValueSetValue::ObjectValue(ovw) => {
let ov = cnv_object_value(&ovw.ov)?;
let ov = cnv_object_value(&ovw)?;
Ok(ValueSetValue::ObjectValue(ov))
}
ast::ValueSetValue::Language { language_tag, .. } => Ok(ValueSetValue::Language {
Expand Down Expand Up @@ -630,6 +630,9 @@ fn cnv_object_value(ov: &ast::ObjectValue) -> CResult<ObjectValue> {
ast::ObjectValue::NumericLiteral(n) => {
todo!()
}
ast::ObjectValue::BooleanLiteral { value } => {
todo!()
}
ast::ObjectValue::ObjectLiteral {
value, language, ..
} => Ok(ObjectValue::ObjectLiteral {
Expand Down
Loading

0 comments on commit 958cb76

Please sign in to comment.