use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum EntityType {
Schema,
Graph,
GraphType,
VertexType,
EdgeType,
Index,
User,
Role,
Ace, Collection,
Metric,
DefaultSchema,
Store, Procedure, }
impl fmt::Display for EntityType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
EntityType::Schema => "schema",
EntityType::Graph => "graph",
EntityType::GraphType => "graph_type",
EntityType::VertexType => "vertex_type",
EntityType::EdgeType => "edge_type",
EntityType::Index => "index",
EntityType::User => "user",
EntityType::Role => "role",
EntityType::Ace => "ace",
EntityType::Collection => "collection",
EntityType::Metric => "metric",
EntityType::DefaultSchema => "default_schema",
EntityType::Store => "store",
EntityType::Procedure => "procedure",
};
write!(f, "{}", s)
}
}
impl From<&str> for EntityType {
fn from(s: &str) -> Self {
match s {
"schema" => EntityType::Schema,
"graph" => EntityType::Graph,
"graph_type" => EntityType::GraphType,
"vertex_type" => EntityType::VertexType,
"edge_type" => EntityType::EdgeType,
"index" => EntityType::Index,
"user" => EntityType::User,
"role" => EntityType::Role,
"ace" => EntityType::Ace,
"collection" => EntityType::Collection,
"metric" => EntityType::Metric,
"default_schema" => EntityType::DefaultSchema,
"store" => EntityType::Store,
"procedure" => EntityType::Procedure,
_ => EntityType::Schema, }
}
}
impl From<String> for EntityType {
fn from(s: String) -> Self {
EntityType::from(s.as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum QueryType {
Get,
GetSchema,
GetGraph,
GetGraphType,
GetUser,
GetDefault,
List,
Search,
CurrentSchema,
CurrentGraph,
Authenticate,
Exists,
GetRole,
ListRoles,
ListUsers,
GetAcesForResource,
GetAcesForPrincipal,
ByStatus,
BySchema,
GetVersion, ListVersions, }
impl fmt::Display for QueryType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
QueryType::Get => "get",
QueryType::GetSchema => "get_schema",
QueryType::GetGraph => "get_graph",
QueryType::GetGraphType => "get_graph_type",
QueryType::GetUser => "get_user",
QueryType::GetDefault => "get_default",
QueryType::List => "list",
QueryType::Search => "search",
QueryType::CurrentSchema => "current_schema",
QueryType::CurrentGraph => "current_graph",
QueryType::Authenticate => "authenticate",
QueryType::Exists => "exists",
QueryType::GetRole => "get_role",
QueryType::ListRoles => "list_roles",
QueryType::ListUsers => "list_users",
QueryType::GetAcesForResource => "get_aces_for_resource",
QueryType::GetAcesForPrincipal => "get_aces_for_principal",
QueryType::ByStatus => "by_status",
QueryType::BySchema => "by_schema",
QueryType::GetVersion => "get_version",
QueryType::ListVersions => "list_versions",
};
write!(f, "{}", s)
}
}
impl From<&str> for QueryType {
fn from(s: &str) -> Self {
match s {
"get" => QueryType::Get,
"get_schema" => QueryType::GetSchema,
"get_graph" => QueryType::GetGraph,
"get_graph_type" => QueryType::GetGraphType,
"get_user" => QueryType::GetUser,
"get_default" => QueryType::GetDefault,
"list" => QueryType::List,
"search" => QueryType::Search,
"current_schema" => QueryType::CurrentSchema,
"current_graph" => QueryType::CurrentGraph,
"authenticate" => QueryType::Authenticate,
"exists" => QueryType::Exists,
"get_role" => QueryType::GetRole,
"list_roles" => QueryType::ListRoles,
"list_users" => QueryType::ListUsers,
"get_aces_for_resource" => QueryType::GetAcesForResource,
"get_aces_for_principal" => QueryType::GetAcesForPrincipal,
"by_status" => QueryType::ByStatus,
"by_schema" => QueryType::BySchema,
"get_version" => QueryType::GetVersion,
"list_versions" => QueryType::ListVersions,
_ => QueryType::Get, }
}
}
impl From<String> for QueryType {
fn from(s: String) -> Self {
QueryType::from(s.as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CatalogOperation {
Create {
entity_type: EntityType,
name: String,
params: Value,
},
Drop {
entity_type: EntityType,
name: String,
cascade: bool,
},
Register { name: String, params: Value },
Unregister { name: String, cascade: bool },
Query {
query_type: QueryType,
params: Value,
},
Update {
entity_type: EntityType,
name: String,
updates: Value,
},
List {
entity_type: EntityType,
filters: Option<Value>,
},
Serialize,
Deserialize { data: Vec<u8> },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CatalogResponse {
Success { data: Option<Value> },
Error { message: String },
List { items: Vec<Value> },
Query { results: Value },
NotSupported,
}
impl CatalogResponse {
pub fn success() -> Self {
Self::Success { data: None }
}
pub fn success_with_data(data: Value) -> Self {
Self::Success { data: Some(data) }
}
pub fn error<S: Into<String>>(message: S) -> Self {
Self::Error {
message: message.into(),
}
}
pub fn list(items: Vec<Value>) -> Self {
Self::List { items }
}
pub fn query(results: Value) -> Self {
Self::Query { results }
}
pub fn is_success(&self) -> bool {
matches!(self, Self::Success { .. })
}
pub fn is_error(&self) -> bool {
matches!(self, Self::Error { .. })
}
pub fn is_not_supported(&self) -> bool {
matches!(self, Self::NotSupported)
}
pub fn data(&self) -> Option<&Value> {
match self {
Self::Success { data } => data.as_ref(),
_ => None,
}
}
pub fn error_message(&self) -> Option<&str> {
match self {
Self::Error { message } => Some(message),
_ => None,
}
}
pub fn items(&self) -> Option<&[Value]> {
match self {
Self::List { items } => Some(items),
_ => None,
}
}
pub fn results(&self) -> Option<&Value> {
match self {
Self::Query { results } => Some(results),
_ => None,
}
}
}