Fix linter warnings when using as external library

This commit is contained in:
Tobie Morgan Hitchcock 2022-07-27 09:15:10 +01:00
parent d6269bbe3d
commit f633769b57
8 changed files with 37 additions and 35 deletions

View file

@ -5,9 +5,6 @@ pub const MAX_CONCURRENT_TASKS: usize = 64;
// Specifies how many subqueries will be processed recursively before the query fails. // Specifies how many subqueries will be processed recursively before the query fails.
pub const MAX_RECURSIVE_QUERIES: usize = 16; 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. // The characters which are supported in server record IDs.
pub const ID_CHARS: [char; 36] = [ 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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',

View file

@ -1,6 +1,5 @@
use crate::ctx::Context; use crate::ctx::Context;
use crate::err::Error; use crate::err::Error;
use crate::sql::object::Object;
use crate::sql::value::Value; use crate::sql::value::Value;
#[cfg(not(feature = "http"))] #[cfg(not(feature = "http"))]
@ -50,7 +49,7 @@ pub async fn head(_: &Context<'_>, mut args: Vec<Value>) -> Result<Value, Error>
}), }),
}, },
1 => match args.remove(0) { 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::head"), name: String::from("http::head"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),
@ -80,7 +79,7 @@ pub async fn get(_: &Context<'_>, mut args: Vec<Value>) -> Result<Value, Error>
}), }),
}, },
1 => match args.remove(0) { 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::get"), name: String::from("http::get"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),
@ -110,14 +109,14 @@ pub async fn put(_: &Context<'_>, mut args: Vec<Value>) -> Result<Value, Error>
}), }),
}, },
2 => match (args.remove(0), args.remove(0)) { 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::put"), name: String::from("http::put"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),
}), }),
}, },
1 => match args.remove(0) { 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::put"), name: String::from("http::put"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),
@ -147,14 +146,14 @@ pub async fn post(_: &Context<'_>, mut args: Vec<Value>) -> Result<Value, Error>
}), }),
}, },
2 => match (args.remove(0), args.remove(0)) { 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::post"), name: String::from("http::post"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),
}), }),
}, },
1 => match args.remove(0) { 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::post"), name: String::from("http::post"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),
@ -184,14 +183,14 @@ pub async fn patch(_: &Context<'_>, mut args: Vec<Value>) -> Result<Value, Error
}), }),
}, },
2 => match (args.remove(0), args.remove(0)) { 2 => 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::patch"), name: String::from("http::patch"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),
}), }),
}, },
1 => match args.remove(0) { 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::patch"), name: String::from("http::patch"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),
@ -221,7 +220,7 @@ pub async fn delete(_: &Context<'_>, mut args: Vec<Value>) -> Result<Value, Erro
}), }),
}, },
1 => match args.remove(0) { 1 => 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 { _ => Err(Error::InvalidArguments {
name: String::from("http::delete"), name: String::from("http::delete"),
message: String::from("The first argument should be a string."), message: String::from("The first argument should be a string."),

View file

@ -2,5 +2,5 @@
#[quickjs(rename = "surrealdb")] #[quickjs(rename = "surrealdb")]
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub mod package { pub mod package {
pub const version: &str = crate::cnf::VERSION; pub const version: &str = env!("CARGO_PKG_VERSION");
} }

View file

@ -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)] #[js::bind(module, public)]
#[quickjs(bare)] #[quickjs(bare)]
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
@ -16,10 +22,10 @@ pub mod package {
pub fn release() -> String { pub fn release() -> String {
get_cfg!(target_os: "windows", "macos", "ios", "linux", "android", "freebsd", "openbsd", "netbsd"); get_cfg!(target_os: "windows", "macos", "ios", "linux", "android", "freebsd", "openbsd", "netbsd");
get_cfg!(target_arch: "x86", "x86_64", "mips", "powerpc", "powerpc64", "arm", "aarch64"); 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 // Get the current version
pub fn version() -> &'static str { pub fn version() -> &'static str {
crate::cnf::VERSION env!("CARGO_PKG_VERSION")
} }
} }

View file

@ -2,5 +2,5 @@
#[quickjs(bare)] #[quickjs(bare)]
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub mod package { pub mod package {
pub const version: &str = crate::cnf::VERSION; pub const version: &str = env!("CARGO_PKG_VERSION");
} }

View file

@ -4,13 +4,13 @@ use crate::sql::object::Object;
use crate::sql::strand::Strand; use crate::sql::strand::Strand;
use crate::sql::value::Value; use crate::sql::value::Value;
pub async fn head(uri: Strand, opts: Object) -> Result<Value, Error> { pub async fn head(uri: Strand, opts: impl Into<Object>) -> Result<Value, Error> {
// Start a new HEAD request // Start a new HEAD request
let mut req = surf::head(uri.as_str()); let mut req = surf::head(uri.as_str());
// Add the User-Agent header // Add the User-Agent header
req = req.header("User-Agent", "SurrealDB"); req = req.header("User-Agent", "SurrealDB");
// Add specified header values // 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()); req = req.header(k.as_str(), v.to_strand().as_str());
} }
// Send the request and wait // Send the request and wait
@ -22,13 +22,13 @@ pub async fn head(uri: Strand, opts: Object) -> Result<Value, Error> {
} }
} }
pub async fn get(uri: Strand, opts: Object) -> Result<Value, Error> { pub async fn get(uri: Strand, opts: impl Into<Object>) -> Result<Value, Error> {
// Start a new GET request // Start a new GET request
let mut req = surf::get(uri.as_str()); let mut req = surf::get(uri.as_str());
// Add the User-Agent header // Add the User-Agent header
req = req.header("User-Agent", "SurrealDB"); req = req.header("User-Agent", "SurrealDB");
// Add specified header values // 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()); req = req.header(k.as_str(), v.to_strand().as_str());
} }
// Send the request and wait // Send the request and wait
@ -50,13 +50,13 @@ pub async fn get(uri: Strand, opts: Object) -> Result<Value, Error> {
} }
} }
pub async fn put(uri: Strand, body: Value, opts: Object) -> Result<Value, Error> { pub async fn put(uri: Strand, body: Value, opts: impl Into<Object>) -> Result<Value, Error> {
// Start a new GET request // Start a new GET request
let mut req = surf::put(uri.as_str()); let mut req = surf::put(uri.as_str());
// Add the User-Agent header // Add the User-Agent header
req = req.header("User-Agent", "SurrealDB"); req = req.header("User-Agent", "SurrealDB");
// Add specified header values // 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()); req = req.header(k.as_str(), v.to_strand().as_str());
} }
// Submit the request body // Submit the request body
@ -80,13 +80,13 @@ pub async fn put(uri: Strand, body: Value, opts: Object) -> Result<Value, Error>
} }
} }
pub async fn post(uri: Strand, body: Value, opts: Object) -> Result<Value, Error> { pub async fn post(uri: Strand, body: Value, opts: impl Into<Object>) -> Result<Value, Error> {
// Start a new GET request // Start a new GET request
let mut req = surf::post(uri.as_str()); let mut req = surf::post(uri.as_str());
// Add the User-Agent header // Add the User-Agent header
req = req.header("User-Agent", "SurrealDB"); req = req.header("User-Agent", "SurrealDB");
// Add specified header values // 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()); req = req.header(k.as_str(), v.to_strand().as_str());
} }
// Submit the request body // Submit the request body
@ -110,13 +110,13 @@ pub async fn post(uri: Strand, body: Value, opts: Object) -> Result<Value, Error
} }
} }
pub async fn patch(uri: Strand, body: Value, opts: Object) -> Result<Value, Error> { pub async fn patch(uri: Strand, body: Value, opts: impl Into<Object>) -> Result<Value, Error> {
// Start a new GET request // Start a new GET request
let mut req = surf::patch(uri.as_str()); let mut req = surf::patch(uri.as_str());
// Add the User-Agent header // Add the User-Agent header
req = req.header("User-Agent", "SurrealDB"); req = req.header("User-Agent", "SurrealDB");
// Add specified header values // 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()); req = req.header(k.as_str(), v.to_strand().as_str());
} }
// Submit the request body // Submit the request body
@ -140,13 +140,13 @@ pub async fn patch(uri: Strand, body: Value, opts: Object) -> Result<Value, Erro
} }
} }
pub async fn delete(uri: Strand, opts: Object) -> Result<Value, Error> { pub async fn delete(uri: Strand, opts: impl Into<Object>) -> Result<Value, Error> {
// Start a new GET request // Start a new GET request
let mut req = surf::delete(uri.as_str()); let mut req = surf::delete(uri.as_str());
// Add the User-Agent header // Add the User-Agent header
req = req.header("User-Agent", "SurrealDB"); req = req.header("User-Agent", "SurrealDB");
// Add specified header values // 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()); req = req.header(k.as_str(), v.to_strand().as_str());
} }
// Send the request and wait // Send the request and wait

View file

@ -11,9 +11,3 @@ macro_rules! map {
m m
}}; }};
} }
macro_rules! get_cfg {
($i:ident : $($s:expr),+) => (
let $i = || { $( if cfg!($i=$s) { return $s; } );+ "unknown"};
)
}

View file

@ -40,6 +40,12 @@ impl From<HashMap<String, Value>> for Object {
} }
} }
impl From<Option<Object>> for Object {
fn from(v: Option<Object>) -> Self {
v.unwrap_or_default()
}
}
impl From<Operation> for Object { impl From<Operation> for Object {
fn from(v: Operation) -> Self { fn from(v: Operation) -> Self {
Object(map! { Object(map! {