Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update cargotoml for root project #5

Closed
wants to merge 9 commits into from
16 changes: 8 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

cargobase.json
test.json
cargobase-async.json

src/main.rs
src/cargobase/cargobase.json
src/cargobase-async/cargobase-async.json

cargobase/src/main.rs
cargobase-async/src/main.rs

.env

_deprecated
36 changes: 22 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
[package]
name = "cargobase"
version = "0.1.1"
edition = "2021"

[dependencies]
[workspace]
members = [
"cargobase",
"cargobase_async",
"cargobase_core"
]
# [package]
# name = "cargobase"
# version = "0.1.1"
# edition = "2021"
#
[workspace.dependencies]
serde = { version = "1.0.215", features = ["derive"] }
serde_json = { version = "1.0.132", features = ["raw_value"] }
serde_derive = "1.0.188"
base64 = "0.22.1"
tokio = { version = "1", features = ["full"], optional = true}
# base64 = "0.22.1"
tokio = { version = "1", features = ["full"]}
# tokio = { version = "1", features = ["full"], optional = true}
uuid = {version ="1.11.0", features = ["v4"] }
thiserror = "2.0.3"
tempfile = "3.14.0"
Expand All @@ -17,9 +24,10 @@ tracing = "0.1"
tracing-subscriber = "0.3"
tracing-test = "0.2.5"

[features]
default = ["sync", "async"] # for development
# default = ["sync"]
sync = [] # synchronous features only
async = ["tokio"] # asynchronous features only
full = ["sync", "async"] # all features
# [features]
# default = []
# default = ["sync", "async"] # for development
# # default = ["sync"]
# sync = [] # synchronous features only
# async = ["tokio"] # asynchronous features only
# full = ["sync", "async"] # all features
35 changes: 35 additions & 0 deletions cargobase/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "cargobase"
version = "0.1.1"
edition = "2021"

[dependencies]
cargobase_core = { path = "../cargobase_core/"}
serde = { workspace = true }
serde_json = { workspace = true }
serde_derive = { workspace = true }
uuid = { workspace = true }
thiserror = { workspace = true }
tempfile = { workspace = true }
serde-reflection = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-test = { workspace = true }

[features]
default = ["sync"]
sync = []


# serde = { version = "1.0.215", features = ["derive"] }
# serde_json = { version = "1.0.132", features = ["raw_value"] }
# serde_derive = "1.0.188"
# # base64 = "0.22.1"
# # tokio = { version = "1", features = ["full"], optional = true}
# uuid = {version ="1.11.0", features = ["v4"] }
# thiserror = "2.0.3"
# tempfile = "3.14.0"
# serde-reflection = "0.4.0"
# tracing = "0.1"
# tracing-subscriber = "0.3"
# tracing-test = "0.2.5"
211 changes: 107 additions & 104 deletions src/cargobase/database.rs → cargobase/src/cargobase/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use serde::{Deserialize, Serialize};
use tracing;

use super::view::View;
use super::DatabaseError;
use super::{query::Operation, Query, Table};

use cargobase_core::DatabaseError;

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Database {
pub(crate) name: String,
Expand Down Expand Up @@ -41,35 +42,35 @@ impl Database {
}
}

#[cfg(feature = "async")]
pub async fn new_async(name: &str) -> Self {
let name = name.to_string();
let file_name = format!("{name}.json");

if tokio::fs::metadata(&file_name).await.is_ok() {
tracing::info!("Database already exists: {name}, loading database");

// Load the database from the file
match Database::load_from_file_async(&file_name).await {
Ok(db) => return db,
Err(e) => {
tracing::error!("Failed to load database from file: {file_name}, error: {e}");
}
}
} else {
tracing::info!("Creating new database: {file_name}");
// Create an empty JSON file for the new database
if let Err(e) = tokio::fs::write(&file_name, "{}").await {
tracing::error!("Failed to create database file: {e}");
}
}

Database {
name,
file_name,
tables: Vec::new(),
}
}
// #[cfg(feature = "async")]
// pub async fn new_async(name: &str) -> Self {
// let name = name.to_string();
// let file_name = format!("{name}.json");
//
// if tokio::fs::metadata(&file_name).await.is_ok() {
// tracing::info!("Database already exists: {name}, loading database");
//
// // Load the database from the file
// match Database::load_from_file_async(&file_name).await {
// Ok(db) => return db,
// Err(e) => {
// tracing::error!("Failed to load database from file: {file_name}, error: {e}");
// }
// }
// } else {
// tracing::info!("Creating new database: {file_name}");
// // Create an empty JSON file for the new database
// if let Err(e) = tokio::fs::write(&file_name, "{}").await {
// tracing::error!("Failed to create database file: {e}");
// }
// }
//
// Database {
// name,
// file_name,
// tables: Vec::new(),
// }
// }

pub fn drop_database(&self) -> Result<(), DatabaseError> {
if std::fs::remove_file(&self.file_name).is_err() {
Expand Down Expand Up @@ -122,13 +123,13 @@ impl Database {
Ok(())
}

#[cfg(feature = "async")]
pub(crate) async fn save_to_file_async(&self) -> Result<(), tokio::io::Error> {
let json_data = serde_json::to_string_pretty(&self)?;
tokio::fs::write(&self.file_name, json_data).await?;
tracing::info!("Database saved to file: {}", self.file_name);
Ok(())
}
// #[cfg(feature = "async")]
// pub(crate) async fn save_to_file_async(&self) -> Result<(), tokio::io::Error> {
// let json_data = serde_json::to_string_pretty(&self)?;
// tokio::fs::write(&self.file_name, json_data).await?;
// tracing::info!("Database saved to file: {}", self.file_name);
// Ok(())
// }

pub(crate) fn load_from_file(file_name: &str) -> Result<Self, std::io::Error> {
let json_data = std::fs::read_to_string(file_name)?;
Expand All @@ -137,13 +138,13 @@ impl Database {
Ok(db)
}

#[cfg(feature = "async")]
pub(crate) async fn load_from_file_async(file_name: &str) -> Result<Self, tokio::io::Error> {
let json_data = tokio::fs::read_to_string(file_name).await?;
let db: Database = serde_json::from_str(&json_data)?;
tracing::info!("Database loaded from file: {}", file_name);
Ok(db)
}
// #[cfg(feature = "async")]
// pub(crate) async fn load_from_file_async(file_name: &str) -> Result<Self, tokio::io::Error> {
// let json_data = tokio::fs::read_to_string(file_name).await?;
// let db: Database = serde_json::from_str(&json_data)?;
// tracing::info!("Database loaded from file: {}", file_name);
// Ok(db)
// }

pub(crate) fn get_table_mut(&mut self, table_name: &str) -> Option<&mut Table> {
self.tables.iter_mut().find(|t| t.name == table_name)
Expand Down Expand Up @@ -216,10 +217,12 @@ mod tests {

use super::*;
use crate::cargobase::setup_temp_db;
use crate::{Columns, Table};
use crate::Table;

use cargobase_core::Columns;

#[cfg(feature = "async")]
use crate::cargobase::setup_temp_db_async;
// #[cfg(feature = "async")]
// use crate::cargobase::setup_temp_db_async;

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Default)]
struct TestData {
Expand All @@ -239,18 +242,18 @@ mod tests {
assert_eq!(db.tables.len(), 1); // the setup_temp_db function adds a table
}

#[cfg(feature = "async")]
#[tokio::test]
async fn test_database_new_async() {
let db = setup_temp_db_async().await;

let db_name = &db.name.to_string();
let fnn = format!("{db_name}.json");

assert_eq!(db.name, db_name.to_string());
assert_eq!(db.file_name, fnn);
assert_eq!(db.tables.len(), 1); // the setup_temp_db function adds a table
}
// #[cfg(feature = "async")]
// #[tokio::test]
// async fn test_database_new_async() {
// let db = setup_temp_db_async().await;
//
// let db_name = &db.name.to_string();
// let fnn = format!("{db_name}.json");
//
// assert_eq!(db.name, db_name.to_string());
// assert_eq!(db.file_name, fnn);
// assert_eq!(db.tables.len(), 1); // the setup_temp_db function adds a table
// }

#[test]
fn test_drop_database() {
Expand Down Expand Up @@ -345,49 +348,49 @@ mod tests {
assert_eq!(db, loaded_db);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn test_save_to_file_async() {
use tempfile::NamedTempFile;

let temp_file = NamedTempFile::new().expect("Failed to create a temporary file");
let db_path = temp_file.path().to_str().unwrap().to_string();

let db = Database {
name: "test_db".to_string(),
file_name: db_path.clone(),
tables: vec![],
};

db.save_to_file_async()
.await
.expect("Failed to save database");
let loaded_db = Database::load_from_file(&db_path).expect("Failed to load database");
assert_eq!(db, loaded_db);
}

#[cfg(feature = "async")]
#[tokio::test]
async fn test_load_from_file_async() {
use tempfile::NamedTempFile;

let temp_file = NamedTempFile::new().expect("Failed to create a temporary file");
let db_path = temp_file.path().to_str().unwrap().to_string();

let db = Database {
name: "test_db".to_string(),
file_name: db_path.to_string(),
tables: vec![],
};

db.save_to_file_async()
.await
.expect("Failed to save database");

let loaded_db = Database::load_from_file_async(&db_path)
.await
.expect("Failed to load database");

assert_eq!(db, loaded_db);
}
// #[cfg(feature = "async")]
// #[tokio::test]
// async fn test_save_to_file_async() {
// use tempfile::NamedTempFile;
//
// let temp_file = NamedTempFile::new().expect("Failed to create a temporary file");
// let db_path = temp_file.path().to_str().unwrap().to_string();
//
// let db = Database {
// name: "test_db".to_string(),
// file_name: db_path.clone(),
// tables: vec![],
// };
//
// db.save_to_file_async()
// .await
// .expect("Failed to save database");
// let loaded_db = Database::load_from_file(&db_path).expect("Failed to load database");
// assert_eq!(db, loaded_db);
// }

// #[cfg(feature = "async")]
// #[tokio::test]
// async fn test_load_from_file_async() {
// use tempfile::NamedTempFile;
//
// let temp_file = NamedTempFile::new().expect("Failed to create a temporary file");
// let db_path = temp_file.path().to_str().unwrap().to_string();
//
// let db = Database {
// name: "test_db".to_string(),
// file_name: db_path.to_string(),
// tables: vec![],
// };
//
// db.save_to_file_async()
// .await
// .expect("Failed to save database");
//
// let loaded_db = Database::load_from_file_async(&db_path)
// .await
// .expect("Failed to load database");
//
// assert_eq!(db, loaded_db);
// }
}
14 changes: 14 additions & 0 deletions cargobase/src/cargobase/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub mod database;
pub mod query;
pub mod table;
pub mod util;
pub mod view;

pub use database::Database;
pub use query::Query;
pub use table::Table;
pub use util::setup_temp_db;
pub use view::View;

// #[cfg(feature = "async")]
// pub use util::setup_temp_db_async;
4 changes: 3 additions & 1 deletion src/cargobase/query.rs → cargobase/src/cargobase/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::{Database, DatabaseError, Row, Table};
use super::{Database, Table};

use cargobase_core::{DatabaseError, Row};

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Copy)]
pub enum Operation {
Expand Down
Loading
Loading