use std::collections::HashMap;
use std::marker::PhantomData;
use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use rincon_core::api::method::{Method, Operation, Parameters, Prepare, RpcReturnType};
use rincon_core::api::types::Empty;
use rincon_core::arango::protocol::{FIELD_CODE, FIELD_RESULT, PATH_DATABASE,
PATH_API_USER};
use super::types::*;
#[derive(Debug, Clone, PartialEq)]
pub struct CreateUser<E>
where E: UserExtra
{
user: NewUser<E>,
}
impl<E> CreateUser<E>
where E: UserExtra
{
pub fn new(user: NewUser<E>) -> Self {
CreateUser {
user,
}
}
pub fn user(&self) -> &NewUser<E> {
&self.user
}
}
impl<E> Method for CreateUser<E>
where E: UserExtra + DeserializeOwned
{
type Result = User<E>;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl<E> Prepare for CreateUser<E>
where E: UserExtra + Serialize
{
type Content = NewUser<E>;
fn operation(&self) -> Operation {
Operation::Create
}
fn path(&self) -> String {
String::from(PATH_API_USER)
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
Some(&self.user)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ListUsers<E>
where E: UserExtra
{
user_info_type: PhantomData<E>,
}
#[cfg_attr(feature = "cargo-clippy", allow(new_without_default_derive))]
impl<E> ListUsers<E>
where E: UserExtra
{
pub fn new() -> Self {
ListUsers {
user_info_type: PhantomData,
}
}
}
impl<E> Method for ListUsers<E>
where E: UserExtra + DeserializeOwned
{
type Result = Vec<User<E>>;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl<E> Prepare for ListUsers<E>
where E: UserExtra
{
type Content = ();
fn operation(&self) -> Operation {
Operation::Read
}
fn path(&self) -> String {
String::from(PATH_API_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 DeleteUser {
name: String,
}
impl DeleteUser {
pub fn with_name<S>(user_name: S) -> Self
where S: Into<String>
{
DeleteUser {
name: user_name.into(),
}
}
pub fn name(&self) -> &str {
&self.name
}
}
impl Method for DeleteUser {
type Result = Empty;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl Prepare for DeleteUser {
type Content = ();
fn operation(&self) -> Operation {
Operation::Delete
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.name
}
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 GetUser<E>
where E: UserExtra
{
name: String,
user_info_type: PhantomData<E>,
}
impl<E> GetUser<E>
where E: UserExtra
{
pub fn new(user_name: String) -> Self {
GetUser {
name: user_name,
user_info_type: PhantomData,
}
}
pub fn with_name<S>(user_name: S) -> Self
where S: Into<String>
{
GetUser {
name: user_name.into(),
user_info_type: PhantomData,
}
}
pub fn name(&self) -> &str {
&self.name
}
}
impl<E> Method for GetUser<E>
where E: UserExtra + DeserializeOwned
{
type Result = User<E>;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl<E> Prepare for GetUser<E>
where E: UserExtra
{
type Content = ();
fn operation(&self) -> Operation {
Operation::Read
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.name
}
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 ModifyUser<E>
where E: UserExtra
{
user_name: String,
updates: UserUpdate<E>,
}
impl<E> ModifyUser<E>
where E: UserExtra
{
pub fn new(user_name: String, updates: UserUpdate<E>) -> Self {
ModifyUser {
user_name,
updates,
}
}
pub fn with_name<N>(name: N) -> Self
where N: Into<String>
{
ModifyUser {
user_name: name.into(),
updates: UserUpdate::empty(),
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
pub fn updates(&self) -> &UserUpdate<E> {
&self.updates
}
}
impl<E> Method for ModifyUser<E>
where E: UserExtra + DeserializeOwned
{
type Result = User<E>;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl<E> Prepare for ModifyUser<E>
where E: UserExtra + Serialize
{
type Content = UserUpdate<E>;
fn operation(&self) -> Operation {
Operation::Modify
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
Some(&self.updates)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ReplaceUser<E>
where E: UserExtra
{
user_name: String,
updates: UserUpdate<E>,
}
impl<E> ReplaceUser<E>
where E: UserExtra
{
pub fn new(user_name: String, updates: UserUpdate<E>) -> Self {
ReplaceUser {
user_name,
updates,
}
}
pub fn with_name<N>(name: N) -> Self
where N: Into<String>
{
ReplaceUser {
user_name: name.into(),
updates: UserUpdate::empty(),
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
pub fn updates(&self) -> &UserUpdate<E> {
&self.updates
}
pub fn updates_mut(&mut self) -> &mut UserUpdate<E> {
&mut self.updates
}
}
impl<E> Method for ReplaceUser<E>
where E: UserExtra + DeserializeOwned
{
type Result = User<E>;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl<E> Prepare for ReplaceUser<E>
where E: UserExtra + Serialize
{
type Content = UserUpdate<E>;
fn operation(&self) -> Operation {
Operation::Replace
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
Some(&self.updates)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ListDatabasesForUser {
user_name: String,
}
impl ListDatabasesForUser {
pub fn new(user_name: String) -> Self {
ListDatabasesForUser {
user_name,
}
}
pub fn for_user<N>(name: N) -> Self
where N: Into<String>
{
ListDatabasesForUser {
user_name: name.into(),
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
}
impl Method for ListDatabasesForUser {
type Result = HashMap<String, Permission>;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl Prepare for ListDatabasesForUser {
type Content = ();
fn operation(&self) -> Operation {
Operation::Read
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
+ PATH_DATABASE
}
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 GetDatabaseAccessLevel {
user_name: String,
database: String,
}
impl GetDatabaseAccessLevel {
pub fn new(user_name: String, database: String) -> Self {
GetDatabaseAccessLevel {
user_name,
database,
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
pub fn database(&self) -> &str {
&self.database
}
}
impl Method for GetDatabaseAccessLevel {
type Result = Permission;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl Prepare for GetDatabaseAccessLevel {
type Content = ();
fn operation(&self) -> Operation {
Operation::Read
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
+ PATH_DATABASE + "/" + &self.database
}
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 SetDatabaseAccessLevel {
user_name: String,
database: String,
access_level: NewAccessLevel,
}
impl SetDatabaseAccessLevel {
pub fn new(user_name: String, database: String, grant: Permission) -> Self {
SetDatabaseAccessLevel {
user_name,
database,
access_level: NewAccessLevel::new(grant),
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
pub fn database(&self) -> &str {
&self.database
}
pub fn access_level(&self) -> &NewAccessLevel {
&self.access_level
}
}
impl Method for SetDatabaseAccessLevel {
type Result = Empty;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl Prepare for SetDatabaseAccessLevel {
type Content = NewAccessLevel;
fn operation(&self) -> Operation {
Operation::Replace
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
+ PATH_DATABASE + "/" + &self.database
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
Some(&self.access_level)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ResetDatabaseAccessLevel {
user_name: String,
database: String,
}
impl ResetDatabaseAccessLevel {
pub fn new(user_name: String, database: String) -> Self {
ResetDatabaseAccessLevel {
user_name,
database,
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
pub fn database(&self) -> &str {
&self.database
}
}
impl Method for ResetDatabaseAccessLevel {
type Result = Empty;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl Prepare for ResetDatabaseAccessLevel {
type Content = ();
fn operation(&self) -> Operation {
Operation::Delete
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
+ PATH_DATABASE + "/" + &self.database
}
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 GetCollectionAccessLevel {
user_name: String,
database: String,
collection: String,
}
impl GetCollectionAccessLevel {
pub fn new(user_name: String, database: String, collection: String) -> Self {
GetCollectionAccessLevel {
user_name,
database,
collection,
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
pub fn database(&self) -> &str {
&self.database
}
pub fn collection(&self) -> &str {
&self.collection
}
}
impl Method for GetCollectionAccessLevel {
type Result = Permission;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: Some(FIELD_RESULT),
code_field: Some(FIELD_CODE),
};
}
impl Prepare for GetCollectionAccessLevel {
type Content = ();
fn operation(&self) -> Operation {
Operation::Read
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
+ PATH_DATABASE + "/" + &self.database
+ "/" + &self.collection
}
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 SetCollectionAccessLevel {
user_name: String,
database: String,
collection: String,
access_level: NewAccessLevel,
}
impl SetCollectionAccessLevel {
pub fn new(user_name: String,
database: String,
collection: String,
grant: Permission
) -> Self {
SetCollectionAccessLevel {
user_name,
database,
collection,
access_level: NewAccessLevel::new(grant),
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
pub fn database(&self) -> &str {
&self.database
}
pub fn collection(&self) -> &str {
&self.collection
}
pub fn access_level(&self) -> &NewAccessLevel {
&self.access_level
}
}
impl Method for SetCollectionAccessLevel {
type Result = Empty;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl Prepare for SetCollectionAccessLevel {
type Content = NewAccessLevel;
fn operation(&self) -> Operation {
Operation::Replace
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
+ PATH_DATABASE + "/" + &self.database
+ "/" + &self.collection
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
Some(&self.access_level)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ResetCollectionAccessLevel {
user_name: String,
database: String,
collection: String,
}
impl ResetCollectionAccessLevel {
pub fn new(user_name: String, database: String, collection: String) -> Self {
ResetCollectionAccessLevel {
user_name,
database,
collection,
}
}
pub fn user_name(&self) -> &str {
&self.user_name
}
pub fn database(&self) -> &str {
&self.database
}
pub fn collection(&self) -> &str {
&self.collection
}
}
impl Method for ResetCollectionAccessLevel {
type Result = Empty;
const RETURN_TYPE: RpcReturnType = RpcReturnType {
result_field: None,
code_field: Some(FIELD_CODE),
};
}
impl Prepare for ResetCollectionAccessLevel {
type Content = ();
fn operation(&self) -> Operation {
Operation::Delete
}
fn path(&self) -> String {
String::from(PATH_API_USER) + "/" + &self.user_name
+ PATH_DATABASE + "/" + &self.database
+ "/" + &self.collection
}
fn parameters(&self) -> Parameters {
Parameters::empty()
}
fn header(&self) -> Parameters {
Parameters::empty()
}
fn content(&self) -> Option<&Self::Content> {
None
}
}