use crate::SceneView;
use gantz_ca::{
CommitAddr, Liveness, MergePolicy, Name, Registry, SectionDecl, section_get, section_insert,
section_iter, section_remove,
};
pub struct Descriptions;
pub struct Demos;
pub struct Views;
pub const DESCRIPTIONS_ID: &str = "gantz.description";
pub const DEMOS_ID: &str = "egui.demo";
pub const VIEWS_ID: &str = "egui.view";
impl SectionDecl for Descriptions {
const ID: &'static str = DESCRIPTIONS_ID;
const POLICY: MergePolicy = MergePolicy::KeepExisting;
const LIVENESS: Liveness = Liveness::WithName;
type Key = Name;
type Value = String;
}
impl SectionDecl for Demos {
const ID: &'static str = DEMOS_ID;
const POLICY: MergePolicy = MergePolicy::KeepExisting;
const LIVENESS: Liveness = Liveness::WithName;
type Key = Name;
type Value = String;
}
impl SectionDecl for Views {
const ID: &'static str = VIEWS_ID;
const POLICY: MergePolicy = MergePolicy::KeepExisting;
const LIVENESS: Liveness = Liveness::WithCommit;
type Key = CommitAddr;
type Value = SceneView;
}
pub fn description(reg: &Registry, name: &Name) -> Option<String> {
section_get::<Descriptions>(reg, name)
}
pub fn set_description(reg: &mut Registry, name: Name, description: String) {
if description.is_empty() {
section_remove::<Descriptions>(reg, &name);
} else {
section_insert::<Descriptions>(reg, name, &description)
.expect("a `String` always encodes as a datum");
}
}
pub fn descriptions(reg: &Registry) -> impl Iterator<Item = (Name, String)> + '_ {
section_iter::<Descriptions>(reg)
}
pub fn demo(reg: &Registry, name: &Name) -> Option<String> {
section_get::<Demos>(reg, name)
}
pub fn set_demo(reg: &mut Registry, name: Name, demo: String) {
section_insert::<Demos>(reg, name, &demo).expect("a `String` always encodes as a datum");
}
pub fn remove_demo(reg: &mut Registry, name: &Name) -> Option<String> {
section_remove::<Demos>(reg, name)
}
pub fn demos(reg: &Registry) -> impl Iterator<Item = (Name, String)> + '_ {
section_iter::<Demos>(reg)
}
pub fn view(reg: &Registry, ca: &CommitAddr) -> Option<SceneView> {
section_get::<Views>(reg, ca)
}
pub fn set_view(reg: &mut Registry, ca: CommitAddr, view: &SceneView) {
if let Err(e) = section_insert::<Views>(reg, ca, view) {
log::error!("failed to encode scene view for {ca}: {e}");
}
}
pub fn remove_view(reg: &mut Registry, ca: &CommitAddr) -> Option<SceneView> {
section_remove::<Views>(reg, ca)
}
pub fn views(reg: &Registry) -> impl Iterator<Item = (CommitAddr, SceneView)> + '_ {
section_iter::<Views>(reg)
}