Skip to main content

yog_event/planes/system/
storage.rs

1//! A mod's declared state in its stores (`docs/design/storage.md`): its
2//! objects' records — a type's saved members, as Yog Data — read when their
3//! owner loads, written as the world saves. Keyed by the mod: each mod's
4//! native answers its own, over `yog-storage`.
5
6use yog_data::Schema;
7use yog_data::with::Raw;
8
9/// Which of the mod's stores (the mod: the request's key): a scope's
10/// (`yog_ids::storage`'s words: `world`, `player`, `chunk`, …) and its
11/// instance (a uuid, a dimension, a chunk's `<dim>_<cx>_<cz>`; empty for
12/// `game` and `world`).
13#[derive(Debug, Clone, PartialEq, Eq, Schema)]
14pub struct Where {
15    pub scope: String,
16    pub instance: String,
17}
18
19/// The record of type `ty` (its full Yog Script name) of `owner` (a block's
20/// position in its chunk; empty when the store is the owner's own).
21#[derive(Debug, Clone, Schema)]
22pub struct Read {
23    pub mod_id: String,
24    pub at: Where,
25    pub owner: String,
26    pub ty: String,
27}
28
29/// A record's bytes.
30#[derive(Debug, Clone, Default, Schema)]
31pub struct Record {
32    #[data(with = "Raw")]
33    pub bytes: Vec<u8>,
34}
35
36/// Writes a record: kept as its bytes, written as the world saves.
37#[derive(Debug, Clone, Schema)]
38pub struct Write {
39    pub mod_id: String,
40    pub at: Where,
41    pub owner: String,
42    pub ty: String,
43    #[data(with = "Raw")]
44    pub bytes: Vec<u8>,
45}
46
47/// Removes `owner`'s records: every type's, or `ty`'s alone.
48#[derive(Debug, Clone, Schema)]
49pub struct Remove {
50    pub mod_id: String,
51    pub at: Where,
52    pub owner: String,
53    pub ty: Option<String>,
54}
55
56/// The owners with records in a store (a chunk's positions).
57#[derive(Debug, Clone, Schema)]
58pub struct Owners {
59    pub mod_id: String,
60    pub at: Where,
61}
62
63/// A `synced` value as it goes to a client (on `yog_ids::yog::SYNCED`):
64/// what it's of (`kind`: a Yog Script class, `mod:Class`; a Rust mod's id),
65/// its owner (a player, an entity, a block; empty for the world's), its
66/// key and its bytes.
67#[derive(Debug, Clone, PartialEq, Eq, Schema)]
68pub struct SyncedRow {
69    pub kind: String,
70    pub owner: String,
71    pub key: String,
72    #[data(with = "Raw")]
73    pub bytes: Vec<u8>,
74}
75
76/// A record's key in its store: its owner's, then its type's.
77#[must_use]
78pub fn record_key(owner: &str, ty: &str) -> String {
79    if owner.is_empty() {
80        ty.to_owned()
81    } else {
82        format!("{owner}{}{ty}", yog_ids::sep::PATH)
83    }
84}
85
86/// The prefix of every record of `owner`.
87#[must_use]
88pub fn owner_prefix(owner: &str) -> String {
89    format!("{owner}{}", yog_ids::sep::PATH)
90}