File size: 3,305 Bytes
7178525
 
 
 
 
 
 
fe2a7bb
7178525
 
 
 
 
42327ea
 
fe2a7bb
 
 
 
42327ea
7178525
42327ea
 
7178525
 
 
42327ea
 
fe2a7bb
d654e03
7178525
42327ea
 
7178525
42327ea
 
 
7178525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42327ea
 
 
 
 
7178525
 
 
 
 
 
 
 
42327ea
 
7178525
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42327ea
7178525
 
42327ea
7178525
42327ea
7178525
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! Хранилище спарсенных схематик (в памяти процесса).

use std::collections::HashMap;
use std::sync::{Arc, RwLock};

use uuid::Uuid;

use crate::encoder::{self, EncodeError, Groups};

#[derive(Debug)]
pub struct Schematic {
    /// Число уникальных блоков.
    pub unique_blocks: usize,
    /// Число уникальных NBT-записей (без «нет NBT»).
    pub unique_nbt: usize,
    /// Список блоков, нарезанный так, что JSON каждой части ≤ 24 000 символов.
    pub block_parts: Vec<Vec<String>>,
    /// Список NBT, нарезанный так же.
    pub nbt_parts: Vec<Vec<String>>,
    /// Весь текст схематики (ячейки по 28 символов).
    pub text: String,
    /// Конец каждой части текста (смещение в байтах).
    pub part_ends: Vec<usize>,
}

impl Schematic {
    pub fn build(groups: &Groups) -> Result<Self, EncodeError> {
        let e = encoder::encode(groups)?;
        let block_parts = encoder::split_list(&e.blocks, encoder::DICT_PART_JSON_MAX)?;
        let nbt_parts = encoder::split_nbt_list(&e.nbt, encoder::DICT_PART_JSON_MAX)?;
        Ok(Schematic {
            unique_blocks: e.blocks.len(),
            unique_nbt: e.nbt.len(),
            block_parts,
            nbt_parts,
            text: e.text,
            part_ends: e.part_ends,
        })
    }
}

#[derive(Clone, Default)]
pub struct Store {
    inner: Arc<RwLock<HashMap<Uuid, Arc<Schematic>>>>,
}

impl Store {
    pub fn insert(&self, schematic: Schematic) -> Uuid {
        let id = Uuid::new_v4();
        self.inner
            .write()
            .unwrap_or_else(|e| e.into_inner())
            .insert(id, Arc::new(schematic));
        id
    }

    pub fn get(&self, id: &Uuid) -> Option<Arc<Schematic>> {
        self.inner
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .get(id)
            .cloned()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sample() -> Schematic {
        let mut cells = Vec::new();
        encoder::push_position(&mut cells, 0, 0, 0).unwrap();
        let mut groups = Groups::new();
        groups.insert(("minecraft:stone".to_string(), String::new()), cells);
        Schematic::build(&groups).unwrap()
    }

    #[test]
    fn insert_and_get() {
        let store = Store::default();
        let id = store.insert(sample());
        let got = store.get(&id).expect("должна найтись");
        assert_eq!(got.unique_blocks, 1);
        assert_eq!(got.unique_nbt, 0);
        assert_eq!(got.part_ends.len(), 1);
    }

    #[test]
    fn missing_uuid() {
        let store = Store::default();
        assert!(store.get(&Uuid::new_v4()).is_none());
    }

    #[test]
    fn ids_are_unique() {
        let store = Store::default();
        assert_ne!(store.insert(sample()), store.insert(sample()));
    }

    #[test]
    fn build_empty() {
        let s = Schematic::build(&Groups::new()).unwrap();
        assert_eq!(s.unique_blocks, 0);
        assert!(s.block_parts.is_empty());
        assert!(s.nbt_parts.is_empty());
        assert!(s.text.is_empty());
        assert!(s.part_ends.is_empty());
    }
}