Implement SQL Param as a newtype tuple struct

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-04 22:38:19 +01:00
parent bd6f01971f
commit 1f05b32f93

View file

@ -11,18 +11,22 @@ use crate::sql::value::Value;
use nom::character::complete::char; use nom::character::complete::char;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fmt; use std::fmt;
use std::ops::Deref;
use std::str; use std::str;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)] #[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Param { pub struct Param(pub Idiom);
pub name: Idiom,
}
impl From<Idiom> for Param { impl From<Idiom> for Param {
fn from(p: Idiom) -> Param { fn from(p: Idiom) -> Param {
Param { Param(p)
name: p, }
} }
impl Deref for Param {
type Target = Idiom;
fn deref(&self) -> &Self::Target {
&self.0
} }
} }
@ -35,14 +39,14 @@ impl Param {
doc: Option<&Value>, doc: Option<&Value>,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
// Find a base variable by name // Find a base variable by name
match self.name.parts.first() { match self.parts.first() {
// The first part will be a field // The first part will be a field
Some(Part::Field(v)) => match &v.name[..] { Some(Part::Field(v)) => match &v.name[..] {
"this" => match doc { "this" => match doc {
// The base document exists // The base document exists
Some(v) => { Some(v) => {
// Get the path parts // Get the path parts
let pth: &[Part] = &self.name; let pth: &[Part] = self;
// Process the paramater value // Process the paramater value
let res = v.compute(ctx, opt, txn, doc).await?; let res = v.compute(ctx, opt, txn, doc).await?;
// Return the desired field // Return the desired field
@ -55,7 +59,7 @@ impl Param {
// The base variable exists // The base variable exists
Some(v) => { Some(v) => {
// Get the path parts // Get the path parts
let pth: &[Part] = &self.name; let pth: &[Part] = self;
// Process the paramater value // Process the paramater value
let res = v.compute(ctx, opt, txn, doc).await?; let res = v.compute(ctx, opt, txn, doc).await?;
// Return the desired field // Return the desired field
@ -72,7 +76,7 @@ impl Param {
impl fmt::Display for Param { impl fmt::Display for Param {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "${}", &self.name) write!(f, "${}", &self.0)
} }
} }