128 lines
3.3 KiB
Rust
128 lines
3.3 KiB
Rust
pub use config::AdminModelConfig;
|
|
pub use dto::AdminApp;
|
|
pub use dto::AdminModel;
|
|
pub use repository::{AdminRepository, RepositoryInfo, RepositoryList};
|
|
|
|
mod auth {
|
|
|
|
struct AdminUser {}
|
|
|
|
struct AdminRole {}
|
|
|
|
struct AdminGroup {}
|
|
|
|
struct AdminActionLog {}
|
|
}
|
|
|
|
mod config {
|
|
// user uses this configuration object to register another model.
|
|
pub struct AdminModelConfig {
|
|
pub name: String,
|
|
pub app_key: String,
|
|
}
|
|
}
|
|
|
|
mod dto {
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
pub struct AdminModel {
|
|
pub key: String,
|
|
pub name: String,
|
|
|
|
pub admin_url: String,
|
|
pub view_only: bool,
|
|
|
|
pub add_url: Option<String>,
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
pub struct AdminApp {
|
|
pub key: String,
|
|
pub name: String,
|
|
|
|
pub app_url: String,
|
|
pub models: Vec<AdminModel>,
|
|
}
|
|
}
|
|
|
|
pub mod repository {
|
|
use serde::{Serialize, Serializer};
|
|
use serde_json::Value;
|
|
use std::vec::IntoIter;
|
|
|
|
pub enum RepositoryList {
|
|
Empty,
|
|
List {
|
|
values: Vec<Value>,
|
|
},
|
|
Page {
|
|
values: Vec<Value>,
|
|
offset: usize,
|
|
total: usize,
|
|
},
|
|
Stream {
|
|
values: Vec<Value>,
|
|
next_index: Option<String>,
|
|
},
|
|
}
|
|
|
|
impl IntoIterator for RepositoryList {
|
|
type Item = Value;
|
|
type IntoIter = IntoIter<Self::Item>;
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
match self {
|
|
RepositoryList::Empty => vec![].into_iter(),
|
|
RepositoryList::List { values } => values.into_iter(),
|
|
RepositoryList::Page { values, .. } => values.into_iter(),
|
|
RepositoryList::Stream { values, .. } => values.into_iter(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Serialize for RepositoryList {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
match self {
|
|
RepositoryList::Empty => serializer.serialize_unit(),
|
|
RepositoryList::List { values }
|
|
| RepositoryList::Page { values, .. }
|
|
| RepositoryList::Stream { values, .. } => values.serialize(serializer),
|
|
}
|
|
}
|
|
}
|
|
|
|
// each repository has to implement a repo info.
|
|
#[derive(Serialize)]
|
|
pub struct RepositoryInfo {
|
|
name: String,
|
|
lookup_key: String,
|
|
display_list: Vec<String>,
|
|
}
|
|
|
|
impl RepositoryInfo {
|
|
pub fn new(name: &str, lookup_key: &str) -> Self {
|
|
RepositoryInfo {
|
|
name: name.to_owned(),
|
|
lookup_key: lookup_key.to_owned(),
|
|
display_list: vec![],
|
|
}
|
|
}
|
|
|
|
pub fn display_list(mut self, display_list: &[&str]) -> RepositoryInfo {
|
|
self.display_list = display_list.iter().map(|&e| e.to_string()).collect();
|
|
self
|
|
}
|
|
}
|
|
|
|
pub trait AdminRepository: Send + Sync {
|
|
fn get_item(&self, id: usize) -> Option<Value>;
|
|
|
|
fn get_repo_info(&self) -> RepositoryInfo;
|
|
fn get_list(&self) -> RepositoryList;
|
|
}
|
|
}
|