Simplify external API and cleanup unused functions (#1857)

This commit is contained in:
Tobie Morgan Hitchcock 2023-04-24 21:51:35 +01:00 committed by GitHub
parent 1fc802717d
commit 3c027e9e2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 45 additions and 198 deletions

View file

@ -1,3 +1,5 @@
#![cfg(feature = "scripting")]
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use trice::Instant; use trice::Instant;

View file

@ -1,10 +1,9 @@
use crate::ctx::cancellation::Cancellation;
use crate::ctx::canceller::Canceller; use crate::ctx::canceller::Canceller;
use crate::ctx::reason::Reason; use crate::ctx::reason::Reason;
use crate::sql::value::Value; use crate::sql::value::Value;
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt::{self, Debug};
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
@ -21,7 +20,6 @@ impl<'a> From<&'a Value> for Cow<'a, Value> {
Cow::Borrowed(v) Cow::Borrowed(v)
} }
} }
pub struct Context<'a> { pub struct Context<'a> {
// An optional parent context. // An optional parent context.
parent: Option<&'a Context<'a>>, 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> { impl<'a> Context<'a> {
/// Create an empty background context. /// Create an empty background context.
pub fn background() -> Self { pub fn background() -> Self {
@ -67,16 +76,6 @@ impl<'a> Context<'a> {
Canceller::new(cancelled) 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 /// Add a deadline to the context. If the current deadline is sooner than
/// the provided deadline, this method does nothing. /// the provided deadline, this method does nothing.
pub fn add_deadline(&mut self, deadline: Instant) { pub fn add_deadline(&mut self, deadline: Instant) {
@ -101,12 +100,6 @@ impl<'a> Context<'a> {
self.values.insert(key, value.into()); 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<Instant> {
self.deadline
}
/// Get the timeout for this operation, if any. This is useful for /// Get the timeout for this operation, if any. This is useful for
/// checking if a long job should be started or not. /// checking if a long job should be started or not.
pub fn timeout(&self) -> Option<Duration> { pub fn timeout(&self) -> Option<Duration> {
@ -131,11 +124,6 @@ impl<'a> Context<'a> {
self.done().is_none() 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. /// Check if the context is not ok to continue.
pub fn is_done(&self) -> bool { pub fn is_done(&self) -> bool {
self.done().is_some() self.done().is_some()
@ -146,20 +134,6 @@ impl<'a> Context<'a> {
matches!(self.done(), Some(Reason::Timedout)) 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 /// Get a value from the context. If no value is stored under the
/// provided key, then this will return None. /// provided key, then this will return None.
pub fn value(&self, key: &str) -> Option<&Value> { pub fn value(&self, key: &str) -> Option<&Value> {
@ -174,15 +148,15 @@ impl<'a> Context<'a> {
}, },
} }
} }
}
impl<'a> fmt::Debug for Context<'a> { /// Get a 'static view into the cancellation status.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { #[cfg(feature = "scripting")]
f.debug_struct("Context") pub fn cancellation(&self) -> crate::ctx::cancellation::Cancellation {
.field("parent", &self.parent) crate::ctx::cancellation::Cancellation::new(
.field("deadline", &self.deadline) self.deadline,
.field("cancelled", &self.cancelled) std::iter::successors(Some(self), |ctx| ctx.parent)
.field("values", &self.values) .map(|ctx| ctx.cancelled.clone())
.finish() .collect(),
)
} }
} }

View file

@ -8,11 +8,11 @@ use crate::err::Error;
impl<'a> Document<'a> { impl<'a> Document<'a> {
pub async fn erase( pub async fn erase(
&mut self, &mut self,
ctx: &Context<'_>, _ctx: &Context<'_>,
opt: &Options, _opt: &Options,
txn: &Transaction, _txn: &Transaction,
_stm: &Statement<'_>, _stm: &Statement<'_>,
) -> Result<(), Error> { ) -> Result<(), Error> {
self.current.to_mut().clear(ctx, opt, txn).await self.current.to_mut().clear().await
} }
} }

View file

@ -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);
}
}

View file

@ -1,16 +1,8 @@
use crate::ctx::Context;
use crate::dbs::Options;
use crate::dbs::Transaction;
use crate::err::Error; use crate::err::Error;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn clear( pub async fn clear(&mut self) -> Result<(), Error> {
&mut self,
_ctx: &Context<'_>,
_opt: &Options,
_txn: &Transaction,
) -> Result<(), Error> {
*self = Value::None; *self = Value::None;
Ok(()) Ok(())
} }
@ -20,15 +12,13 @@ impl Value {
mod tests { mod tests {
use super::*; use super::*;
use crate::dbs::test::mock;
use crate::sql::test::Parse; use crate::sql::test::Parse;
#[tokio::test] #[tokio::test]
async fn clear_value() { async fn clear_value() {
let (ctx, opt, txn) = mock().await;
let mut val = Value::parse("{ test: { other: null, something: 123 } }"); let mut val = Value::parse("{ test: { other: null, something: 123 } }");
let res = Value::None; let res = Value::None;
val.clear(&ctx, &opt, &txn).await.unwrap(); val.clear().await.unwrap();
assert_eq!(res, val); assert_eq!(res, val);
} }
} }

View file

@ -4,7 +4,7 @@ use crate::sql::value::Value;
use std::cmp::Ordering; use std::cmp::Ordering;
impl Value { impl Value {
pub fn compare( pub(crate) fn compare(
&self, &self,
other: &Self, other: &Self,
path: &[Part], path: &[Part],

View file

@ -7,7 +7,7 @@ use crate::sql::part::Part;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn decrement( pub(crate) async fn decrement(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -7,7 +7,7 @@ use crate::sql::thing::Thing;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn def( pub(crate) async fn def(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -13,7 +13,7 @@ use std::collections::HashSet;
impl Value { impl Value {
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)] #[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
pub async fn del( pub(crate) async fn del(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -4,7 +4,7 @@ use crate::sql::value::Value;
use std::cmp::min; use std::cmp::min;
impl Value { impl Value {
pub fn diff(&self, val: &Value, path: Idiom) -> Vec<Operation> { pub(crate) fn diff(&self, val: &Value, path: Idiom) -> Vec<Operation> {
let mut ops: Vec<Operation> = vec![]; let mut ops: Vec<Operation> = vec![];
match (self, val) { match (self, val) {
(Value::Object(a), Value::Object(b)) if a != b => { (Value::Object(a), Value::Object(b)) if a != b => {

View file

@ -4,7 +4,7 @@ use crate::sql::part::Part;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub fn each(&self, path: &[Part]) -> Vec<Idiom> { pub(crate) fn each(&self, path: &[Part]) -> Vec<Idiom> {
self._each(path, Idiom::default()) self._each(path, Idiom::default())
} }
fn _each(&self, path: &[Part], prev: Idiom) -> Vec<Idiom> { fn _each(&self, path: &[Part], prev: Idiom) -> Vec<Idiom> {

View file

@ -3,7 +3,7 @@ use crate::sql::part::Part;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub fn every(&self, path: Option<&[Part]>, steps: bool, arrays: bool) -> Vec<Idiom> { pub(crate) fn every(&self, path: Option<&[Part]>, steps: bool, arrays: bool) -> Vec<Idiom> {
match path { match path {
Some(path) => self.pick(path)._every(steps, arrays, Idiom::from(path)), Some(path) => self.pick(path)._every(steps, arrays, Idiom::from(path)),
None => self._every(steps, arrays, Idiom::default()), None => self._every(steps, arrays, Idiom::default()),

View file

@ -7,7 +7,7 @@ use crate::sql::part::Part;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn extend( pub(crate) async fn extend(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -14,7 +14,7 @@ use futures::future::try_join_all;
impl Value { impl Value {
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)] #[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
pub async fn fetch( pub(crate) async fn fetch(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -5,7 +5,7 @@ use crate::sql::thing::Thing;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub fn generate(self, tb: &Table, retable: bool) -> Result<Thing, Error> { pub(crate) fn generate(self, tb: &Table, retable: bool) -> Result<Thing, Error> {
match self { match self {
// There is a floating point number for the id field // There is a floating point number for the id field
Value::Number(id) if id.is_float() => Ok(Thing { Value::Number(id) if id.is_float() => Ok(Thing {

View file

@ -17,7 +17,7 @@ use async_recursion::async_recursion;
impl Value { impl Value {
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)] #[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
pub async fn get( pub(crate) async fn get(
&self, &self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -7,7 +7,7 @@ use crate::sql::part::Part;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn increment( pub(crate) async fn increment(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -5,7 +5,7 @@ use crate::err::Error;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn merge( pub(crate) async fn merge(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -6,7 +6,6 @@ pub(super) mod serde;
mod value; mod value;
mod all; mod all;
mod array;
mod clear; mod clear;
mod compare; mod compare;
mod decrement; mod decrement;
@ -24,12 +23,10 @@ mod get;
mod increment; mod increment;
mod last; mod last;
mod merge; mod merge;
mod object;
mod patch; mod patch;
mod pick; mod pick;
mod put; mod put;
mod replace; mod replace;
mod rid; mod rid;
mod set; mod set;
mod single;
mod walk; mod walk;

View file

@ -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);
}
}

View file

@ -6,7 +6,7 @@ use crate::sql::operation::Op;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn patch( pub(crate) async fn patch(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -5,7 +5,7 @@ use crate::err::Error;
use crate::sql::value::Value; use crate::sql::value::Value;
impl Value { impl Value {
pub async fn replace( pub(crate) async fn replace(
&mut self, &mut self,
_ctx: &Context<'_>, _ctx: &Context<'_>,
_opt: &Options, _opt: &Options,

View file

@ -11,7 +11,7 @@ use async_recursion::async_recursion;
impl Value { impl Value {
#[cfg_attr(not(target_arch = "wasm32"), async_recursion)] #[cfg_attr(not(target_arch = "wasm32"), async_recursion)]
#[cfg_attr(target_arch = "wasm32", async_recursion(?Send))] #[cfg_attr(target_arch = "wasm32", async_recursion(?Send))]
pub async fn set( pub(crate) async fn set(
&mut self, &mut self,
ctx: &Context<'_>, ctx: &Context<'_>,
opt: &Options, opt: &Options,

View file

@ -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,
}
}
}

View file

@ -615,14 +615,6 @@ impl Value {
Ok(self) Ok(self)
} }
/// Convert this Value to an Option
pub fn output(self) -> Option<Value> {
match self {
Value::None => None,
_ => Some(self),
}
}
// ----------------------------------- // -----------------------------------
// Simple value detection // Simple value detection
// ----------------------------------- // -----------------------------------