singleton macro thing

This commit is contained in:
yuvlian
2026-02-14 17:55:54 +07:00
parent d16cfd2494
commit 448bbe5bda
3 changed files with 72 additions and 11 deletions

View File

@@ -4,4 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
paste = "1.0.15"
serde = { version = "1.0.228", features = ["derive"] }

View File

@@ -1 +1,70 @@
// TODO
#![allow(unused)]
use crate::types::{
CharacterDetail, CharacterFull, CharacterMini, CharacterSearch, Id, ItemMini, ItemSearch,
LightconeDetail, LightconeFull, LightconeMini, LightconeSearch, RelicMini, RelicSearch,
};
use paste::paste;
use std::sync::OnceLock;
macro_rules! define_data_store {
($base:ident, full) => {
paste::item! {
pub static [<$base:upper _DETAIL>]: OnceLock<[<$base Detail>]> = OnceLock::new();
pub static [<$base:upper _SEARCH>]: OnceLock<[<$base Search>]> = OnceLock::new();
pub fn [<get_ $base:lower _detail>]() -> &'static [<$base Detail>] {
[<$base:upper _DETAIL>]
.get()
.expect(concat!(stringify!([<$base:upper _DETAIL>]), " not initialized"))
}
pub fn [<get_ $base:lower _search>]() -> &'static [<$base Search>] {
[<$base:upper _SEARCH>]
.get()
.expect(concat!(stringify!([<$base:upper _SEARCH>]), " not initialized"))
}
pub fn [<get_ $base:lower full_by_id>](id: &Id) -> Option<&[<$base Full>]> {
[<get_ $base:lower _detail>]().0.get(id)
}
pub fn [<get_ $base:lower _mini_by_id>](id: &Id) -> Option<&[<$base Mini>]> {
[<get_ $base:lower _search>]().0.get(id)
}
pub fn [<set_ $base:lower _detail>](data: [<$base Detail>]) -> Result<(), [<$base Detail>]> {
[<$base:upper _DETAIL>].set(data)
}
pub fn [<set_ $base:lower _search>](data: [<$base Search>]) -> Result<(), [<$base Search>]> {
[<$base:upper _SEARCH>].set(data)
}
}
};
($base:ident, mini) => {
paste::item! {
pub static [<$base:upper _SEARCH>]: OnceLock<[<$base Search>]> = OnceLock::new();
pub fn [<get_ $base:lower _search>]() -> &'static [<$base Search>] {
[<$base:upper _SEARCH>]
.get()
.expect(concat!(stringify!([<$base:upper _SEARCH>]), " not initialized"))
}
pub fn [<get_ $base:lower _mini_by_id>](id: &Id) -> Option<&[<$base Mini>]> {
[<get_ $base:lower _search>]().0.get(id)
}
pub fn [<set_ $base:lower _search>](data: [<$base Search>]) -> Result<(), [<$base Search>]> {
[<$base:upper _SEARCH>].set(data)
}
}
};
}
define_data_store!(Character, full);
define_data_store!(Lightcone, full);
define_data_store!(Relic, mini);
define_data_store!(Item, mini);

View File

@@ -10,16 +10,7 @@ pub struct RelicMini {
pub name: String,
pub r#type: RelicType,
pub img_url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelicDetail(pub HashMap<Id, RelicFull>);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelicFull {
pub name: String,
pub r#type: RelicType,
pub img_url: Uri,
// lets just display these in the search page
pub two_pc: Desc,
pub four_pc: Option<Desc>,
}