diff --git a/lib/src/cnf/mod.rs b/lib/src/cnf/mod.rs index 19dd18ab..7176ce75 100644 --- a/lib/src/cnf/mod.rs +++ b/lib/src/cnf/mod.rs @@ -5,9 +5,6 @@ pub const MAX_CONCURRENT_TASKS: usize = 64; // Specifies how many subqueries will be processed recursively before the query fails. pub const MAX_RECURSIVE_QUERIES: usize = 16; -// The current package version release of the SurrealDB crate -pub const VERSION: &str = env!("CARGO_PKG_VERSION"); - // The characters which are supported in server record IDs. pub const ID_CHARS: [char; 36] = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', diff --git a/lib/src/fnc/http.rs b/lib/src/fnc/http.rs index 2a8e1f3b..eb89beee 100644 --- a/lib/src/fnc/http.rs +++ b/lib/src/fnc/http.rs @@ -1,6 +1,5 @@ use crate::ctx::Context; use crate::err::Error; -use crate::sql::object::Object; use crate::sql::value::Value; #[cfg(not(feature = "http"))] @@ -50,7 +49,7 @@ pub async fn head(_: &Context<'_>, mut args: Vec) -> Result }), }, 1 => match args.remove(0) { - Value::Strand(uri) => crate::fnc::util::http::head(uri, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::head(uri, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::head"), message: String::from("The first argument should be a string."), @@ -80,7 +79,7 @@ pub async fn get(_: &Context<'_>, mut args: Vec) -> Result }), }, 1 => match args.remove(0) { - Value::Strand(uri) => crate::fnc::util::http::get(uri, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::get(uri, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::get"), message: String::from("The first argument should be a string."), @@ -110,14 +109,14 @@ pub async fn put(_: &Context<'_>, mut args: Vec) -> Result }), }, 2 => match (args.remove(0), args.remove(0)) { - (Value::Strand(uri), val) => crate::fnc::util::http::put(uri, val, Object::default()).await, + (Value::Strand(uri), val) => crate::fnc::util::http::put(uri, val, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::put"), message: String::from("The first argument should be a string."), }), }, 1 => match args.remove(0) { - Value::Strand(uri) => crate::fnc::util::http::put(uri, Value::Null, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::put(uri, Value::Null, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::put"), message: String::from("The first argument should be a string."), @@ -147,14 +146,14 @@ pub async fn post(_: &Context<'_>, mut args: Vec) -> Result }), }, 2 => match (args.remove(0), args.remove(0)) { - (Value::Strand(uri), val) => crate::fnc::util::http::post(uri, val, Object::default()).await, + (Value::Strand(uri), val) => crate::fnc::util::http::post(uri, val, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::post"), message: String::from("The first argument should be a string."), }), }, 1 => match args.remove(0) { - Value::Strand(uri) => crate::fnc::util::http::post(uri, Value::Null, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::post(uri, Value::Null, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::post"), message: String::from("The first argument should be a string."), @@ -184,14 +183,14 @@ pub async fn patch(_: &Context<'_>, mut args: Vec) -> Result match (args.remove(0), args.remove(0)) { - (Value::Strand(uri), val) => crate::fnc::util::http::patch(uri, val, Object::default()).await, + (Value::Strand(uri), val) => crate::fnc::util::http::patch(uri, val, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::patch"), message: String::from("The first argument should be a string."), }), }, 1 => match args.remove(0) { - Value::Strand(uri) => crate::fnc::util::http::patch(uri, Value::Null, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::patch(uri, Value::Null, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::patch"), message: String::from("The first argument should be a string."), @@ -221,7 +220,7 @@ pub async fn delete(_: &Context<'_>, mut args: Vec) -> Result match args.remove(0) { - Value::Strand(uri) => crate::fnc::util::http::delete(uri, Object::default()).await, + Value::Strand(uri) => crate::fnc::util::http::delete(uri, None).await, _ => Err(Error::InvalidArguments { name: String::from("http::delete"), message: String::from("The first argument should be a string."), diff --git a/lib/src/fnc/script/globals/surrealdb.rs b/lib/src/fnc/script/globals/surrealdb.rs index 7143328d..362890a5 100644 --- a/lib/src/fnc/script/globals/surrealdb.rs +++ b/lib/src/fnc/script/globals/surrealdb.rs @@ -2,5 +2,5 @@ #[quickjs(rename = "surrealdb")] #[allow(non_upper_case_globals)] pub mod package { - pub const version: &str = crate::cnf::VERSION; + pub const version: &str = env!("CARGO_PKG_VERSION"); } diff --git a/lib/src/fnc/script/modules/os.rs b/lib/src/fnc/script/modules/os.rs index 6060efae..88a6b528 100644 --- a/lib/src/fnc/script/modules/os.rs +++ b/lib/src/fnc/script/modules/os.rs @@ -1,3 +1,9 @@ +macro_rules! get_cfg { + ($i:ident : $($s:expr),+) => ( + let $i = || { $( if cfg!($i=$s) { return $s; } );+ "unknown"}; + ) +} + #[js::bind(module, public)] #[quickjs(bare)] #[allow(non_upper_case_globals)] @@ -16,10 +22,10 @@ pub mod package { pub fn release() -> String { get_cfg!(target_os: "windows", "macos", "ios", "linux", "android", "freebsd", "openbsd", "netbsd"); get_cfg!(target_arch: "x86", "x86_64", "mips", "powerpc", "powerpc64", "arm", "aarch64"); - format!("{} for {} on {}", crate::cnf::VERSION, target_os(), target_arch()) + format!("{} for {} on {}", env!("CARGO_PKG_VERSION"), target_os(), target_arch()) } // Get the current version pub fn version() -> &'static str { - crate::cnf::VERSION + env!("CARGO_PKG_VERSION") } } diff --git a/lib/src/fnc/script/modules/surrealdb.rs b/lib/src/fnc/script/modules/surrealdb.rs index e5a3adf0..acf79797 100644 --- a/lib/src/fnc/script/modules/surrealdb.rs +++ b/lib/src/fnc/script/modules/surrealdb.rs @@ -2,5 +2,5 @@ #[quickjs(bare)] #[allow(non_upper_case_globals)] pub mod package { - pub const version: &str = crate::cnf::VERSION; + pub const version: &str = env!("CARGO_PKG_VERSION"); } diff --git a/lib/src/fnc/util/http/mod.rs b/lib/src/fnc/util/http/mod.rs index f203535b..ff85ea6e 100644 --- a/lib/src/fnc/util/http/mod.rs +++ b/lib/src/fnc/util/http/mod.rs @@ -4,13 +4,13 @@ use crate::sql::object::Object; use crate::sql::strand::Strand; use crate::sql::value::Value; -pub async fn head(uri: Strand, opts: Object) -> Result { +pub async fn head(uri: Strand, opts: impl Into) -> Result { // Start a new HEAD request let mut req = surf::head(uri.as_str()); // Add the User-Agent header req = req.header("User-Agent", "SurrealDB"); // Add specified header values - for (k, v) in opts.iter() { + for (k, v) in opts.into().iter() { req = req.header(k.as_str(), v.to_strand().as_str()); } // Send the request and wait @@ -22,13 +22,13 @@ pub async fn head(uri: Strand, opts: Object) -> Result { } } -pub async fn get(uri: Strand, opts: Object) -> Result { +pub async fn get(uri: Strand, opts: impl Into) -> Result { // Start a new GET request let mut req = surf::get(uri.as_str()); // Add the User-Agent header req = req.header("User-Agent", "SurrealDB"); // Add specified header values - for (k, v) in opts.iter() { + for (k, v) in opts.into().iter() { req = req.header(k.as_str(), v.to_strand().as_str()); } // Send the request and wait @@ -50,13 +50,13 @@ pub async fn get(uri: Strand, opts: Object) -> Result { } } -pub async fn put(uri: Strand, body: Value, opts: Object) -> Result { +pub async fn put(uri: Strand, body: Value, opts: impl Into) -> Result { // Start a new GET request let mut req = surf::put(uri.as_str()); // Add the User-Agent header req = req.header("User-Agent", "SurrealDB"); // Add specified header values - for (k, v) in opts.iter() { + for (k, v) in opts.into().iter() { req = req.header(k.as_str(), v.to_strand().as_str()); } // Submit the request body @@ -80,13 +80,13 @@ pub async fn put(uri: Strand, body: Value, opts: Object) -> Result } } -pub async fn post(uri: Strand, body: Value, opts: Object) -> Result { +pub async fn post(uri: Strand, body: Value, opts: impl Into) -> Result { // Start a new GET request let mut req = surf::post(uri.as_str()); // Add the User-Agent header req = req.header("User-Agent", "SurrealDB"); // Add specified header values - for (k, v) in opts.iter() { + for (k, v) in opts.into().iter() { req = req.header(k.as_str(), v.to_strand().as_str()); } // Submit the request body @@ -110,13 +110,13 @@ pub async fn post(uri: Strand, body: Value, opts: Object) -> Result Result { +pub async fn patch(uri: Strand, body: Value, opts: impl Into) -> Result { // Start a new GET request let mut req = surf::patch(uri.as_str()); // Add the User-Agent header req = req.header("User-Agent", "SurrealDB"); // Add specified header values - for (k, v) in opts.iter() { + for (k, v) in opts.into().iter() { req = req.header(k.as_str(), v.to_strand().as_str()); } // Submit the request body @@ -140,13 +140,13 @@ pub async fn patch(uri: Strand, body: Value, opts: Object) -> Result Result { +pub async fn delete(uri: Strand, opts: impl Into) -> Result { // Start a new GET request let mut req = surf::delete(uri.as_str()); // Add the User-Agent header req = req.header("User-Agent", "SurrealDB"); // Add specified header values - for (k, v) in opts.iter() { + for (k, v) in opts.into().iter() { req = req.header(k.as_str(), v.to_strand().as_str()); } // Send the request and wait diff --git a/lib/src/mac/mod.rs b/lib/src/mac/mod.rs index 5887410d..0b9e1bb8 100644 --- a/lib/src/mac/mod.rs +++ b/lib/src/mac/mod.rs @@ -11,9 +11,3 @@ macro_rules! map { m }}; } - -macro_rules! get_cfg { - ($i:ident : $($s:expr),+) => ( - let $i = || { $( if cfg!($i=$s) { return $s; } );+ "unknown"}; - ) -} diff --git a/lib/src/sql/object.rs b/lib/src/sql/object.rs index 1fcc6fb7..978c8dcb 100644 --- a/lib/src/sql/object.rs +++ b/lib/src/sql/object.rs @@ -40,6 +40,12 @@ impl From> for Object { } } +impl From> for Object { + fn from(v: Option) -> Self { + v.unwrap_or_default() + } +} + impl From for Object { fn from(v: Operation) -> Self { Object(map! {