use serde::ser::Serialize;
use rincon_core::api::method::{Method, Operation, Parameters, Prepare, RpcReturnType};
use rincon_core::arango::protocol::{FIELD_CODE, FIELD_RESULT,
PATH_API_DATABASE, PATH_CURRENT, PATH_USER};
use super::types::*;
use user::types::{NewUser, UserExtra};
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone, PartialEq)]
pub struct GetCurrentDatabase {}
#[cfg_attr(feature = "cargo-clippy", allow(new_without_default_derive))]
impl GetCurrentDatabase {
pub fn new() -> Self {
GetCurrentDatabase {}
}
}
impl Method for GetCurrentDatabase {
type Result = Database;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl Prepare for GetCurrentDatabase {
type Content = ();
fn operation(&self) -> Operation {
Operation::Read
}
fn path(&self) -> String {
String::from(PATH_API_DATABASE) + PATH_CURRENT
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
None
}
}
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone, PartialEq)]
pub struct ListDatabases {}
#[cfg_attr(feature = "cargo-clippy", allow(new_without_default_derive))]
impl ListDatabases {
pub fn new() -> Self {
ListDatabases {}
}
}
impl Method for ListDatabases {
type Result = Vec<String>;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl Prepare for ListDatabases {
type Content = ();
fn operation(&self) -> Operation {
Operation::Read
}
fn path(&self) -> String {
String::from(PATH_API_DATABASE)
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
None
}
}
#[allow(missing_copy_implementations)]
#[derive(Debug, Clone, PartialEq)]
pub struct ListAccessibleDatabases {}
#[cfg_attr(feature = "cargo-clippy", allow(new_without_default_derive))]
impl ListAccessibleDatabases {
pub fn new() -> Self {
ListAccessibleDatabases {}
}
}
impl Method for ListAccessibleDatabases {
type Result = Vec<String>;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl Prepare for ListAccessibleDatabases {
type Content = ();
fn operation(&self) -> Operation {
Operation::Read
}
fn path(&self) -> String {
String::from(PATH_API_DATABASE) + PATH_USER
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
None
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct CreateDatabase<E>
where E: UserExtra
{
database: NewDatabase<E>,
}
impl<E> CreateDatabase<E>
where E: UserExtra
{
pub fn new(database: NewDatabase<E>) -> Self {
CreateDatabase {
database,
}
}
pub fn with_name<N>(name: N) -> Self
where N: Into<String>
{
CreateDatabase {
database: NewDatabase::with_name(name),
}
}
pub fn with_name_for_users<N>(name: N, users: Vec<NewUser<E>>) -> Self
where N: Into<String>
{
CreateDatabase {
database: NewDatabase::new(name, users),
}
}
pub fn database(&self) -> &NewDatabase<E> {
&self.database
}
}
impl<E> Method for CreateDatabase<E>
where E: UserExtra
{
type Result = bool;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl<E> Prepare for CreateDatabase<E>
where E: UserExtra + Serialize
{
type Content = NewDatabase<E>;
fn operation(&self) -> Operation {
Operation::Create
}
fn path(&self) -> String {
String::from(PATH_API_DATABASE)
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
Some(&self.database)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DropDatabase {
database_name: String,
}
impl DropDatabase {
pub fn new(database_name: String) -> Self
{
DropDatabase {
database_name,
}
}
pub fn with_name<N>(name: N) -> Self
where N: Into<String>
{
DropDatabase {
database_name: name.into(),
}
}
pub fn database_name(&self) -> &str {
&self.database_name
}
}
impl Method for DropDatabase {
type Result = bool;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl Prepare for DropDatabase {
type Content = ();
fn operation(&self) -> Operation {
Operation::Delete
}
fn path(&self) -> String {
String::from(PATH_API_DATABASE) + "/" + &self.database_name
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
None
}
}