Implement SQL Ident as a newtype tuple struct

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-05 01:27:57 +01:00
parent 3233660037
commit ed92fb4d85
9 changed files with 25 additions and 25 deletions

View file

@ -142,7 +142,7 @@ impl<'a> Executor<'a> {
// Allowed to run?
opt.check(Level::Db)?;
// Process the option
match &stm.name.name.to_uppercase()[..] {
match &stm.name.to_uppercase()[..] {
"FIELDS" => opt = opt.fields(stm.what),
"EVENTS" => opt = opt.events(stm.what),
"TABLES" => opt = opt.tables(stm.what),

View file

@ -8,40 +8,40 @@ use nom::character::complete::char;
use nom::sequence::delimited;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::ops::Deref;
use std::str;
#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Ident {
pub name: String,
}
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Ident(pub String);
impl From<String> for Ident {
fn from(s: String) -> Self {
Ident {
name: s,
}
Ident(s)
}
}
impl<'a> From<&'a str> for Ident {
fn from(i: &str) -> Ident {
Ident {
name: String::from(i),
}
Ident(String::from(i))
}
}
impl<'a> From<&'a String> for Ident {
fn from(i: &String) -> Ident {
Ident {
name: String::from(i),
}
Ident(String::from(i))
}
}
impl Deref for Ident {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl fmt::Display for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", escape(&self.name, &val_char, "`"))
write!(f, "{}", escape(&self.0, &val_char, "`"))
}
}

View file

@ -41,7 +41,7 @@ impl Param {
// Find a base variable by name
match self.parts.first() {
// The first part will be a field
Some(Part::Field(v)) => match &v.name[..] {
Some(Part::Field(v)) => match v.as_str() {
"this" => match doc {
// The base document exists
Some(v) => {
@ -55,7 +55,7 @@ impl Param {
// The base document does not exist
None => Ok(Value::None),
},
_ => match ctx.value::<Value>(&v.name) {
_ => match ctx.value::<Value>(v) {
// The base variable exists
Some(v) => {
// Get the path parts

View file

@ -16,7 +16,7 @@ impl Value {
Some(p) => match (self, other) {
// Current path part is an object
(Value::Object(a), Value::Object(b)) => match p {
Part::Field(f) => match (a.get(&f.name), b.get(&f.name)) {
Part::Field(f) => match (a.get(f as &str), b.get(f as &str)) {
(Some(a), Some(b)) => a.compare(b, path.next(), collate, numeric),
(Some(_), None) => Some(Ordering::Greater),
(None, Some(_)) => Some(Ordering::Less),

View file

@ -27,10 +27,10 @@ impl Value {
Value::Object(v) => match p {
Part::Field(f) => match path.len() {
1 => {
v.remove(&f.name);
v.remove(f as &str);
Ok(())
}
_ => match v.get_mut(&f.name) {
_ => match v.get_mut(f as &str) {
Some(v) if v.is_some() => v.del(ctx, opt, txn, path.next()).await,
_ => Ok(()),
},

View file

@ -13,7 +13,7 @@ impl Value {
Some(p) => match self {
// Current path part is an object
Value::Object(v) => match p {
Part::Field(f) => match v.get(&f.name) {
Part::Field(f) => match v.get(f as &str) {
Some(v) => v._each(path.next(), prev.push(p.clone())),
None => vec![],
},

View file

@ -26,7 +26,7 @@ impl Value {
Some(p) => match self {
// Current path part is an object
Value::Object(v) => match p {
Part::Field(f) => match v.get(&f.name) {
Part::Field(f) => match v.get(f as &str) {
Some(v) => v.get(ctx, opt, txn, path.next()).await,
None => Ok(Value::None),
},

View file

@ -9,7 +9,7 @@ impl Value {
Some(p) => match self {
// Current path part is an object
Value::Object(v) => match p {
Part::Field(f) => match v.get(&f.name) {
Part::Field(f) => match v.get(f as &str) {
Some(v) => v.pick(path.next()),
None => Value::None,
},

View file

@ -24,12 +24,12 @@ impl Value {
Some(p) => match self {
// Current path part is an object
Value::Object(v) => match p {
Part::Field(f) => match v.get_mut(&f.name) {
Part::Field(f) => match v.get_mut(f as &str) {
Some(v) if v.is_some() => v.set(ctx, opt, txn, path.next(), val).await,
_ => {
let mut obj = Value::base();
obj.set(ctx, opt, txn, path.next(), val).await?;
v.insert(f.name.to_owned(), obj);
v.insert(f.to_string(), obj);
Ok(())
}
},