Compare commits

...

10 Commits

9 changed files with 679 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,7 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

442
Cargo.lock generated Normal file
View File

@ -0,0 +1,442 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "ahash"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if",
"getrandom",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "bson"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d43b38e074cc0de2957f10947e376a1d88b9c4dbab340b590800cc1b2e066b2"
dependencies = [
"ahash",
"base64",
"bitvec",
"hex",
"indexmap",
"js-sys",
"once_cell",
"rand",
"serde",
"serde_bytes",
"serde_json",
"time",
"uuid",
]
[[package]]
name = "bumpalo"
version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "deranged"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
dependencies = [
"powerfmt",
]
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "getrandom"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "indexmap"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "js-sys"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "metalize"
version = "0.1.0"
dependencies = [
"bson",
"serde",
"serde_json",
"uuid",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "once_cell"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "serde"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_bytes"
version = "0.11.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b8497c313fd43ab992087548117643f6fcd935cbf36f176ffda0aacf9591734"
dependencies = [
"serde",
]
[[package]]
name = "serde_derive"
version = "1.0.197"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.115"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12dc5c46daa8e9fdf4f5e71b6cf9a53f2487da0e86e55808e2d35539666497dd"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "2.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44cfb93f38070beee36b3fef7d4f5a16f27751d94b187b666a5cc5e9b0d30687"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "time"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749"
dependencies = [
"deranged",
"itoa",
"num-conv",
"powerfmt",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]]
name = "time-macros"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "uuid"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
dependencies = [
"getrandom",
"serde",
]
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]]
name = "wyz"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
dependencies = [
"tap",
]
[[package]]
name = "zerocopy"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "metalize"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bson = "2.10.0"
serde = "1.0.197"
serde_json = "1.0.115"
uuid = "1.8.0"

BIN
logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 247 KiB

57
src/collection.rs Normal file
View File

@ -0,0 +1,57 @@
use serde::Serialize;
use serde::de::DeserializeOwned;
use bson::Document;
use std::fs::File;
use std::io::{BufReader, Seek, Write};
use std::error::Error;
pub struct Collection<T>
{
pub entries: Vec<T>
}
impl<T> Collection<T>{
fn get(&self) -> Option<&T>{
self.entries.get(0)
}
fn new( entries: Vec<T>) -> Collection<T>{
Collection{
entries:entries
}
}
}
impl<T> Collection<T>
where
T : Serialize
{
pub fn save<W>(&self, mut writer : W) -> Result<(), Box<dyn Error>>
where W : Write {
for a in self.entries.iter(){
let d = bson::to_document(a)?;
d.to_writer(&mut writer)?;
}
Ok(())
}
}
impl<T> Collection<T>
where
T: DeserializeOwned
{
pub fn load(file: File) -> Result<Collection<T>, Box<dyn Error>>{
let n = file.metadata()?.len();
let mut reader = BufReader::new(file);
let mut current_position = reader.stream_position()?;
let mut entries = Vec::new();
while current_position < n{
let d = Document::from_reader(&mut reader)?;
let h: T = bson::from_document(d)?;
entries.push(h);
current_position = reader.stream_position()?;
}
Ok(Collection::new( entries))
}
}

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod collection;
pub mod table;

28
src/main.rs Normal file
View File

@ -0,0 +1,28 @@
use std::fs::File;
use metalize::table::Table;
use serde::{Deserialize, Serialize};
use metalize::collection::Collection;
#[derive(Serialize, Deserialize)]
struct Potato {
bob: f64
}
fn main() {
let mut peeps: Collection<Potato> = Collection{entries: Vec::new()};
peeps.entries.push(Potato{bob:12.0});
peeps.entries.push(Potato{bob:42.0});
let mut table = Table::init().unwrap();
table.insert_collection(peeps, String::from("Potato")).unwrap();
// let mut table = Table::init().unwrap();
// table.addToCollection(1, Potato{bob: 66.0}).unwrap();
println!("blah");
}

130
src/table.rs Normal file
View File

@ -0,0 +1,130 @@
use std::{fs::{File,remove_file}, io::{Seek, BufReader}};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::error::Error;
use uuid::Uuid;
use std::path::Path;
use serde_json;
use crate::collection::Collection;
#[derive(Serialize, Deserialize, Clone)]
struct TableRow {
id: usize,
key: Uuid,
num_entries: usize,
path: String,
file_pos: u64,
name: String
}
impl TableRow
{
fn new<T:Serialize>(id: usize, key: Uuid, entry: Collection<T>, file_pos: u64, name: String) -> Result<TableRow, Box<dyn Error>>{
let path = format!("{}.bson", key);
let file = File::create(&path)?;
entry.save(file)?;
Ok(
TableRow { id: id, key, num_entries: entry.entries.len(), path: path, file_pos: file_pos, name: name }
)
}
}
pub struct Table{
collections: std::collections::HashMap<String, TableRow>
}
impl Table {
fn new() -> Table{
Table{ collections: std::collections::HashMap::new()}
}
pub fn init() -> Result<Table, Box<dyn Error>>{
let mut table = Table::new();
// check for existing table entries
let path = Path::new("metalize.table");
if path.exists(){
let file = File::open(path)?;
let n = file.metadata()?.len();
let mut file = BufReader::new(file);
while file.stream_position()? < n {
let row : TableRow = serde_json::from_reader(&mut file)?;
let name = row.name.clone();
table.collections.insert(name, row);
}
}
Ok(table)
}
fn rewrite_table(&mut self) -> Result<(), Box<dyn Error>>{
remove_file("metalize.table")?;
let file = File::create("metalize.table")?;
for coll in self.collections.iter(){
let x = serde_json::to_value(coll.1)?;
serde_json::to_writer(&file, &x)?;
}
Ok(())
}
pub fn insert_collection<T:Serialize>(&mut self, entry: Collection<T>, name: String) -> Result<(), Box<dyn Error>> {
let path = Path::new("metalize.table");
let y = if path.exists() {
File::open(path)?
} else {
File::create(path)?
};
let new_pos = y.metadata()?.len();
let table_row = TableRow::new(
self.collections.len()+1,
Uuid::new_v4(),
entry,
new_pos,
name.clone()
)?;
let x = serde_json::to_value(&table_row)?;
serde_json::to_writer(&y, &x)?;
self.collections.insert(name, table_row);
Ok(())
}
pub fn add_to_collection<T:Serialize + DeserializeOwned>(&mut self, name: String, object: T) -> Result<(), Box<dyn Error>>{
// TODO: this currently wipes out files and rewrites the entirety of them. Update to only change the sections that need to change.
let selected_row = self.get_row(name).expect("No collection with that name in table.");
let mut selected_collection: Collection<T> = Collection::load(File::open(&selected_row.path)?)?;
remove_file(&selected_row.path)?;
selected_collection.entries.push(object);
selected_row.num_entries = selected_collection.entries.len();
selected_collection.save(File::create(&selected_row.path)?)?;
self.rewrite_table()?;
Ok(())
}
pub fn delete_collection(&mut self, name:String) -> Result<(), Box<dyn Error>>{
let selected_row = self.get_row(name.clone()).expect("No collection with that name in table");
remove_file(&selected_row.path)?;
self.collections.remove(&name);
Ok(())
}
pub fn update<T:Serialize>(&mut self, name: String, entry: Collection<T>)-> Result<(), Box<dyn Error>>{
// TODO: this currently wipes out files and rewrites the entirety of them. Update to only change the sections that need to change.
let selected_row = self.get_row(name).expect("No collection with that name in table.");
remove_file(&selected_row.path)?;
selected_row.num_entries = entry.entries.len();
entry.save(File::create(&selected_row.path)?)?;
self.rewrite_table()?;
Ok(())
}
fn get_row(&mut self, name: String) -> Result<&mut TableRow, Box<dyn Error>>{
let row = self.collections.get_mut(&name).expect("No collection with that name in table.");
Ok(row)
}
pub fn get<T:DeserializeOwned>(&mut self, name:String)-> Result<Collection<T>, Box<dyn Error>>{
let table_row = self.get_row(name).expect("No collection with that name in table.");
let file = File::open(&table_row.path)?;
Collection::load(file)
}
}