Skip to main content

yog_storage/
scope.rs

1//! What a store belongs to, and so where its file is.
2
3use std::path::{Path, PathBuf};
4
5/// What a store belongs to — under a root: the game's folder for
6/// [`Scope::Game`], the world's for the rest (see [the crate docs](crate)).
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum Scope<'a> {
9    /// The install, whatever world (settings, what a player made to keep).
10    Game,
11    /// The world as a whole.
12    World,
13    /// One dimension of the world, by its id (`minecraft:overworld`).
14    Dimension(&'a str),
15    /// One player, by uuid.
16    Player(&'a str),
17    /// One entity, by uuid.
18    Entity(&'a str),
19    /// One chunk: its dimension and coordinates.
20    Chunk(&'a str, i32, i32),
21}
22
23use yog_ids::storage::{CHUNK, DIMENSION, DIR, ENTITY, GAME, PLAYER, SEP, WORLD};
24
25/// A store's file.
26const STORE_EXT: &str = yog_ids::Format::Data.ext();
27
28impl Scope<'_> {
29    /// The file of `mod_id`'s store of this scope under `root`.
30    pub fn path(self, root: &Path, mod_id: &str) -> PathBuf {
31        let base = root.join(DIR).join(file_safe(mod_id));
32        let (dir, name) = match self {
33            Scope::Game => (None, GAME.to_owned()),
34            Scope::World => (None, WORLD.to_owned()),
35            Scope::Dimension(dim) => (Some(DIMENSION), file_safe(dim)),
36            Scope::Player(uuid) => (Some(PLAYER), file_safe(uuid)),
37            Scope::Entity(uuid) => (Some(ENTITY), file_safe(uuid)),
38            Scope::Chunk(dim, x, z) => (Some(CHUNK), format!("{}{SEP}{x}{SEP}{z}", file_safe(dim))),
39        };
40        let file = format!("{name}{}{STORE_EXT}", yog_ids::sep::DOT);
41        match dir {
42            Some(dir) => base.join(dir).join(file),
43            None => base.join(file),
44        }
45    }
46}
47
48/// `name` (`ns:path/x`) as one file name: its separators replaced.
49fn file_safe(name: &str) -> String {
50    name.replace(yog_ids::sep::NAMESPACE, SEP)
51        .replace(yog_ids::sep::PATH, SEP)
52}