Spaces:
Sleeping
Sleeping
Delete store.rs
Browse files
store.rs
DELETED
|
@@ -1,109 +0,0 @@
|
|
| 1 |
-
//! Хранилище спарсенных схематик (в памяти процесса).
|
| 2 |
-
|
| 3 |
-
use std::collections::HashMap;
|
| 4 |
-
use std::sync::{Arc, RwLock};
|
| 5 |
-
|
| 6 |
-
use uuid::Uuid;
|
| 7 |
-
|
| 8 |
-
use crate::encoder::{self, EncodeError, Groups};
|
| 9 |
-
|
| 10 |
-
#[derive(Debug)]
|
| 11 |
-
pub struct Schematic {
|
| 12 |
-
/// Число уникальных блоков.
|
| 13 |
-
pub unique_blocks: usize,
|
| 14 |
-
/// Число уникальных NBT-записей (без «нет NBT»).
|
| 15 |
-
pub unique_nbt: usize,
|
| 16 |
-
/// Список блоков, нарезанный так, что JSON каждой части ≤ 24 000 символов.
|
| 17 |
-
pub block_parts: Vec<Vec<String>>,
|
| 18 |
-
/// Список NBT, нарезанный так же.
|
| 19 |
-
pub nbt_parts: Vec<Vec<String>>,
|
| 20 |
-
/// Весь текст схематики (ячейки по 28 символов).
|
| 21 |
-
pub text: String,
|
| 22 |
-
/// Конец каждой части текста (смещение в байтах).
|
| 23 |
-
pub part_ends: Vec<usize>,
|
| 24 |
-
}
|
| 25 |
-
|
| 26 |
-
impl Schematic {
|
| 27 |
-
pub fn build(groups: &Groups) -> Result<Self, EncodeError> {
|
| 28 |
-
let e = encoder::encode(groups)?;
|
| 29 |
-
let block_parts = encoder::split_list(&e.blocks, encoder::DICT_PART_JSON_MAX)?;
|
| 30 |
-
let nbt_parts = encoder::split_nbt_list(&e.nbt, encoder::DICT_PART_JSON_MAX)?;
|
| 31 |
-
Ok(Schematic {
|
| 32 |
-
unique_blocks: e.blocks.len(),
|
| 33 |
-
unique_nbt: e.nbt.len(),
|
| 34 |
-
block_parts,
|
| 35 |
-
nbt_parts,
|
| 36 |
-
text: e.text,
|
| 37 |
-
part_ends: e.part_ends,
|
| 38 |
-
})
|
| 39 |
-
}
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
#[derive(Clone, Default)]
|
| 43 |
-
pub struct Store {
|
| 44 |
-
inner: Arc<RwLock<HashMap<Uuid, Arc<Schematic>>>>,
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
impl Store {
|
| 48 |
-
pub fn insert(&self, schematic: Schematic) -> Uuid {
|
| 49 |
-
let id = Uuid::new_v4();
|
| 50 |
-
self.inner
|
| 51 |
-
.write()
|
| 52 |
-
.unwrap_or_else(|e| e.into_inner())
|
| 53 |
-
.insert(id, Arc::new(schematic));
|
| 54 |
-
id
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
pub fn get(&self, id: &Uuid) -> Option<Arc<Schematic>> {
|
| 58 |
-
self.inner
|
| 59 |
-
.read()
|
| 60 |
-
.unwrap_or_else(|e| e.into_inner())
|
| 61 |
-
.get(id)
|
| 62 |
-
.cloned()
|
| 63 |
-
}
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
#[cfg(test)]
|
| 67 |
-
mod tests {
|
| 68 |
-
use super::*;
|
| 69 |
-
|
| 70 |
-
fn sample() -> Schematic {
|
| 71 |
-
let mut cells = Vec::new();
|
| 72 |
-
encoder::push_position(&mut cells, 0, 0, 0).unwrap();
|
| 73 |
-
let mut groups = Groups::new();
|
| 74 |
-
groups.insert(("minecraft:stone".to_string(), String::new()), cells);
|
| 75 |
-
Schematic::build(&groups).unwrap()
|
| 76 |
-
}
|
| 77 |
-
|
| 78 |
-
#[test]
|
| 79 |
-
fn insert_and_get() {
|
| 80 |
-
let store = Store::default();
|
| 81 |
-
let id = store.insert(sample());
|
| 82 |
-
let got = store.get(&id).expect("должна найтись");
|
| 83 |
-
assert_eq!(got.unique_blocks, 1);
|
| 84 |
-
assert_eq!(got.unique_nbt, 0);
|
| 85 |
-
assert_eq!(got.part_ends.len(), 1);
|
| 86 |
-
}
|
| 87 |
-
|
| 88 |
-
#[test]
|
| 89 |
-
fn missing_uuid() {
|
| 90 |
-
let store = Store::default();
|
| 91 |
-
assert!(store.get(&Uuid::new_v4()).is_none());
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
#[test]
|
| 95 |
-
fn ids_are_unique() {
|
| 96 |
-
let store = Store::default();
|
| 97 |
-
assert_ne!(store.insert(sample()), store.insert(sample()));
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
#[test]
|
| 101 |
-
fn build_empty() {
|
| 102 |
-
let s = Schematic::build(&Groups::new()).unwrap();
|
| 103 |
-
assert_eq!(s.unique_blocks, 0);
|
| 104 |
-
assert!(s.block_parts.is_empty());
|
| 105 |
-
assert!(s.nbt_parts.is_empty());
|
| 106 |
-
assert!(s.text.is_empty());
|
| 107 |
-
assert!(s.part_ends.is_empty());
|
| 108 |
-
}
|
| 109 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|