use crate::ArangoQuery;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use reqwest::{Body, Client};
impl From<ArangoQuery> for Body {
fn from(item: ArangoQuery) -> Self {
let b = serde_json::to_vec(&item).unwrap();
b.into()
}
}
#[derive(Clone)]
pub struct ArangoConnection {
pub host: Arc<String>,
pub database: Arc<String>,
pub client: Arc<Client>,
pub context: Arc<Context>,
}
impl ArangoConnection {
#[must_use]
pub fn new(host: String, database: String, client: Client) -> Self {
Self::with_context(host, database, client, Context::default())
}
#[must_use]
pub fn with_context(host: String, database: String, client: Client, context: Context) -> Self {
ArangoConnection {
host: Arc::new(host),
database: Arc::new(database),
client: Arc::new(client),
context: Arc::new(context),
}
}
#[must_use]
pub fn cursor(&self) -> String {
format!("{}/_db/{}/_api/cursor", self.host, self.database)
}
#[must_use]
pub fn collection(&self) -> String {
format!("{}/_db/{}/_api/collection", self.host, self.database)
}
}
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Default)]
pub struct CollectionMandatory {
#[serde(skip_serializing_if = "String::is_empty", default)]
pub(crate) _key: String,
#[serde(skip_serializing_if = "String::is_empty", default)]
pub(crate) _id: String,
#[serde(skip_serializing_if = "String::is_empty", default)]
pub(crate) _rev: String,
#[serde(rename = "_oldRev", skip_serializing_if = "String::is_empty", default)]
pub(crate) _old_rev: String,
#[serde(flatten)]
pub(crate) extra: HashMap<String, Value>,
}
impl CollectionMandatory {
#[must_use]
pub fn with_key(_key: &str) -> Self {
Self { _key: _key.to_owned(), ..Self::default() }
}
#[must_use]
pub fn id(&self) -> &str {
&self._id
}
#[must_use]
pub fn key(&self) -> &str {
&self._key
}
}
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Context {
pub app_prefix: String,
}
impl Context {
#[must_use]
pub fn collection_name(&self, local_name: &str) -> String {
if self.app_prefix.is_empty() {
local_name.to_owned()
} else {
format!("{}_{}", self.app_prefix, local_name)
}
}
}