coercion the easy cases

main
Ziyang Hu 2 years ago
parent fd32670b35
commit 98156906e7

@ -4,6 +4,7 @@ use thiserror::Error;
use cozorocks::BridgeError;
use crate::parser::Rule;
use crate::relation::data::DataKind;
use crate::relation::value::{StaticValue, Value};
#[derive(Error, Debug)]
pub enum CozoError {
@ -79,6 +80,12 @@ pub enum CozoError {
#[error(transparent)]
Parse(#[from] pest::error::Error<Rule>),
#[error("Not null constraint violated")]
NotNullViolated(StaticValue),
#[error("Type mismatch")]
TypeMismatch,
// #[error(transparent)]
// Storage(#[from] cozo_rocks::BridgeStatus),
//

@ -107,6 +107,79 @@ impl Typing {
_ => unreachable!()
})
}
pub fn is_nullable(&self) -> bool {
matches!(self, Typing::Any | Typing::Nullable(_))
}
pub fn coerce<'a>(&self, v: Value<'a>) -> Result<Value<'a>> {
if *self == Typing::Any {
return Ok(v);
}
if v == Value::Null {
return if matches!(self, Typing::Nullable(_)) {
Ok(Value::Null)
} else {
Err(CozoError::NotNullViolated(v.to_static()))
};
}
if let Typing::Nullable(t) = self {
return t.coerce(v);
}
match self {
Typing::Any => unreachable!(),
Typing::Nullable(_) => unreachable!(),
Typing::Bool => self.coerce_bool(v),
Typing::Int => self.coerce_int(v),
Typing::Float => self.coerce_float(v),
Typing::Text => self.coerce_text(v),
Typing::Uuid => self.coerce_uuid(v),
Typing::Homogeneous(t) => {
match v {
Value::List(vs) => {
Ok(Value::List(vs.into_iter().map(|v| t.coerce(v)).collect::<Result<Vec<_>>>()?))
}
_ => Err(CozoError::TypeMismatch)
}
}
Typing::UnnamedTuple(ut) => {
todo!()
},
Typing::NamedTuple(nt) => {
todo!()
}
}
}
fn coerce_bool<'a>(&self, v: Value<'a>) -> Result<Value<'a>> {
match v {
v @ Value::Bool(_) => Ok(v),
_ => Err(CozoError::TypeMismatch)
}
}
fn coerce_int<'a>(&self, v: Value<'a>) -> Result<Value<'a>> {
match v {
v @ Value::Int(_) => Ok(v),
_ => Err(CozoError::TypeMismatch)
}
}
fn coerce_float<'a>(&self, v: Value<'a>) -> Result<Value<'a>> {
match v {
v @ Value::Float(_) => Ok(v),
_ => Err(CozoError::TypeMismatch)
}
}
fn coerce_text<'a>(&self, v: Value<'a>) -> Result<Value<'a>> {
match v {
v @ Value::Text(_) => Ok(v),
_ => Err(CozoError::TypeMismatch)
}
}
fn coerce_uuid<'a>(&self, v: Value<'a>) -> Result<Value<'a>> {
match v {
v @ Value::Uuid(_) => Ok(v),
_ => Err(CozoError::TypeMismatch)
}
}
}
impl TryFrom<&str> for Typing {

Loading…
Cancel
Save