diff --git a/lib/src/ctx/cancellation.rs b/lib/src/ctx/cancellation.rs index 9b54387a..1ccd9b26 100644 --- a/lib/src/ctx/cancellation.rs +++ b/lib/src/ctx/cancellation.rs @@ -1,3 +1,5 @@ +#![cfg(feature = "scripting")] + use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use trice::Instant; diff --git a/lib/src/ctx/context.rs b/lib/src/ctx/context.rs index da518544..0c56f798 100644 --- a/lib/src/ctx/context.rs +++ b/lib/src/ctx/context.rs @@ -1,10 +1,9 @@ -use crate::ctx::cancellation::Cancellation; use crate::ctx::canceller::Canceller; use crate::ctx::reason::Reason; use crate::sql::value::Value; use std::borrow::Cow; use std::collections::HashMap; -use std::fmt; +use std::fmt::{self, Debug}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::Duration; @@ -21,7 +20,6 @@ impl<'a> From<&'a Value> for Cow<'a, Value> { Cow::Borrowed(v) } } - pub struct Context<'a> { // An optional parent context. parent: Option<&'a Context<'a>>, @@ -39,6 +37,17 @@ impl<'a> Default for Context<'a> { } } +impl<'a> Debug for Context<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_struct("Context") + .field("parent", &self.parent) + .field("deadline", &self.deadline) + .field("cancelled", &self.cancelled) + .field("values", &self.values) + .finish() + } +} + impl<'a> Context<'a> { /// Create an empty background context. pub fn background() -> Self { @@ -67,16 +76,6 @@ impl<'a> Context<'a> { Canceller::new(cancelled) } - /// Get a 'static view into the cancellation status. - pub fn cancellation(&self) -> Cancellation { - Cancellation::new( - self.deadline, - std::iter::successors(Some(self), |ctx| ctx.parent) - .map(|ctx| ctx.cancelled.clone()) - .collect(), - ) - } - /// Add a deadline to the context. If the current deadline is sooner than /// the provided deadline, this method does nothing. pub fn add_deadline(&mut self, deadline: Instant) { @@ -101,12 +100,6 @@ impl<'a> Context<'a> { self.values.insert(key, value.into()); } - /// Get the deadline for this operation, if any. This is useful for - /// checking if a long job should be started or not. - pub fn deadline(&self) -> Option { - self.deadline - } - /// Get the timeout for this operation, if any. This is useful for /// checking if a long job should be started or not. pub fn timeout(&self) -> Option { @@ -131,11 +124,6 @@ impl<'a> Context<'a> { self.done().is_none() } - /// Check if the context is not ok to continue. - pub fn is_err(&self) -> bool { - self.done().is_some() - } - /// Check if the context is not ok to continue. pub fn is_done(&self) -> bool { self.done().is_some() @@ -146,20 +134,6 @@ impl<'a> Context<'a> { matches!(self.done(), Some(Reason::Timedout)) } - /// Check if the context is not ok to continue, because it was cancelled. - pub fn is_cancelled(&self) -> bool { - matches!(self.done(), Some(Reason::Canceled)) - } - - /// Check if the status of the context. This will return a Result, with an Ok - /// if the operation may proceed, and an Error if it should be stopped. - pub fn check(&self) -> Result<(), Reason> { - match self.done() { - Some(reason) => Err(reason), - None => Ok(()), - } - } - /// Get a value from the context. If no value is stored under the /// provided key, then this will return None. pub fn value(&self, key: &str) -> Option<&Value> { @@ -174,15 +148,15 @@ impl<'a> Context<'a> { }, } } -} -impl<'a> fmt::Debug for Context<'a> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("Context") - .field("parent", &self.parent) - .field("deadline", &self.deadline) - .field("cancelled", &self.cancelled) - .field("values", &self.values) - .finish() + /// Get a 'static view into the cancellation status. + #[cfg(feature = "scripting")] + pub fn cancellation(&self) -> crate::ctx::cancellation::Cancellation { + crate::ctx::cancellation::Cancellation::new( + self.deadline, + std::iter::successors(Some(self), |ctx| ctx.parent) + .map(|ctx| ctx.cancelled.clone()) + .collect(), + ) } } diff --git a/lib/src/doc/erase.rs b/lib/src/doc/erase.rs index 54419c0e..6fd1bfdc 100644 --- a/lib/src/doc/erase.rs +++ b/lib/src/doc/erase.rs @@ -8,11 +8,11 @@ use crate::err::Error; impl<'a> Document<'a> { pub async fn erase( &mut self, - ctx: &Context<'_>, - opt: &Options, - txn: &Transaction, + _ctx: &Context<'_>, + _opt: &Options, + _txn: &Transaction, _stm: &Statement<'_>, ) -> Result<(), Error> { - self.current.to_mut().clear(ctx, opt, txn).await + self.current.to_mut().clear().await } } diff --git a/lib/src/sql/value/array.rs b/lib/src/sql/value/array.rs deleted file mode 100644 index b6e35fcb..00000000 --- a/lib/src/sql/value/array.rs +++ /dev/null @@ -1,49 +0,0 @@ -use crate::ctx::Context; -use crate::dbs::Options; -use crate::dbs::Transaction; -use crate::err::Error; -use crate::sql::array::Array; -use crate::sql::part::Part; -use crate::sql::value::Value; - -impl Value { - pub async fn array( - &mut self, - ctx: &Context<'_>, - opt: &Options, - txn: &Transaction, - path: &[Part], - ) -> Result<(), Error> { - let val = Value::from(Array::default()); - self.set(ctx, opt, txn, path, val).await - } -} - -#[cfg(test)] -mod tests { - - use super::*; - use crate::dbs::test::mock; - use crate::sql::idiom::Idiom; - use crate::sql::test::Parse; - - #[tokio::test] - async fn array_none() { - let (ctx, opt, txn) = mock().await; - let idi = Idiom::default(); - let mut val = Value::parse("{ test: { other: null, something: 123 } }"); - let res = Value::parse("[]"); - val.array(&ctx, &opt, &txn, &idi).await.unwrap(); - assert_eq!(res, val); - } - - #[tokio::test] - async fn array_path() { - let (ctx, opt, txn) = mock().await; - let idi = Idiom::parse("test"); - let mut val = Value::parse("{ test: { other: null, something: 123 } }"); - let res = Value::parse("{ test: [] }"); - val.array(&ctx, &opt, &txn, &idi).await.unwrap(); - assert_eq!(res, val); - } -} diff --git a/lib/src/sql/value/clear.rs b/lib/src/sql/value/clear.rs index 788d51fd..844816cf 100644 --- a/lib/src/sql/value/clear.rs +++ b/lib/src/sql/value/clear.rs @@ -1,16 +1,8 @@ -use crate::ctx::Context; -use crate::dbs::Options; -use crate::dbs::Transaction; use crate::err::Error; use crate::sql::value::Value; impl Value { - pub async fn clear( - &mut self, - _ctx: &Context<'_>, - _opt: &Options, - _txn: &Transaction, - ) -> Result<(), Error> { + pub async fn clear(&mut self) -> Result<(), Error> { *self = Value::None; Ok(()) } @@ -20,15 +12,13 @@ impl Value { mod tests { use super::*; - use crate::dbs::test::mock; use crate::sql::test::Parse; #[tokio::test] async fn clear_value() { - let (ctx, opt, txn) = mock().await; let mut val = Value::parse("{ test: { other: null, something: 123 } }"); let res = Value::None; - val.clear(&ctx, &opt, &txn).await.unwrap(); + val.clear().await.unwrap(); assert_eq!(res, val); } } diff --git a/lib/src/sql/value/compare.rs b/lib/src/sql/value/compare.rs index 0b2f1b24..e85dd5bc 100644 --- a/lib/src/sql/value/compare.rs +++ b/lib/src/sql/value/compare.rs @@ -4,7 +4,7 @@ use crate::sql::value::Value; use std::cmp::Ordering; impl Value { - pub fn compare( + pub(crate) fn compare( &self, other: &Self, path: &[Part], diff --git a/lib/src/sql/value/decrement.rs b/lib/src/sql/value/decrement.rs index f8b71030..f093a0bf 100644 --- a/lib/src/sql/value/decrement.rs +++ b/lib/src/sql/value/decrement.rs @@ -7,7 +7,7 @@ use crate::sql::part::Part; use crate::sql::value::Value; impl Value { - pub async fn decrement( + pub(crate) async fn decrement( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/def.rs b/lib/src/sql/value/def.rs index bff87d44..db9a4d48 100644 --- a/lib/src/sql/value/def.rs +++ b/lib/src/sql/value/def.rs @@ -7,7 +7,7 @@ use crate::sql::thing::Thing; use crate::sql::value::Value; impl Value { - pub async fn def( + pub(crate) async fn def( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/del.rs b/lib/src/sql/value/del.rs index 3743d4d9..21b52a2d 100644 --- a/lib/src/sql/value/del.rs +++ b/lib/src/sql/value/del.rs @@ -13,7 +13,7 @@ use std::collections::HashSet; impl Value { #[cfg_attr(not(target_arch = "wasm32"), async_recursion)] #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] - pub async fn del( + pub(crate) async fn del( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/diff.rs b/lib/src/sql/value/diff.rs index 3481122a..25dbbf84 100644 --- a/lib/src/sql/value/diff.rs +++ b/lib/src/sql/value/diff.rs @@ -4,7 +4,7 @@ use crate::sql::value::Value; use std::cmp::min; impl Value { - pub fn diff(&self, val: &Value, path: Idiom) -> Vec { + pub(crate) fn diff(&self, val: &Value, path: Idiom) -> Vec { let mut ops: Vec = vec![]; match (self, val) { (Value::Object(a), Value::Object(b)) if a != b => { diff --git a/lib/src/sql/value/each.rs b/lib/src/sql/value/each.rs index 880cc56a..a8f05b77 100644 --- a/lib/src/sql/value/each.rs +++ b/lib/src/sql/value/each.rs @@ -4,7 +4,7 @@ use crate::sql::part::Part; use crate::sql::value::Value; impl Value { - pub fn each(&self, path: &[Part]) -> Vec { + pub(crate) fn each(&self, path: &[Part]) -> Vec { self._each(path, Idiom::default()) } fn _each(&self, path: &[Part], prev: Idiom) -> Vec { diff --git a/lib/src/sql/value/every.rs b/lib/src/sql/value/every.rs index cbf40236..9e046857 100644 --- a/lib/src/sql/value/every.rs +++ b/lib/src/sql/value/every.rs @@ -3,7 +3,7 @@ use crate::sql::part::Part; use crate::sql::value::Value; impl Value { - pub fn every(&self, path: Option<&[Part]>, steps: bool, arrays: bool) -> Vec { + pub(crate) fn every(&self, path: Option<&[Part]>, steps: bool, arrays: bool) -> Vec { match path { Some(path) => self.pick(path)._every(steps, arrays, Idiom::from(path)), None => self._every(steps, arrays, Idiom::default()), diff --git a/lib/src/sql/value/extend.rs b/lib/src/sql/value/extend.rs index a6f03b80..0e7dbbc7 100644 --- a/lib/src/sql/value/extend.rs +++ b/lib/src/sql/value/extend.rs @@ -7,7 +7,7 @@ use crate::sql::part::Part; use crate::sql::value::Value; impl Value { - pub async fn extend( + pub(crate) async fn extend( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/fetch.rs b/lib/src/sql/value/fetch.rs index 00e52ff9..6514cb6e 100644 --- a/lib/src/sql/value/fetch.rs +++ b/lib/src/sql/value/fetch.rs @@ -14,7 +14,7 @@ use futures::future::try_join_all; impl Value { #[cfg_attr(not(target_arch = "wasm32"), async_recursion)] #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] - pub async fn fetch( + pub(crate) async fn fetch( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/generate.rs b/lib/src/sql/value/generate.rs index 1dabb50c..abb4cc10 100644 --- a/lib/src/sql/value/generate.rs +++ b/lib/src/sql/value/generate.rs @@ -5,7 +5,7 @@ use crate::sql::thing::Thing; use crate::sql::value::Value; impl Value { - pub fn generate(self, tb: &Table, retable: bool) -> Result { + pub(crate) fn generate(self, tb: &Table, retable: bool) -> Result { match self { // There is a floating point number for the id field Value::Number(id) if id.is_float() => Ok(Thing { diff --git a/lib/src/sql/value/get.rs b/lib/src/sql/value/get.rs index 7a3f145d..2083510e 100644 --- a/lib/src/sql/value/get.rs +++ b/lib/src/sql/value/get.rs @@ -17,7 +17,7 @@ use async_recursion::async_recursion; impl Value { #[cfg_attr(not(target_arch = "wasm32"), async_recursion)] #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] - pub async fn get( + pub(crate) async fn get( &self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/increment.rs b/lib/src/sql/value/increment.rs index 5481a077..0e8fc1bf 100644 --- a/lib/src/sql/value/increment.rs +++ b/lib/src/sql/value/increment.rs @@ -7,7 +7,7 @@ use crate::sql::part::Part; use crate::sql::value::Value; impl Value { - pub async fn increment( + pub(crate) async fn increment( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/merge.rs b/lib/src/sql/value/merge.rs index 27299348..c92e61f7 100644 --- a/lib/src/sql/value/merge.rs +++ b/lib/src/sql/value/merge.rs @@ -5,7 +5,7 @@ use crate::err::Error; use crate::sql::value::Value; impl Value { - pub async fn merge( + pub(crate) async fn merge( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/mod.rs b/lib/src/sql/value/mod.rs index a5d90ca3..e2a8f340 100644 --- a/lib/src/sql/value/mod.rs +++ b/lib/src/sql/value/mod.rs @@ -6,7 +6,6 @@ pub(super) mod serde; mod value; mod all; -mod array; mod clear; mod compare; mod decrement; @@ -24,12 +23,10 @@ mod get; mod increment; mod last; mod merge; -mod object; mod patch; mod pick; mod put; mod replace; mod rid; mod set; -mod single; mod walk; diff --git a/lib/src/sql/value/object.rs b/lib/src/sql/value/object.rs deleted file mode 100644 index 97d8cfd0..00000000 --- a/lib/src/sql/value/object.rs +++ /dev/null @@ -1,49 +0,0 @@ -use crate::ctx::Context; -use crate::dbs::Options; -use crate::dbs::Transaction; -use crate::err::Error; -use crate::sql::object::Object; -use crate::sql::part::Part; -use crate::sql::value::Value; - -impl Value { - pub async fn object( - &mut self, - ctx: &Context<'_>, - opt: &Options, - txn: &Transaction, - path: &[Part], - ) -> Result<(), Error> { - let val = Value::from(Object::default()); - self.set(ctx, opt, txn, path, val).await - } -} - -#[cfg(test)] -mod tests { - - use super::*; - use crate::dbs::test::mock; - use crate::sql::idiom::Idiom; - use crate::sql::test::Parse; - - #[tokio::test] - async fn object_none() { - let (ctx, opt, txn) = mock().await; - let idi = Idiom::default(); - let mut val = Value::parse("{ test: { other: null, something: 123 } }"); - let res = Value::parse("{}"); - val.object(&ctx, &opt, &txn, &idi).await.unwrap(); - assert_eq!(res, val); - } - - #[tokio::test] - async fn object_path() { - let (ctx, opt, txn) = mock().await; - let idi = Idiom::parse("test"); - let mut val = Value::parse("{ test: { other: null, something: 123 } }"); - let res = Value::parse("{ test: {} }"); - val.object(&ctx, &opt, &txn, &idi).await.unwrap(); - assert_eq!(res, val); - } -} diff --git a/lib/src/sql/value/patch.rs b/lib/src/sql/value/patch.rs index 0b4bd37c..b3b0dc5a 100644 --- a/lib/src/sql/value/patch.rs +++ b/lib/src/sql/value/patch.rs @@ -6,7 +6,7 @@ use crate::sql::operation::Op; use crate::sql::value::Value; impl Value { - pub async fn patch( + pub(crate) async fn patch( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/replace.rs b/lib/src/sql/value/replace.rs index 8111025b..a92d46c2 100644 --- a/lib/src/sql/value/replace.rs +++ b/lib/src/sql/value/replace.rs @@ -5,7 +5,7 @@ use crate::err::Error; use crate::sql::value::Value; impl Value { - pub async fn replace( + pub(crate) async fn replace( &mut self, _ctx: &Context<'_>, _opt: &Options, diff --git a/lib/src/sql/value/set.rs b/lib/src/sql/value/set.rs index 8d9fd49c..1d2299c1 100644 --- a/lib/src/sql/value/set.rs +++ b/lib/src/sql/value/set.rs @@ -11,7 +11,7 @@ use async_recursion::async_recursion; impl Value { #[cfg_attr(not(target_arch = "wasm32"), async_recursion)] #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] - pub async fn set( + pub(crate) async fn set( &mut self, ctx: &Context<'_>, opt: &Options, diff --git a/lib/src/sql/value/single.rs b/lib/src/sql/value/single.rs deleted file mode 100644 index 637726cc..00000000 --- a/lib/src/sql/value/single.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::sql::value::Value; - -impl Value { - pub fn single(&self) -> &Self { - match self { - Value::Array(v) => v.first().unwrap_or(&Value::None), - v => v, - } - } -} diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index 72cc588f..76c6822b 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -615,14 +615,6 @@ impl Value { Ok(self) } - /// Convert this Value to an Option - pub fn output(self) -> Option { - match self { - Value::None => None, - _ => Some(self), - } - } - // ----------------------------------- // Simple value detection // -----------------------------------