aboutsummaryrefslogtreecommitdiff
path: root/src/kvstore.rs
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2021-11-09 12:46:43 +0100
committerDaniel Schadt <kingdread@gmx.de>2021-11-09 12:50:33 +0100
commitd21c5dc9c53b02620fce916ffc1a2695e9d3f698 (patch)
treefb17ba6ee4e0a1ed532ab6e570418abff5264616 /src/kvstore.rs
parent919796ec954414ae16f113896bdf212381c96437 (diff)
downloadmodderbaas-d21c5dc9c53b02620fce916ffc1a2695e9d3f698.tar.gz
modderbaas-d21c5dc9c53b02620fce916ffc1a2695e9d3f698.tar.bz2
modderbaas-d21c5dc9c53b02620fce916ffc1a2695e9d3f698.zip
Separate the binary and library
This uses the workspace feature of cargo, with the benefit that 1) We can more cleanly group the binary (user facing) code from the library 2) We can have dependencies that apply to the binary only The first point could've been achieved without workspaces (Cargo supports both binaries and libraries in a crate), but the second point is what really makes this approach a lot better.
Diffstat (limited to 'src/kvstore.rs')
-rw-r--r--src/kvstore.rs49
1 files changed, 0 insertions, 49 deletions
diff --git a/src/kvstore.rs b/src/kvstore.rs
deleted file mode 100644
index 006bb94..0000000
--- a/src/kvstore.rs
+++ /dev/null
@@ -1,49 +0,0 @@
-//! Support module for writing `key=value` stores.
-//!
-//! These files are used by minetest mods (`mod.conf`), games (`game.conf`) and worlds
-//! (`world.mt`).
-//!
-//! Key-Value-Stores (KVStores) are represented by a [`std::collections::HashMap`] on the Rust
-//! side.
-use std::{collections::HashMap, fs, io::Write, path::Path};
-
-use super::error::Result;
-
-/// Read the given file as a KVStore.
-pub fn read<P: AsRef<Path>>(path: P) -> Result<HashMap<String, String>> {
- read_inner(path.as_ref())
-}
-
-fn read_inner(path: &Path) -> Result<HashMap<String, String>> {
- let content = fs::read_to_string(path)?;
-
- Ok(content
- .lines()
- .map(|line| line.splitn(2, '=').map(str::trim).collect::<Vec<_>>())
- .map(|v| (v[0].into(), v[1].into()))
- .collect())
-}
-
-/// Write the given KVStore back to the file.
-///
-/// The order of the keys is guaranteed to be the following:
-///
-/// 1. All options that *don't* start with `"load_mod_"` are saved in alphabetical order.
-/// 2. All remaining options are saved in alphabetical order.
-///
-/// Note that this function will **override** existing files!
-pub fn write<P: AsRef<Path>>(data: &HashMap<String, String>, path: P) -> Result<()> {
- write_inner(data, path.as_ref())
-}
-
-fn write_inner(data: &HashMap<String, String>, path: &Path) -> Result<()> {
- let mut items = data.iter().collect::<Vec<_>>();
- items.sort_by_key(|i| (if i.0.starts_with("load_mod_") { 1 } else { 0 }, i.0));
-
- let mut output = fs::File::create(path)?;
- for (key, value) in items {
- writeln!(output, "{} = {}", key, value)?;
- }
-
- Ok(())
-}