code: static init
This commit is contained in:
parent
cdfa399060
commit
d1c98516a8
73
Cargo.lock
generated
73
Cargo.lock
generated
@ -792,6 +792,41 @@ dependencies = [
|
|||||||
"typenum",
|
"typenum",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling"
|
||||||
|
version = "0.14.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"darling_macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_core"
|
||||||
|
version = "0.14.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
|
||||||
|
dependencies = [
|
||||||
|
"fnv",
|
||||||
|
"ident_case",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"strsim",
|
||||||
|
"syn 1.0.109",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_macro"
|
||||||
|
version = "0.14.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"quote",
|
||||||
|
"syn 1.0.109",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "der"
|
name = "der"
|
||||||
version = "0.7.8"
|
version = "0.7.8"
|
||||||
@ -824,6 +859,37 @@ dependencies = [
|
|||||||
"syn 1.0.109",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive_builder"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "660047478bc508c0fde22c868991eec0c40a63e48d610befef466d48e2bee574"
|
||||||
|
dependencies = [
|
||||||
|
"derive_builder_macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive_builder_core"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9b217e6dd1011a54d12f3b920a411b5abd44b1716ecfe94f5f2f2f7b52e08ab7"
|
||||||
|
dependencies = [
|
||||||
|
"darling",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 1.0.109",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "derive_builder_macro"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7a5f77d7e20ac9153428f7ca14a88aba652adfc7a0ef0a06d654386310ef663b"
|
||||||
|
dependencies = [
|
||||||
|
"derive_builder_core",
|
||||||
|
"syn 1.0.109",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "deunicode"
|
name = "deunicode"
|
||||||
version = "1.4.2"
|
version = "1.4.2"
|
||||||
@ -1472,6 +1538,12 @@ dependencies = [
|
|||||||
"cc",
|
"cc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ident_case"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "idna"
|
name = "idna"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
@ -1755,6 +1827,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"axum 0.7.4",
|
"axum 0.7.4",
|
||||||
"barrel",
|
"barrel",
|
||||||
|
"derive_builder",
|
||||||
"dotenvy",
|
"dotenvy",
|
||||||
"dunce",
|
"dunce",
|
||||||
"entity",
|
"entity",
|
||||||
|
@ -50,3 +50,4 @@ log = "0.4.20"
|
|||||||
env_logger = "0.11"
|
env_logger = "0.11"
|
||||||
serde_json = "1.0.108"
|
serde_json = "1.0.108"
|
||||||
slug = "0.1.5"
|
slug = "0.1.5"
|
||||||
|
derive_builder = "0.13.0"
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
pub use config::AdminModelConfig;
|
pub use config::AdminModelConfig;
|
||||||
pub use dto::AdminApp;
|
pub use dto::AdminApp;
|
||||||
pub use dto::AdminModel;
|
pub use dto::AdminModel;
|
||||||
pub use repository::{AdminRepository, RepositoryInfo, RepositoryList};
|
pub use repository::{
|
||||||
|
AdminRepository, RepoInfo, RepositoryInfo, RepositoryInfoBuilder, RepositoryList,
|
||||||
|
};
|
||||||
|
|
||||||
mod auth {
|
mod auth {
|
||||||
|
|
||||||
@ -47,6 +49,7 @@ mod dto {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod repository {
|
pub mod repository {
|
||||||
|
use derive_builder::Builder;
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::vec::IntoIter;
|
use std::vec::IntoIter;
|
||||||
@ -95,8 +98,18 @@ pub mod repository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Static initializer for RepositoryInfo.
|
||||||
|
pub struct RepoInfo {
|
||||||
|
pub name: &'static str,
|
||||||
|
pub lookup_key: &'static str,
|
||||||
|
pub display_list: &'static [&'static str],
|
||||||
|
}
|
||||||
|
|
||||||
|
// consider builder: https://docs.rs/derive_builder/latest/derive_builder/
|
||||||
|
// downside of builders as macro: no intellisense!
|
||||||
// each repository has to implement a repo info.
|
// each repository has to implement a repo info.
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize, Builder)]
|
||||||
|
#[builder(setter(into))]
|
||||||
pub struct RepositoryInfo {
|
pub struct RepositoryInfo {
|
||||||
name: String,
|
name: String,
|
||||||
lookup_key: String,
|
lookup_key: String,
|
||||||
@ -112,16 +125,34 @@ pub mod repository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* // self mutating builder pattern?
|
||||||
pub fn display_list(mut self, display_list: &[&str]) -> RepositoryInfo {
|
pub fn display_list(mut self, display_list: &[&str]) -> RepositoryInfo {
|
||||||
self.display_list = display_list.iter().map(|&e| e.to_string()).collect();
|
self.display_list = display_list.iter().map(|&e| e.to_string()).collect();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RepoInfo> for RepositoryInfo {
|
||||||
|
fn from(repo_info: RepoInfo) -> Self {
|
||||||
|
RepositoryInfo {
|
||||||
|
name: repo_info.name.to_string(),
|
||||||
|
lookup_key: repo_info.lookup_key.to_string(),
|
||||||
|
display_list: repo_info
|
||||||
|
.display_list
|
||||||
|
.iter()
|
||||||
|
.map(|&s| s.to_string())
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait AdminRepository: Send + Sync {
|
pub trait AdminRepository: Send + Sync {
|
||||||
fn get_item(&self, id: usize) -> Option<Value>;
|
fn info(&self) -> RepositoryInfo;
|
||||||
|
fn list(&self) -> RepositoryList;
|
||||||
fn get_repo_info(&self) -> RepositoryInfo;
|
fn get(&self, id: usize) -> Option<Value>;
|
||||||
fn get_list(&self) -> RepositoryList;
|
fn create(&self, data: Value) -> Option<Value>;
|
||||||
|
fn update(&self, id: usize, data: Value) -> Option<Value>;
|
||||||
|
fn delete(&self, id: usize) -> Option<Value>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
// implementation of static repository
|
// implementation of static repository
|
||||||
|
|
||||||
use super::domain::{AdminModelConfig, AdminRepository, RepositoryInfo, RepositoryList};
|
use super::domain::{AdminModelConfig, AdminRepository, RepoInfo, RepositoryInfo, RepositoryList};
|
||||||
use super::state::AdminRegistry;
|
use super::state::AdminRegistry;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
@ -25,19 +25,38 @@ impl MyStaticRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AdminRepository for MyStaticRepository {
|
impl AdminRepository for MyStaticRepository {
|
||||||
fn get_item(&self, id: usize) -> Option<Value> {
|
fn get(&self, id: usize) -> Option<Value> {
|
||||||
self.content.get(id).cloned()
|
self.content.get(id).cloned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_list(&self) -> RepositoryList {
|
fn list(&self) -> RepositoryList {
|
||||||
RepositoryList::List {
|
RepositoryList::List {
|
||||||
values: self.content.clone(),
|
values: self.content.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_repo_info(&self) -> RepositoryInfo {
|
fn info(&self) -> RepositoryInfo {
|
||||||
RepositoryInfo::new("My Static Repository", "id")
|
RepoInfo {
|
||||||
.display_list(&["id", "name", "age", "level"])
|
name: "My Static Repository",
|
||||||
|
lookup_key: "id",
|
||||||
|
display_list: &["id", "name", "level", "age"],
|
||||||
|
}
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create(&self, data: Value) -> Option<Value> {
|
||||||
|
println!("I would now create: {}", data);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&self, id: usize, data: Value) -> Option<Value> {
|
||||||
|
println!("I would now update: {}, {}", id, data);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete(&self, id: usize) -> Option<Value> {
|
||||||
|
println!("Would delete: {}", id);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,8 +109,8 @@ pub async fn list_item_collection(
|
|||||||
AdminContext {
|
AdminContext {
|
||||||
available_apps: registry.get_apps(),
|
available_apps: registry.get_apps(),
|
||||||
content: model_key.to_owned(),
|
content: model_key.to_owned(),
|
||||||
item_info: Some(repo.get_repo_info()),
|
item_info: Some(repo.info()),
|
||||||
item_list: repo.get_list(),
|
item_list: repo.list(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user