Derive Serialize implementations for enums (#1887)

This commit is contained in:
Rushmore Mushambi 2023-04-29 22:50:25 +02:00 committed by GitHub
parent 3f3b2248b3
commit 1e8903b699
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 25 additions and 185 deletions

View file

@ -334,7 +334,7 @@ impl Iterable {
// Parse the data from the store
let gra: crate::key::graph::Graph = (&k).into();
// Fetch the data from the store
let key = thing::new(opt.ns(), opt.db(), &gra.ft, &gra.fk);
let key = thing::new(opt.ns(), opt.db(), gra.ft, &gra.fk);
let val = txn.clone().lock().await.get(key).await?;
let rid = Thing::from((gra.ft, gra.fk));
// Parse the data from the store

View file

@ -325,7 +325,7 @@ impl Iterable {
// Parse the data from the store
let gra: crate::key::graph::Graph = (&k).into();
// Fetch the data from the store
let key = thing::new(opt.ns(), opt.db(), &gra.ft, &gra.fk);
let key = thing::new(opt.ns(), opt.db(), gra.ft, &gra.fk);
let val = txn.clone().lock().await.get(key).await?;
let rid = Thing::from((gra.ft, gra.fk));
// Parse the data from the store

View file

@ -8,7 +8,7 @@ pub struct Namespace<'a> {
pub ns: &'a str,
}
pub fn new<'a>(ns: &'a str) -> Namespace<'a> {
pub fn new(ns: &str) -> Namespace<'_> {
Namespace::new(ns)
}

View file

@ -10,7 +10,7 @@ pub struct Ns<'a> {
pub ns: &'a str,
}
pub fn new<'a>(ns: &'a str) -> Ns<'a> {
pub fn new(ns: &str) -> Ns<'_> {
Ns::new(ns)
}

View file

@ -14,7 +14,8 @@ use std::fmt;
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Constant";
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Deserialize, Store, Hash)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[serde(rename = "$surrealdb::private::sql::Constant")]
pub enum Constant {
MathE,
MathFrac1Pi,
@ -35,6 +36,7 @@ pub enum Constant {
MathPi,
MathSqrt2,
MathTau,
// Add new variants here
}
impl Constant {
@ -99,36 +101,6 @@ impl fmt::Display for Constant {
}
}
#[rustfmt::skip]
impl Serialize for Constant {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::MathE => s.serialize_unit_variant(TOKEN, 0, "MathE"),
Self::MathFrac1Pi => s.serialize_unit_variant(TOKEN, 1, "MathFrac1Pi"),
Self::MathFrac1Sqrt2 => s.serialize_unit_variant(TOKEN, 2, "MathFrac1Sqrt2"),
Self::MathFrac2Pi => s.serialize_unit_variant(TOKEN, 3, "MathFrac2Pi"),
Self::MathFrac2SqrtPi => s.serialize_unit_variant(TOKEN, 4, "MathFrac2SqrtPi"),
Self::MathFracPi2 => s.serialize_unit_variant(TOKEN, 5, "MathFracPi2"),
Self::MathFracPi3 => s.serialize_unit_variant(TOKEN, 6, "MathFracPi3"),
Self::MathFracPi4 => s.serialize_unit_variant(TOKEN, 7, "MathFracPi4"),
Self::MathFracPi6 => s.serialize_unit_variant(TOKEN, 8, "MathFracPi6"),
Self::MathFracPi8 => s.serialize_unit_variant(TOKEN, 9, "MathFracPi8"),
Self::MathLn10 => s.serialize_unit_variant(TOKEN, 10, "MathLn10"),
Self::MathLn2 => s.serialize_unit_variant(TOKEN, 11, "MathLn2"),
Self::MathLog102 => s.serialize_unit_variant(TOKEN, 12, "MathLog102"),
Self::MathLog10E => s.serialize_unit_variant(TOKEN, 13, "MathLog10E"),
Self::MathLog210 => s.serialize_unit_variant(TOKEN, 14, "MathLog210"),
Self::MathLog2E => s.serialize_unit_variant(TOKEN, 15, "MathLog2E"),
Self::MathPi => s.serialize_unit_variant(TOKEN, 16, "MathPi"),
Self::MathSqrt2 => s.serialize_unit_variant(TOKEN, 17, "MathSqrt2"),
Self::MathTau => s.serialize_unit_variant(TOKEN, 18, "MathTau"),
}
}
}
pub fn constant(i: &str) -> IResult<&str, Constant> {
alt((constant_math,))(i)
}

View file

@ -23,19 +23,20 @@ use nom::multi::separated_list0;
use nom::multi::separated_list1;
use nom::sequence::delimited;
use nom::sequence::preceded;
use serde::ser::SerializeTupleVariant;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt;
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Function";
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Hash)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[serde(rename = "$surrealdb::private::sql::Function")]
pub enum Function {
Cast(Kind, Value),
Normal(String, Vec<Value>),
Custom(String, Vec<Value>),
Script(Script, Vec<Value>),
// Add new variants here
}
impl PartialOrd for Function {
@ -221,40 +222,6 @@ impl fmt::Display for Function {
}
}
impl Serialize for Function {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::Cast(s, e) => {
let mut serializer = serializer.serialize_tuple_variant(TOKEN, 0, "Cast", 2)?;
serializer.serialize_field(s)?;
serializer.serialize_field(e)?;
serializer.end()
}
Self::Normal(s, e) => {
let mut serializer = serializer.serialize_tuple_variant(TOKEN, 1, "Normal", 2)?;
serializer.serialize_field(s)?;
serializer.serialize_field(e)?;
serializer.end()
}
Self::Custom(s, e) => {
let mut serializer = serializer.serialize_tuple_variant(TOKEN, 2, "Custom", 2)?;
serializer.serialize_field(s)?;
serializer.serialize_field(e)?;
serializer.end()
}
Self::Script(s, e) => {
let mut serializer = serializer.serialize_tuple_variant(TOKEN, 3, "Script", 2)?;
serializer.serialize_field(s)?;
serializer.serialize_field(e)?;
serializer.end()
}
}
}
}
pub fn function(i: &str) -> IResult<&str, Function> {
alt((normal, custom, script, cast))(i)
}

View file

@ -27,7 +27,8 @@ pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Geometry";
const SINGLE: char = '\'';
const DOUBLE: char = '\"';
#[derive(Clone, Debug, PartialEq, Deserialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename = "$surrealdb::private::sql::Geometry")]
pub enum Geometry {
Point(Point<f64>),
Line(LineString<f64>),
@ -36,6 +37,7 @@ pub enum Geometry {
MultiLine(MultiLineString<f64>),
MultiPolygon(MultiPolygon<f64>),
Collection(Vec<Geometry>),
// Add new variants here
}
impl PartialOrd for Geometry {
@ -464,23 +466,6 @@ impl fmt::Display for Geometry {
}
}
impl Serialize for Geometry {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::Point(v) => s.serialize_newtype_variant(TOKEN, 0, "Point", v),
Self::Line(v) => s.serialize_newtype_variant(TOKEN, 1, "Line", v),
Self::Polygon(v) => s.serialize_newtype_variant(TOKEN, 2, "Polygon", v),
Self::MultiPoint(v) => s.serialize_newtype_variant(TOKEN, 3, "MultiPoint", v),
Self::MultiLine(v) => s.serialize_newtype_variant(TOKEN, 4, "MultiLine", v),
Self::MultiPolygon(v) => s.serialize_newtype_variant(TOKEN, 5, "MultiPolygon", v),
Self::Collection(v) => s.serialize_newtype_variant(TOKEN, 6, "Collection", v),
}
}
}
impl hash::Hash for Geometry {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
match self {

View file

@ -6,7 +6,6 @@ use crate::sql::ident::ident_raw;
use crate::sql::thing::Thing;
use nom::branch::alt;
use nom::character::complete::char;
use serde::ser::SerializeTupleVariant;
use serde::{Deserialize, Serialize};
use std::fmt;
@ -47,10 +46,12 @@ impl Iterator for IntoIter {
}
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Deserialize, Hash)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[serde(rename = "$surrealdb::private::sql::Model")]
pub enum Model {
Count(String, u64),
Range(String, u64, u64),
// Add new variants here
}
impl IntoIterator for Model {
@ -77,29 +78,6 @@ impl fmt::Display for Model {
}
}
impl Serialize for Model {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::Count(tb, c) => {
let mut serializer = serializer.serialize_tuple_variant(TOKEN, 0, "Count", 2)?;
serializer.serialize_field(tb)?;
serializer.serialize_field(c)?;
serializer.end()
}
Self::Range(tb, b, e) => {
let mut serializer = serializer.serialize_tuple_variant(TOKEN, 1, "Range", 3)?;
serializer.serialize_field(tb)?;
serializer.serialize_field(b)?;
serializer.serialize_field(e)?;
serializer.end()
}
}
}
}
pub fn model(i: &str) -> IResult<&str, Model> {
alt((model_count, model_range))(i)
}

View file

@ -22,11 +22,13 @@ use std::str::FromStr;
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Number";
#[derive(Clone, Debug, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename = "$surrealdb::private::sql::Number")]
pub enum Number {
Int(i64),
Float(f64),
Decimal(BigDecimal),
// Add new variants here
}
impl Default for Number {
@ -166,19 +168,6 @@ impl Display for Number {
}
}
impl Serialize for Number {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Number::Int(v) => s.serialize_newtype_variant(TOKEN, 0, "Int", v),
Number::Float(v) => s.serialize_newtype_variant(TOKEN, 1, "Float", v),
Number::Decimal(v) => s.serialize_newtype_variant(TOKEN, 2, "Decimal", v),
}
}
}
impl Number {
// -----------------------------------
// Constants

View file

@ -23,7 +23,8 @@ use std::fmt::{self, Display, Formatter};
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Subquery";
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Hash)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[serde(rename = "$surrealdb::private::sql::Subquery")]
pub enum Subquery {
Value(Value),
Ifelse(IfelseStatement),
@ -34,6 +35,7 @@ pub enum Subquery {
Delete(DeleteStatement),
Relate(RelateStatement),
Insert(InsertStatement),
// Add new variants here
}
impl PartialOrd for Subquery {
@ -224,25 +226,6 @@ impl Display for Subquery {
}
}
impl Serialize for Subquery {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Self::Value(v) => serializer.serialize_newtype_variant(TOKEN, 0, "Value", v),
Self::Ifelse(v) => serializer.serialize_newtype_variant(TOKEN, 1, "Ifelse", v),
Self::Output(v) => serializer.serialize_newtype_variant(TOKEN, 2, "Output", v),
Self::Select(v) => serializer.serialize_newtype_variant(TOKEN, 3, "Select", v),
Self::Create(v) => serializer.serialize_newtype_variant(TOKEN, 4, "Create", v),
Self::Update(v) => serializer.serialize_newtype_variant(TOKEN, 5, "Update", v),
Self::Delete(v) => serializer.serialize_newtype_variant(TOKEN, 6, "Delete", v),
Self::Relate(v) => serializer.serialize_newtype_variant(TOKEN, 7, "Relate", v),
Self::Insert(v) => serializer.serialize_newtype_variant(TOKEN, 8, "Insert", v),
}
}
}
pub fn subquery(i: &str) -> IResult<&str, Subquery> {
alt((subquery_ifelse, subquery_other, subquery_value))(i)
}

View file

@ -103,7 +103,8 @@ pub fn whats(i: &str) -> IResult<&str, Values> {
Ok((i, Values(v)))
}
#[derive(Clone, Debug, Default, PartialEq, PartialOrd, Deserialize, Store, Hash)]
#[derive(Clone, Debug, Default, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[serde(rename = "$surrealdb::private::sql::Value")]
#[format(Named)]
pub enum Value {
#[default]
@ -134,6 +135,7 @@ pub enum Value {
Function(Box<Function>),
Subquery(Box<Subquery>),
Expression(Box<Expression>),
// Add new variants here
}
impl Eq for Value {}
@ -1949,42 +1951,6 @@ impl Value {
}
}
impl Serialize for Value {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Value::None => s.serialize_unit_variant(TOKEN, 0, "None"),
Value::Null => s.serialize_unit_variant(TOKEN, 1, "Null"),
Value::Bool(v) => s.serialize_newtype_variant(TOKEN, 2, "Bool", v),
Value::Number(v) => s.serialize_newtype_variant(TOKEN, 3, "Number", v),
Value::Strand(v) => s.serialize_newtype_variant(TOKEN, 4, "Strand", v),
Value::Duration(v) => s.serialize_newtype_variant(TOKEN, 5, "Duration", v),
Value::Datetime(v) => s.serialize_newtype_variant(TOKEN, 6, "Datetime", v),
Value::Uuid(v) => s.serialize_newtype_variant(TOKEN, 7, "Uuid", v),
Value::Array(v) => s.serialize_newtype_variant(TOKEN, 8, "Array", v),
Value::Object(v) => s.serialize_newtype_variant(TOKEN, 9, "Object", v),
Value::Geometry(v) => s.serialize_newtype_variant(TOKEN, 10, "Geometry", v),
Value::Bytes(v) => s.serialize_newtype_variant(TOKEN, 11, "Bytes", v),
Value::Param(v) => s.serialize_newtype_variant(TOKEN, 12, "Param", v),
Value::Idiom(v) => s.serialize_newtype_variant(TOKEN, 13, "Idiom", v),
Value::Table(v) => s.serialize_newtype_variant(TOKEN, 14, "Table", v),
Value::Thing(v) => s.serialize_newtype_variant(TOKEN, 15, "Thing", v),
Value::Model(v) => s.serialize_newtype_variant(TOKEN, 16, "Model", v),
Value::Regex(v) => s.serialize_newtype_variant(TOKEN, 17, "Regex", v),
Value::Block(v) => s.serialize_newtype_variant(TOKEN, 18, "Block", v),
Value::Range(v) => s.serialize_newtype_variant(TOKEN, 19, "Range", v),
Value::Edges(v) => s.serialize_newtype_variant(TOKEN, 20, "Edges", v),
Value::Future(v) => s.serialize_newtype_variant(TOKEN, 21, "Future", v),
Value::Constant(v) => s.serialize_newtype_variant(TOKEN, 22, "Constant", v),
Value::Function(v) => s.serialize_newtype_variant(TOKEN, 23, "Function", v),
Value::Subquery(v) => s.serialize_newtype_variant(TOKEN, 24, "Subquery", v),
Value::Expression(v) => s.serialize_newtype_variant(TOKEN, 25, "Expression", v),
}
}
}
// ------------------------------
pub(crate) trait TryAdd<Rhs = Self> {