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::Arc;
use trice::Instant;

View file

@ -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<Instant> {
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<Duration> {
@ -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(),
)
}
}

View file

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

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

View file

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

View file

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

View file

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

View file

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

View file

@ -4,7 +4,7 @@ use crate::sql::value::Value;
use std::cmp::min;
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![];
match (self, val) {
(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;
impl Value {
pub fn each(&self, path: &[Part]) -> Vec<Idiom> {
pub(crate) fn each(&self, path: &[Part]) -> Vec<Idiom> {
self._each(path, Idiom::default())
}
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;
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 {
Some(path) => self.pick(path)._every(steps, arrays, Idiom::from(path)),
None => self._every(steps, arrays, Idiom::default()),

View file

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

View file

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

View file

@ -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<Thing, Error> {
pub(crate) fn generate(self, tb: &Table, retable: bool) -> Result<Thing, Error> {
match self {
// There is a floating point number for the id field
Value::Number(id) if id.is_float() => Ok(Thing {

View file

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

View file

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

View file

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

View file

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

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;
impl Value {
pub async fn patch(
pub(crate) async fn patch(
&mut self,
ctx: &Context<'_>,
opt: &Options,

View file

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

View file

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

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)
}
/// Convert this Value to an Option
pub fn output(self) -> Option<Value> {
match self {
Value::None => None,
_ => Some(self),
}
}
// -----------------------------------
// Simple value detection
// -----------------------------------