Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ziteh committed Jul 20, 2024
2 parents 5a694e2 + 6b09e1c commit f3fce25
Show file tree
Hide file tree
Showing 34 changed files with 738 additions and 357 deletions.
2 changes: 1 addition & 1 deletion elebox-cli/src/category_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn category_cmd(db: &dyn elebox_core::Database, cmd: &CategoryCommand) {
// };
}
Some(CategorySubCommand::Export(args)) => {
let _ = manager.export_csv(&args.path);
let _ = manager.export(&args.path);
}
None => {
println!("List part category");
Expand Down
4 changes: 2 additions & 2 deletions elebox-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ fn main() {
EntityType::Init => db.init(),
EntityType::Part(cmd) => part_cmd(&db, cmd),
EntityType::Category(cmd) => category_cmd(&db, cmd),
EntityType::Export(args) => elebox_core::export_csv(&db, &args.path),
EntityType::Export(args) => elebox_core::export(&db, &args.path),
EntityType::Import(args) => {
let _ = elebox_core::import_csv(&args.path);
let _ = elebox_core::import(&args.path);
}
};
}
4 changes: 2 additions & 2 deletions elebox-cli/src/part_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ pub fn part_cmd(db: &dyn elebox_core::Database, cmd: &PartCommand) {
}
}
PartSubCommand::Export(args) => {
let _ = manager.export_csv(&args.path);
let _ = manager.export(&args.path);
}
PartSubCommand::Import(args) => {
let _ = manager.import_csv(&args.path);
let _ = manager.import(&args.path);
}
},
None => {
Expand Down
3 changes: 2 additions & 1 deletion elebox-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ edition = "2021"
jammdb = "0.11.0"
serde = { version = "1.0", features = ["derive"] }
rmp-serde = "1.1.2"
uuid = { version = "1.5.0", features = ["v4", "fast-rng", "macro-diagnostics"] }
uuid = { version = "1.5.0", features = ["v7", "fast-rng", "macro-diagnostics"] }
csv = "1.3.0"
serde_yaml = "0.9.34"

[profile.release]
lto = true
Expand Down
50 changes: 50 additions & 0 deletions elebox-core/src/backup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::{
CategoryManager, Database, JammDatabase, ManufacturerManager, PackageManager, PartManager,
};
use std::path::PathBuf;

pub fn export(db: &dyn Database, path: &str) {
let filename_part = format!("{}{}", path, "elebox_export_parts.yaml");
let _ = PartManager::new(db).export(&filename_part);

let filename_cat = format!("{}{}", path, "elebox_export_categories.yaml");
let _ = CategoryManager::new(db).export(&filename_cat);

let filename_pkg = format!("{}{}", path, "elebox_export_packages.yaml");
let _ = PackageManager::new(db).export(&filename_pkg);

let filename_mfr = format!("{}{}", path, "elebox_export_mfrs.yaml");
let _ = ManufacturerManager::new(db).export(&filename_mfr);
}

pub fn import(path: &str) -> Result<(), String> {
let db_path = format!("{}{}", path, "import_elebox.db");
let db = JammDatabase::new(&db_path);
db.init();

let filename_mfr = format!("{}{}", path, "elebox_export_mfrs.yaml");
let res = ManufacturerManager::new(&db).import(&filename_mfr);
if res.is_err() {
return Err("Part".to_string());
}

let filename_pkg = format!("{}{}", path, "elebox_export_packages.yaml");
let res = PackageManager::new(&db).import(&filename_pkg);
if res.is_err() {
return Err("Part".to_string());
}

let filename_cat = format!("{}{}", path, "elebox_export_categories.yaml");
let res = CategoryManager::new(&db).import(&filename_cat);
if res.is_err() {
return Err("Part".to_string());
}

let filename_part = format!("{}{}", path, "elebox_export_parts.yaml");
let res = PartManager::new(&db).import(&filename_part);
if res.is_err() {
return Err("Part".to_string());
}

Ok(())
}
31 changes: 24 additions & 7 deletions elebox-core/src/category.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{csv::*, db::*, errors::EleboxError};
use crate::{csv::*, db::*, errors::EleboxError, yaml::*};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Debug};

Expand Down Expand Up @@ -87,6 +87,13 @@ impl<'a> CategoryManager<'a> {
));
}

if ori_name != &new_category.name && self.db.get_category_id(&new_category.name).is_some() {
return Err(EleboxError::AlreadyExists(
"Category".to_string(),
new_category.name.clone(),
));
}

// Normalize
let mut cat = Category {
name: new_category.name.clone(),
Expand Down Expand Up @@ -158,26 +165,36 @@ impl<'a> CategoryManager<'a> {
Ok(())
}

pub fn export_csv(&self, filename: &str) -> Result<(), ()> {
pub fn export(&self, filename: &str) -> Result<(), ()> {
let cats = self.list();
let res = write_csv(filename, cats, None);
let res = write_yaml(filename, cats);
return res;
}

pub fn import_csv(&self, filename: &str) -> Result<(), ()> {
let res_parts = read_csv(filename, None);
pub fn import(&self, filename: &str) -> Result<(), ()> {
let res_parts = read_yaml(filename);
if res_parts.is_err() {
return Err(());
}

let cats: Vec<Category> = res_parts.unwrap();
for cat in cats {
let _ = self.add(&cat);
for cat in &cats {
self.add_recursion(cat, &cats);
}

Ok(())
}

fn add_recursion(&self, category: &Category, cats: &[Category]) -> Result<(), EleboxError> {
if let Some(parent_name) = &category.parent {
if let Some(parent_cat) = cats.iter().find(|c| c.name == *parent_name) {
self.add_recursion(parent_cat, cats);
}
}

self.add(category)
}

fn to_node(&self, name: String, map: &HashMap<String, Vec<String>>) -> TreeNode {
let mut children = vec![];

Expand Down
50 changes: 4 additions & 46 deletions elebox-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod backup;
mod category;
mod csv;
mod db;
Expand All @@ -6,6 +7,7 @@ mod errors;
mod manufacturer;
mod package;
mod part;
mod yaml;

pub use category::*;
pub use db::CustomField;
Expand All @@ -17,50 +19,6 @@ pub use manufacturer::*;
pub use package::*;
pub use part::*;

pub use backup::export;
pub use backup::import;
pub use default_db::create_default_db;

pub fn export_csv(db: &dyn Database, path: &str) {
let filename_part = format!("{}{}", path, "elebox_export_parts.tsv");
let _ = PartManager::new(db).export_csv(&filename_part);

let filename_cat = format!("{}{}", path, "elebox_export_categories.tsv");
let _ = CategoryManager::new(db).export_csv(&filename_cat);

let filename_pkg = format!("{}{}", path, "elebox_export_packages.tsv");
let _ = PackageManager::new(db).export_csv(&filename_pkg);

let filename_mfr = format!("{}{}", path, "elebox_export_mfrs.tsv");
let _ = ManufacturerManager::new(db).export_csv(&filename_mfr);
}

pub fn import_csv(path: &str) -> Result<(), String> {
let db_path = format!("{}{}", path, "import_elebox.db");
let db = JammDatabase::new(&db_path);
db.init();

let filename_mfr = format!("{}{}", path, "elebox_export_mfrs.tsv");
let res = ManufacturerManager::new(&db).import_csv(&filename_mfr);
if res.is_err() {
return Err("Part".to_string());
}

let filename_pkg = format!("{}{}", path, "elebox_export_packages.tsv");
let res = PackageManager::new(&db).import_csv(&filename_pkg);
if res.is_err() {
return Err("Part".to_string());
}

let filename_cat = format!("{}{}", path, "elebox_export_categories.tsv");
let res = CategoryManager::new(&db).import_csv(&filename_cat);
if res.is_err() {
return Err("Part".to_string());
}

let filename_part = format!("{}{}", path, "elebox_export_parts.tsv");
let res = PartManager::new(&db).import_csv(&filename_part);
if res.is_err() {
return Err("Part".to_string());
}

Ok(())
}
17 changes: 12 additions & 5 deletions elebox-core/src/manufacturer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{csv::*, db::*, errors::EleboxError};
use crate::{csv::*, db::*, errors::EleboxError, yaml::*};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

Expand Down Expand Up @@ -73,6 +73,13 @@ impl<'a> ManufacturerManager<'a> {
));
}

if ori_name != &new_mfr.name && self.db.get_mfr_id(&new_mfr.name).is_some() {
return Err(EleboxError::AlreadyExists(
"Mfr".to_string(),
new_mfr.name.clone(),
));
}

let db_mfr = self.to_db_mfr(new_mfr)?;
self.db.update_mfr(ori_name, &db_mfr);
Ok(())
Expand Down Expand Up @@ -108,14 +115,14 @@ impl<'a> ManufacturerManager<'a> {
.collect()
}

pub fn export_csv(&self, filename: &str) -> Result<(), ()> {
pub fn export(&self, filename: &str) -> Result<(), ()> {
let mfrs = self.list();
let res = write_csv(filename, mfrs, None);
let res = write_yaml(filename, mfrs);
return res;
}

pub fn import_csv(&self, filename: &str) -> Result<(), ()> {
let res_parts = read_csv(filename, None);
pub fn import(&self, filename: &str) -> Result<(), ()> {
let res_parts = read_yaml(filename);
if res_parts.is_err() {
return Err(());
}
Expand Down
17 changes: 12 additions & 5 deletions elebox-core/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{csv::*, db::*, errors::EleboxError};
use crate::{csv::*, db::*, errors::EleboxError, yaml::*};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

Expand Down Expand Up @@ -82,6 +82,13 @@ impl<'a> PackageManager<'a> {
));
}

if ori_name != &new_package.name && self.db.get_package_id(&new_package.name).is_some() {
return Err(EleboxError::AlreadyExists(
"Package".to_string(),
new_package.name.clone(),
));
}

let db_pkg = self.to_db_package(new_package)?;
self.db.update_package(ori_name, &db_pkg);
Ok(())
Expand Down Expand Up @@ -124,14 +131,14 @@ impl<'a> PackageManager<'a> {
.collect()
}

pub fn export_csv(&self, filename: &str) -> Result<(), ()> {
pub fn export(&self, filename: &str) -> Result<(), ()> {
let pkgs = self.list();
let res = write_csv(filename, pkgs, None);
let res = write_yaml(filename, pkgs);
return res;
}

pub fn import_csv(&self, filename: &str) -> Result<(), ()> {
let res_parts = read_csv(filename, None);
pub fn import(&self, filename: &str) -> Result<(), ()> {
let res_parts = read_yaml(filename);
if res_parts.is_err() {
return Err(());
}
Expand Down
22 changes: 16 additions & 6 deletions elebox-core/src/part.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{csv::*, db::*, errors::EleboxError};
use crate::{csv::*, db::*, errors::EleboxError, yaml::*};
use serde::{Deserialize, Serialize};
use std::fmt::{format, Debug};

Expand Down Expand Up @@ -127,6 +127,13 @@ impl<'a> PartManager<'a> {
));
}

if ori_name != &new_part.name && self.db.get_part_id(&new_part.name).is_some() {
return Err(EleboxError::AlreadyExists(
"Part".to_string(),
new_part.name.clone(),
));
}

let db_part = self.to_db_part(new_part)?;
self.db.update_part(ori_name, &db_part);
Ok(())
Expand Down Expand Up @@ -224,21 +231,24 @@ impl<'a> PartManager<'a> {
.collect()
}

pub fn export_csv(&self, filename: &str) -> Result<(), ()> {
pub fn export(&self, filename: &str) -> Result<(), ()> {
let parts = self.list();
let res = write_csv(filename, parts, None);
let res = write_yaml(filename, parts);
return res;
}

pub fn import_csv(&self, filename: &str) -> Result<(), ()> {
let res_parts = read_csv(filename, None);
pub fn import(&self, filename: &str) -> Result<(), ()> {
let res_parts = read_yaml(filename);

if res_parts.is_err() {
return Err(());
}

let parts: Vec<Part> = res_parts.unwrap();
for part in parts {
let _ = self.add(&part);
if let Err(e) = self.add(&part) {
panic!("{}", e.to_string())
}
}

Ok(())
Expand Down
24 changes: 24 additions & 0 deletions elebox-core/src/yaml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::fs::{self, File};

use csv::{ReaderBuilder, WriterBuilder};
use serde::{Deserialize, Serialize};

use serde_yaml;

pub fn write_yaml<T>(filename: &str, items: Vec<T>) -> Result<(), ()>
where
T: Serialize,
{
let contents = serde_yaml::to_string(&items).unwrap();
fs::write(filename, contents);
Ok(())
}

pub fn read_yaml<T>(filename: &str) -> Result<Vec<T>, ()>
where
T: for<'a> Deserialize<'a>,
{
let content = fs::read_to_string(filename).unwrap();
let items: Vec<T> = serde_yaml::from_str(&content).unwrap();
Ok(items)
}
2 changes: 1 addition & 1 deletion elebox-tauri/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elebox",
"private": true,
"version": "1.0.0-beta.2",
"version": "1.0.0-beta.3",
"type": "module",
"scripts": {
"tauri dev": "tauri dev",
Expand Down
2 changes: 1 addition & 1 deletion elebox-tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "elebox"
version = "1.0.0-beta.2"
version = "1.0.0-beta.3"
license = "MIT OR Apache-2.0"
description = "Lightweight personal electronic parts inventory management tool"
repository = "https://github.com/ziteh/elebox"
Expand Down
Loading

0 comments on commit f3fce25

Please sign in to comment.