Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
ziteh committed Sep 28, 2024
2 parents 193515c + d44bacd commit 429b6f7
Show file tree
Hide file tree
Showing 17 changed files with 213 additions and 70 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: GitHub Pages deploy

on:
workflow_dispatch:
push:
branches: [main]
# push:
# branches: [main]

permissions:
contents: write
Expand Down
4 changes: 2 additions & 2 deletions docs/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = withNextra({
// locales: ["en", "zh"],
// defaultLocale: "en",
// },
assetPrefix: "./",
basePath: "/elebox",
// assetPrefix: "./",
// basePath: "/elebox",
reactStrictMode: true,
images: {
loader: "akamai",
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import Image from "next/image";
Download
</Button>
<Button
href="/elebox/docs/overview"
href="/docs/overview"
variant="text"
disableElevation
sx={{ textTransform: "none" }}
Expand Down
10 changes: 8 additions & 2 deletions elebox-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ struct Cli {

#[derive(Debug, Args)]
struct PathArgs {
filetype: String, // TODO enum

#[arg(default_value = "./")]
path: String,
}
Expand Down Expand Up @@ -70,7 +72,11 @@ fn main() {
EntityType::Category(cmd) => Ok(category_cmd(manager.category(), cmd)),
EntityType::Mfr(cmd) => Ok(manufacturer_cmd(manager.manufacturer(), cmd)),
EntityType::Package(cmd) => Ok(package_cmd(manager.package(), cmd)),
EntityType::Export(args) => manager.export(&PathBuf::from(args.path.clone())),
EntityType::Import(args) => manager.import(&PathBuf::from(args.path.clone())),
EntityType::Export(args) => {
manager.export(&PathBuf::from(args.path.clone()), &args.filetype)
}
EntityType::Import(args) => {
manager.import(&PathBuf::from(args.path.clone()), &args.filetype)
}
};
}
22 changes: 14 additions & 8 deletions elebox-cli/src/part_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,20 @@ pub fn part_cmd(handler: elebox_core::PartHandler, cmd: &PartCommand) {
println!("ERR: {err}");
}
}
PartSubCommand::Export(args) => match handler.export(&PathBuf::from(&args.path)) {
Ok(_) => println!("Export success: {}", args.path),
Err(_) => println!("Export error: {}", args.path),
},
PartSubCommand::Import(args) => match handler.import(&PathBuf::from(&args.path)) {
Ok(_) => println!("Import success: {}", args.path),
Err(_) => println!("Import error: {}", args.path),
},
PartSubCommand::Export(args) => {
todo!();
// match handler.export(&PathBuf::from(&args.path)) {
// Ok(_) => println!("Export success: {}", args.path),
// Err(_) => println!("Export error: {}", args.path),
// }
}
PartSubCommand::Import(args) => {
todo!();
// match handler.import(&PathBuf::from(&args.path)) {
// Ok(_) => println!("Import success: {}", args.path),
// Err(_) => println!("Import error: {}", args.path),
// }
}
},
None => {
println!("List part");
Expand Down
7 changes: 4 additions & 3 deletions elebox-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ uuid = { version = "1.5.0", features = [
] }
csv = "1.3.0"
serde_yaml = "0.9.34"
serde_json = "1.0.122"

[dev-dependencies]
mockall = "0.13.0"

[profile.release]
lto = true
strip = true

[dev-dependencies]
mockall = "0.13.0"
20 changes: 17 additions & 3 deletions elebox-core/src/category.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{comm::*, errors::EleboxError, jamm_db::*, yaml::*};
use crate::{comm::*, errors::EleboxError, jamm_db::*, json::*, yaml::*};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt::Debug, path::PathBuf};

Expand Down Expand Up @@ -193,12 +193,26 @@ impl<'a> Handler<Category> for CategoryHandler<'_> {
impl Transferable for CategoryHandler<'_> {
fn export(&self, filename: &PathBuf) -> Result<(), EleboxError> {
let items = self.list()?;
let _ = write_yaml(filename, items).unwrap();

if YamlFile::check_extension(filename) {
let _ = YamlFile::write(filename, items);
} else if JsonFile::check_extension(filename) {
let _ = JsonFile::write(filename, items);
} else {
todo!()
}

Ok(())
}

fn import(&self, filename: &PathBuf) -> Result<(), EleboxError> {
let res_items = read_yaml(filename);
let res_items: Result<Vec<Category>, ()> = if YamlFile::check_extension(filename) {
YamlFile::read(filename)
} else if JsonFile::check_extension(filename) {
JsonFile::read(filename)
} else {
todo!()
};

if res_items.is_err() {
todo!()
Expand Down
8 changes: 8 additions & 0 deletions elebox-core/src/comm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::{DbError, EleboxError};

pub const ITEM_PART: &str = "part";
Expand Down Expand Up @@ -30,3 +32,9 @@ pub trait Transferable {
fn export(&self, filename: &PathBuf) -> Result<(), EleboxError>;
fn import(&self, filename: &PathBuf) -> Result<(), EleboxError>;
}

pub trait HumanReadable {
fn write<T: Serialize>(filename: &PathBuf, items: Vec<T>) -> Result<(), ()>;
fn read<T: for<'de> Deserialize<'de>>(filename: &PathBuf) -> Result<Vec<T>, ()>;
fn check_extension(filename: &PathBuf) -> bool;
}
35 changes: 35 additions & 0 deletions elebox-core/src/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::HumanReadable;
use serde::{Deserialize, Serialize};
use serde_json;
use std::{
fs::{self},
path::PathBuf,
};

pub struct JsonFile {}

impl HumanReadable for JsonFile {
fn write<T>(filename: &PathBuf, items: Vec<T>) -> Result<(), ()>
where
T: Serialize,
{
// TODO unwrap
let contents = serde_json::to_string(&items).unwrap();
fs::write(filename, contents).unwrap();
Ok(())
}

fn read<T>(filename: &PathBuf) -> Result<Vec<T>, ()>
where
T: for<'de> Deserialize<'de>,
{
// TODO unwrap
let content = fs::read_to_string(filename).unwrap();
let items: Vec<T> = serde_json::from_str(&content).unwrap();
Ok(items)
}

fn check_extension(filename: &PathBuf) -> bool {
filename.extension().map_or(false, |ext| ext == "json")
}
}
1 change: 1 addition & 0 deletions elebox-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod csv;
mod default_db;
mod errors;
mod jamm_db;
mod json;
mod manager;
mod manufacturer;
mod package;
Expand Down
23 changes: 12 additions & 11 deletions elebox-core/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,34 @@ impl Manager {
ManufacturerHandler { db: &*self.mfr_db }
}

pub fn export(&self, path: &PathBuf) -> Result<(), EleboxError> {
let filename = path.join(PART_FILENAME);
pub fn export(&self, path: &PathBuf, extension: &str) -> Result<(), EleboxError> {
let filename = path.join(PART_FILENAME).with_extension(extension);
let _ = self.part().export(&filename)?;

let filename = path.join(PACKAGE_FILENAME);
let filename = path.join(PACKAGE_FILENAME).with_extension(extension);
let _ = self.package().export(&filename)?;

let filename = path.join(CATEGORY_FILENAME);
let filename = path.join(CATEGORY_FILENAME).with_extension(extension);
let _ = self.category().export(&filename)?;

let filename = path.join(MFR_FILENAME);
let filename = path.join(MFR_FILENAME).with_extension(extension);
let _ = self.manufacturer().export(&filename)?;

Ok(())
}

pub fn import(&self, path: &PathBuf) -> Result<(), EleboxError> {
let filename = path.join(CATEGORY_FILENAME);
// TODO auto detect type
pub fn import(&self, path: &PathBuf, extension: &str) -> Result<(), EleboxError> {
let filename = path.join(CATEGORY_FILENAME).with_extension(extension);
let _ = self.category().import(&filename);

let filename = path.join(PACKAGE_FILENAME);
let filename = path.join(PACKAGE_FILENAME).with_extension(extension);
let _ = self.package().import(&filename);

let filename = path.join(MFR_FILENAME);
let filename = path.join(MFR_FILENAME).with_extension(extension);
let _ = self.manufacturer().import(&filename);

let filename = path.join(PART_FILENAME);
let filename = path.join(PART_FILENAME).with_extension(extension);
let _ = self.part().import(&filename);

Ok(())
Expand All @@ -112,7 +113,7 @@ impl Manager {
mfr_db: Box<dyn Database<DbManufacturer>>,
path: &PathBuf,
) -> Result<Self, EleboxError> {
// TODO
// TODO extension
let mgr = Self::new(part_db, package_db, category_db, mfr_db);
let _ = mgr.init();

Expand Down
20 changes: 17 additions & 3 deletions elebox-core/src/manufacturer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{comm::*, errors::EleboxError, jamm_db::*, yaml::*};
use crate::{comm::*, errors::EleboxError, jamm_db::*, json::*, yaml::*};
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, path::PathBuf};

Expand Down Expand Up @@ -102,12 +102,26 @@ impl<'a> Handler<Manufacturer> for ManufacturerHandler<'_> {
impl Transferable for ManufacturerHandler<'_> {
fn export(&self, filename: &PathBuf) -> Result<(), EleboxError> {
let items = self.list()?;
let _ = write_yaml(filename, items).unwrap();

if YamlFile::check_extension(filename) {
let _ = YamlFile::write(filename, items);
} else if JsonFile::check_extension(filename) {
let _ = JsonFile::write(filename, items);
} else {
todo!()
}

Ok(())
}

fn import(&self, filename: &PathBuf) -> Result<(), EleboxError> {
let res_items = read_yaml(filename);
let res_items: Result<Vec<Manufacturer>, ()> = if YamlFile::check_extension(filename) {
YamlFile::read(filename)
} else if JsonFile::check_extension(filename) {
JsonFile::read(filename)
} else {
todo!()
};

if res_items.is_err() {
todo!()
Expand Down
20 changes: 17 additions & 3 deletions elebox-core/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{comm::*, errors::EleboxError, jamm_db::*, yaml::*};
use crate::{comm::*, errors::EleboxError, jamm_db::*, json::*, yaml::*};
use core::fmt;
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, path::PathBuf};
Expand Down Expand Up @@ -129,12 +129,26 @@ impl<'a> Handler<Package> for PackageHandler<'_> {
impl Transferable for PackageHandler<'_> {
fn export(&self, filename: &PathBuf) -> Result<(), EleboxError> {
let items = self.list()?;
let _ = write_yaml(filename, items).unwrap();

if YamlFile::check_extension(filename) {
let _ = YamlFile::write(filename, items);
} else if JsonFile::check_extension(filename) {
let _ = JsonFile::write(filename, items);
} else {
todo!()
}

Ok(())
}

fn import(&self, filename: &PathBuf) -> Result<(), EleboxError> {
let res_items = read_yaml(filename);
let res_items: Result<Vec<Package>, ()> = if YamlFile::check_extension(filename) {
YamlFile::read(filename)
} else if JsonFile::check_extension(filename) {
JsonFile::read(filename)
} else {
todo!()
};

if res_items.is_err() {
todo!()
Expand Down
26 changes: 20 additions & 6 deletions elebox-core/src/part.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{comm::*, errors::*, jamm_db::*, yaml::*};
use crate::{comm::*, errors::*, jamm_db::*, json::*, yaml::*};

use serde::{Deserialize, Serialize};
use std::{fmt::Debug, path::PathBuf};
Expand Down Expand Up @@ -230,19 +230,33 @@ impl<'a> Handler<Part> for PartHandler<'_> {

impl Transferable for PartHandler<'_> {
fn export(&self, filename: &PathBuf) -> Result<(), EleboxError> {
let parts = self.list()?;
let _ = write_yaml(filename, parts).unwrap();
let items = self.list()?;

if YamlFile::check_extension(filename) {
let _ = YamlFile::write(filename, items);
} else if JsonFile::check_extension(filename) {
let _ = JsonFile::write(filename, items);
} else {
todo!()
}

Ok(())
}

fn import(&self, filename: &PathBuf) -> Result<(), EleboxError> {
let res_parts = read_yaml(filename);
let res_items: Result<Vec<Part>, ()> = if YamlFile::check_extension(filename) {
YamlFile::read(filename)
} else if JsonFile::check_extension(filename) {
JsonFile::read(filename)
} else {
todo!()
};

if res_parts.is_err() {
if res_items.is_err() {
todo!()
}

let parts: Vec<Part> = res_parts.unwrap();
let parts: Vec<Part> = res_items.unwrap();
for part in parts {
if let Err(e) = self.add(&part) {
match e {
Expand Down
Loading

0 comments on commit 429b6f7

Please sign in to comment.