From f0e5753cd1f6fff0cc37d65112104bc8d42439a1 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 6 May 2023 21:49:34 +0100 Subject: [PATCH] Use static str where possible for context param names (#1942) --- lib/src/ctx/context.rs | 7 ++++--- lib/src/dbs/executor.rs | 4 ++-- lib/src/dbs/session.rs | 12 ++++-------- lib/src/doc/alter.rs | 2 +- lib/src/doc/event.rs | 8 ++++---- lib/src/doc/field.rs | 24 ++++++++++++------------ lib/src/doc/pluck.rs | 2 +- lib/src/sql/subquery.rs | 12 ++++++------ 8 files changed, 34 insertions(+), 37 deletions(-) diff --git a/lib/src/ctx/context.rs b/lib/src/ctx/context.rs index 0c56f798..2ed7ccff 100644 --- a/lib/src/ctx/context.rs +++ b/lib/src/ctx/context.rs @@ -28,7 +28,7 @@ pub struct Context<'a> { // Whether or not this context is cancelled. cancelled: Arc, // A collection of read only values stored in this context. - values: HashMap>, + values: HashMap, Cow<'a, Value>>, } impl<'a> Default for Context<'a> { @@ -93,11 +93,12 @@ impl<'a> Context<'a> { /// Add a value to the context. It overwrites any previously set values /// with the same key. - pub fn add_value(&mut self, key: String, value: V) + pub fn add_value(&mut self, key: K, value: V) where + K: Into>, V: Into>, { - self.values.insert(key, value.into()); + self.values.insert(key.into(), value.into()); } /// Get the timeout for this operation, if any. This is useful for diff --git a/lib/src/dbs/executor.rs b/lib/src/dbs/executor.rs index e911cfec..a01a2a35 100644 --- a/lib/src/dbs/executor.rs +++ b/lib/src/dbs/executor.rs @@ -110,14 +110,14 @@ impl<'a> Executor<'a> { async fn set_ns(&self, ctx: &mut Context<'_>, opt: &mut Options, ns: &str) { let mut session = ctx.value("session").unwrap_or(&Value::None).clone(); session.put(NS.as_ref(), ns.to_owned().into()); - ctx.add_value(String::from("session"), session); + ctx.add_value("session", session); opt.ns = Some(ns.into()); } async fn set_db(&self, ctx: &mut Context<'_>, opt: &mut Options, db: &str) { let mut session = ctx.value("session").unwrap_or(&Value::None).clone(); session.put(DB.as_ref(), db.to_owned().into()); - ctx.add_value(String::from("session"), session); + ctx.add_value("session", session); opt.db = Some(db.into()); } diff --git a/lib/src/dbs/session.rs b/lib/src/dbs/session.rs index 0a4ce053..34b12386 100644 --- a/lib/src/dbs/session.rs +++ b/lib/src/dbs/session.rs @@ -93,19 +93,15 @@ impl Session { /// Convert a session into a runtime pub(crate) fn context<'a>(&self, mut ctx: Context<'a>) -> Context<'a> { // Add auth data - let key = String::from("auth"); let val: Value = self.sd.to_owned().into(); - ctx.add_value(key, val); + ctx.add_value("auth", val); // Add scope data - let key = String::from("scope"); let val: Value = self.sc.to_owned().into(); - ctx.add_value(key, val); + ctx.add_value("scope", val); // Add token data - let key = String::from("token"); let val: Value = self.tk.to_owned().into(); - ctx.add_value(key, val); + ctx.add_value("token", val); // Add session value - let key = String::from("session"); let val: Value = Value::from(map! { "db".to_string() => self.db.to_owned().into(), "id".to_string() => self.id.to_owned().into(), @@ -116,7 +112,7 @@ impl Session { "sd".to_string() => self.sd.to_owned().into(), "tk".to_string() => self.tk.to_owned().into(), }); - ctx.add_value(key, val); + ctx.add_value("session", val); // Output context ctx } diff --git a/lib/src/doc/alter.rs b/lib/src/doc/alter.rs index 3d8714e6..78b1c99c 100644 --- a/lib/src/doc/alter.rs +++ b/lib/src/doc/alter.rs @@ -68,7 +68,7 @@ impl<'a> Document<'a> { let mut ctx = Context::new(ctx); // Add insertable value if let Workable::Insert(value) = &self.extras { - ctx.add_value("input".into(), value); + ctx.add_value("input", value); } // Process ON DUPLICATE KEY clause for x in x.iter() { diff --git a/lib/src/doc/event.rs b/lib/src/doc/event.rs index 34fa35fb..37f9997c 100644 --- a/lib/src/doc/event.rs +++ b/lib/src/doc/event.rs @@ -37,10 +37,10 @@ impl<'a> Document<'a> { }; // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("event".into(), met); - ctx.add_value("value".into(), self.current.deref()); - ctx.add_value("after".into(), self.current.deref()); - ctx.add_value("before".into(), self.initial.deref()); + ctx.add_value("event", met); + ctx.add_value("value", self.current.deref()); + ctx.add_value("after", self.current.deref()); + ctx.add_value("before", self.initial.deref()); // Process conditional clause let val = ev.when.compute(&ctx, opt, txn, Some(&self.current)).await?; // Execute event if value is truthy diff --git a/lib/src/doc/field.rs b/lib/src/doc/field.rs index 469397e8..43e838df 100644 --- a/lib/src/doc/field.rs +++ b/lib/src/doc/field.rs @@ -62,10 +62,10 @@ impl<'a> Document<'a> { if let Some(expr) = &fd.value { // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("input".into(), &inp); - ctx.add_value("value".into(), &val); - ctx.add_value("after".into(), &val); - ctx.add_value("before".into(), &old); + ctx.add_value("input", &inp); + ctx.add_value("value", &val); + ctx.add_value("after", &val); + ctx.add_value("before", &old); // Process the VALUE clause val = expr.compute(&ctx, opt, txn, Some(&self.current)).await?; } @@ -90,10 +90,10 @@ impl<'a> Document<'a> { if let Some(expr) = &fd.assert { // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("input".into(), &inp); - ctx.add_value("value".into(), &val); - ctx.add_value("after".into(), &val); - ctx.add_value("before".into(), &old); + ctx.add_value("input", &inp); + ctx.add_value("value", &val); + ctx.add_value("after", &val); + ctx.add_value("before", &old); // Process the ASSERT clause if !expr.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { return Err(Error::FieldValue { @@ -121,10 +121,10 @@ impl<'a> Document<'a> { let opt = &opt.perms(false); // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("input".into(), &inp); - ctx.add_value("value".into(), &val); - ctx.add_value("after".into(), &val); - ctx.add_value("before".into(), &old); + ctx.add_value("input", &inp); + ctx.add_value("value", &val); + ctx.add_value("after", &val); + ctx.add_value("before", &old); // Process the PERMISSION clause if !e.compute(&ctx, opt, txn, Some(&self.current)).await?.is_truthy() { val = old diff --git a/lib/src/doc/pluck.rs b/lib/src/doc/pluck.rs index 0316665b..af20850c 100644 --- a/lib/src/doc/pluck.rs +++ b/lib/src/doc/pluck.rs @@ -72,7 +72,7 @@ impl<'a> Document<'a> { let val = self.current.pick(k); // Configure the context let mut ctx = Context::new(ctx); - ctx.add_value("value".into(), &val); + ctx.add_value("value", &val); // Process the PERMISSION clause if !e .compute(&ctx, opt, txn, Some(&self.current)) diff --git a/lib/src/sql/subquery.rs b/lib/src/sql/subquery.rs index 69f27c0f..ca3d528d 100644 --- a/lib/src/sql/subquery.rs +++ b/lib/src/sql/subquery.rs @@ -81,7 +81,7 @@ impl Subquery { let mut ctx = Context::new(ctx); // Add parent document if let Some(doc) = doc { - ctx.add_value("parent".into(), doc); + ctx.add_value("parent", doc); } // Process subquery match v.compute(&ctx, opt, txn, doc).await? { @@ -103,7 +103,7 @@ impl Subquery { let mut ctx = Context::new(ctx); // Add parent document if let Some(doc) = doc { - ctx.add_value("parent".into(), doc); + ctx.add_value("parent", doc); } // Process subquery match v.compute(&ctx, opt, txn, doc).await? { @@ -125,7 +125,7 @@ impl Subquery { let mut ctx = Context::new(ctx); // Add parent document if let Some(doc) = doc { - ctx.add_value("parent".into(), doc); + ctx.add_value("parent", doc); } // Process subquery match v.compute(&ctx, opt, txn, doc).await? { @@ -147,7 +147,7 @@ impl Subquery { let mut ctx = Context::new(ctx); // Add parent document if let Some(doc) = doc { - ctx.add_value("parent".into(), doc); + ctx.add_value("parent", doc); } // Process subquery match v.compute(&ctx, opt, txn, doc).await? { @@ -169,7 +169,7 @@ impl Subquery { let mut ctx = Context::new(ctx); // Add parent document if let Some(doc) = doc { - ctx.add_value("parent".into(), doc); + ctx.add_value("parent", doc); } // Process subquery match v.compute(&ctx, opt, txn, doc).await? { @@ -191,7 +191,7 @@ impl Subquery { let mut ctx = Context::new(ctx); // Add parent document if let Some(doc) = doc { - ctx.add_value("parent".into(), doc); + ctx.add_value("parent", doc); } // Process subquery match v.compute(&ctx, opt, txn, doc).await? {