Skip to content

Commit

Permalink
idk
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe-g-gelardi committed Nov 25, 2024
1 parent 586eeea commit acea68d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
20 changes: 10 additions & 10 deletions src/cargobase/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ impl Columns {
Columns(columns)
}

// pub fn from_struct<T: Serialize + Default>(required: bool) -> Self {
// let value = json!(T::default());
// let columns = if let Value::Object(map) = value {
// map.keys().map(|key| Column::new(key, required)).collect()
// } else {
// vec![]
// };
// Columns(columns)
// }

pub fn from_struct<T: Serialize + DeserializeOwned + Default>(required: bool) -> Self {
// Initialize the reflection tracer
let mut tracer = Tracer::new(TracerConfig::default());
Expand Down Expand Up @@ -85,6 +75,16 @@ impl Columns {
}
}

// pub fn from_struct<T: Serialize + Default>(required: bool) -> Self {
// let value = json!(T::default());
// let columns = if let Value::Object(map) = value {
// map.keys().map(|key| Column::new(key, required)).collect()
// } else {
// vec![]
// };
// Columns(columns)
// }

#[cfg(test)]
mod tests {
use super::*;
Expand Down
22 changes: 9 additions & 13 deletions src/cargobase/database.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use super::util::init_tracing;
use super::{query::Operation, Query, Table};
use serde::{Deserialize, Serialize};

use tracing::{error, info};

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

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Database {
Expand All @@ -18,21 +15,19 @@ impl Database {
let name = name.to_string();
let file_name = format!("{name}.json");

init_tracing();

if std::path::Path::new(&file_name).exists() {
info!("Database already exists: {name}, loading database");
println!("Database already exists: {name}, loading database");

if let Ok(db) = Database::load_from_file(&file_name) {
return db;
} else {
error!("Failed to load database from file: {file_name}");
eprintln!("Failed to load database from file: {file_name}");
}
} else {
info!("Creating new database: {file_name}");
println!("Creating new database: {file_name}");
// Create an empty JSON file for the new database
if let Err(e) = std::fs::write(&file_name, "{}") {
error!("Failed to create database file: {e}");
eprintln!("Failed to create database file: {e}");
}
}

Expand All @@ -55,20 +50,21 @@ impl Database {
Ok(())
}

// pub fn drop_table(&mut self, table_name: &str) -> Result<(), String> {
pub fn drop_table(&mut self, table_name: &str) -> Result<(), DatabaseError> {
let mut db =
Database::load_from_file(&self.file_name).map_err(|e| DatabaseError::LoadError(e))?;

if let Some(index) = db.tables.iter().position(|t| t.name == table_name) {
let removed_table = db.tables.remove(index);
println!("Table {} dropped successfully", removed_table.name);
println!("Table `{}` dropped successfully", removed_table.name);
db.save_to_file().map_err(|e| DatabaseError::SaveError(e))?;

self.tables = db.tables;
Ok(())
} else {
Err(DatabaseError::TableNotFound(table_name.to_string()))
eprintln!("{}", DatabaseError::TableNotFound(table_name.to_string()));
// Err(DatabaseError::TableNotFound(table_name.to_string()))
Ok(())
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/cargobase/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ pub fn setup_temp_db() -> Database {
pub fn init_tracing() {
let subscriber = fmt::Subscriber::builder()
.with_max_level(tracing::Level::DEBUG)
.with_target(false)
.without_time()
.compact()
.finish();
/*
example implementation:
info!(target: "cargobase", "Database `{name}` already exists, loading...");
*/
tracing::subscriber::set_global_default(subscriber).expect("Failed to set subscriber");
}

0 comments on commit acea68d

Please sign in to comment.