refactor: make public the code that needs to be exposed, remove unused code
This commit is contained in:
159
Cargo.lock
generated
159
Cargo.lock
generated
@@ -2,26 +2,127 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.8.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"version_check",
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "allocator-api2"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.58"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1e928d4b69e3077709075a938a05ffbedfa53a84c8f766efbf8220bb1ff60e1"
|
||||
dependencies = [
|
||||
"find-msvc-tools",
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
|
||||
|
||||
[[package]]
|
||||
name = "db_builder"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fallible-iterator"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649"
|
||||
|
||||
[[package]]
|
||||
name = "fallible-streaming-iterator"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
|
||||
|
||||
[[package]]
|
||||
name = "find-msvc-tools"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.14.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||
dependencies = [
|
||||
"ahash",
|
||||
"allocator-api2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashlink"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8094feaf31ff591f651a2664fb9cfd92bba7a60ce3197265e9482ebe753c8f7"
|
||||
dependencies = [
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "libsqlite3-sys"
|
||||
version = "0.27.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf4e226dcd58b4be396f7bd3c20da8fdee2911400705297ba7d2d7cc2c30f716"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"pkg-config",
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
|
||||
|
||||
[[package]]
|
||||
name = "pkg-config"
|
||||
version = "0.3.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
@@ -40,6 +141,20 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rusqlite"
|
||||
version = "0.30.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a78046161564f5e7cd9008aff3b2990b3850dc8e0349119b98e8f251e099f24d"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"fallible-iterator",
|
||||
"fallible-streaming-iterator",
|
||||
"hashlink",
|
||||
"libsqlite3-sys",
|
||||
"smallvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
@@ -83,6 +198,18 @@ dependencies = [
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.15.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.117"
|
||||
@@ -100,6 +227,38 @@ version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.8.48"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eed437bf9d6692032087e337407a86f04cd8d6a16a37199ed57949d415bd68e9"
|
||||
dependencies = [
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.8.48"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70e3cd084b1788766f53af483dd21f93881ff30d7320490ec3ef7526d203bad4"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
|
||||
@@ -6,3 +6,4 @@ edition = "2024"
|
||||
[dependencies]
|
||||
serde_json = "1.0.149"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
rusqlite = { version = "0.30.0", features = ["bundled"] }
|
||||
|
||||
53
src/lib.rs
53
src/lib.rs
@@ -1,3 +1,50 @@
|
||||
mod mappings;
|
||||
mod schemas;
|
||||
mod query_builders;
|
||||
use crate::databases::{connect, disconnect, run_command};
|
||||
use crate::query_builders::table_sql_serializer::TableSqlSerializer;
|
||||
use crate::schemas::reader::SchemaParsingFacade;
|
||||
|
||||
pub mod mappings;
|
||||
pub mod schemas;
|
||||
pub mod query_builders;
|
||||
mod databases;
|
||||
|
||||
pub struct DbBuilderFacade {}
|
||||
|
||||
impl DbBuilderFacade {
|
||||
pub fn build(self, json_schema_path: String, db_connection: String) -> Result<(), String> {
|
||||
let schema_parsing_facade = SchemaParsingFacade{};
|
||||
let tables = schema_parsing_facade.parse(json_schema_path);
|
||||
|
||||
let table_serializer = TableSqlSerializer{};
|
||||
|
||||
let mut sql_tables = String::new();
|
||||
|
||||
for table in tables {
|
||||
sql_tables += &table_serializer.serialize(table);
|
||||
}
|
||||
|
||||
let connection_result = connect(db_connection);
|
||||
|
||||
if let Ok(connection) = connection_result {
|
||||
run_command(&connection, sql_tables).expect("Failed to execute query");
|
||||
disconnect(connection).expect("Failed to disconnect from sqlite");
|
||||
Ok(())
|
||||
} else {
|
||||
Err("Failed to connect to the server".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_db_builder() {
|
||||
let facade = DbBuilderFacade{};
|
||||
|
||||
match facade.build("./example.json".to_string(), "test_db".to_string()) {
|
||||
Ok(_) => {assert!(true)},
|
||||
Err(message) => {assert!(false, "{}", message)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,6 @@ use crate::schemas::entities::Column;
|
||||
pub struct ColumnSqlSerializer {}
|
||||
|
||||
impl ColumnSqlSerializer {
|
||||
pub fn new() -> Self {
|
||||
ColumnSqlSerializer {}
|
||||
}
|
||||
|
||||
pub fn serialize(self, column: Column) -> Result<String, String> {
|
||||
let mut query = String::from(column.name);
|
||||
query.push(' ');
|
||||
@@ -71,7 +67,7 @@ mod tests {
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let serializer = ColumnSqlSerializer::new();
|
||||
let serializer = ColumnSqlSerializer{};
|
||||
let result = serializer.serialize(column).unwrap();
|
||||
|
||||
assert_eq!(result, "id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY ,".to_string());
|
||||
@@ -86,7 +82,7 @@ mod tests {
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let serializer = ColumnSqlSerializer::new();
|
||||
let serializer = ColumnSqlSerializer{};
|
||||
let result = serializer.serialize(column).unwrap();
|
||||
|
||||
assert_eq!(result, "name TEXT NOT NULL DEFAULT 'None' ,".to_string());
|
||||
@@ -101,7 +97,7 @@ mod tests {
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let serializer = ColumnSqlSerializer::new();
|
||||
let serializer = ColumnSqlSerializer {};
|
||||
let result = serializer.serialize(column).unwrap();
|
||||
|
||||
assert_eq!(result, "age INTEGER NOT NULL DEFAULT 18 ,".to_string());
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
mod column_sql_serializer;
|
||||
mod table_sql_serializer;
|
||||
pub(crate) mod column_sql_serializer;
|
||||
pub(crate) mod table_sql_serializer;
|
||||
|
||||
@@ -5,15 +5,11 @@ use crate::schemas::entities::Table;
|
||||
pub struct TableSqlSerializer {}
|
||||
|
||||
impl TableSqlSerializer {
|
||||
pub fn new() -> Self {
|
||||
TableSqlSerializer{}
|
||||
}
|
||||
|
||||
pub fn serialize(self, table: Table) -> String {
|
||||
let mut query = String::from("CREATE TABLE ");
|
||||
query += &format!("{} (\n", table.name);
|
||||
|
||||
let column_serializer = ColumnSqlSerializer::new();
|
||||
let column_serializer = ColumnSqlSerializer{};
|
||||
for column in table.columns {
|
||||
query += &column_serializer.serialize(column).unwrap();
|
||||
}
|
||||
@@ -43,7 +39,7 @@ mod test {
|
||||
.with_column(column)
|
||||
.build();
|
||||
|
||||
let serializer = TableSqlSerializer::new();
|
||||
let serializer = TableSqlSerializer{};
|
||||
|
||||
let result = serializer.serialize(table);
|
||||
|
||||
|
||||
@@ -71,16 +71,16 @@ impl ColumnBuilder {
|
||||
return Err("Cannot set AUTO_INCREMENT on non-INTEGER column".to_string());
|
||||
}
|
||||
|
||||
Ok(Column::new(
|
||||
self.name,
|
||||
self.datatype,
|
||||
self.nullable,
|
||||
self.default,
|
||||
self.auto_increment,
|
||||
self.unique,
|
||||
self.check_constraint,
|
||||
self.primary_key,
|
||||
))
|
||||
Ok(Column {
|
||||
name: self.name,
|
||||
datatype: self.datatype,
|
||||
nullable: self.nullable,
|
||||
default: self.default,
|
||||
auto_increment: self.auto_increment,
|
||||
unique: self.unique,
|
||||
check_constraint: self.check_constraint,
|
||||
primary_key: self.primary_key,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ impl TableBuilder {
|
||||
}
|
||||
|
||||
pub fn build(self) -> Table {
|
||||
Table::new(self.name, self.columns, self.strict)
|
||||
Table { name: self.name, columns: self.columns, strict: self.strict }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
pub(crate) mod reader;
|
||||
pub(crate) mod entities;
|
||||
pub(crate) mod builders;
|
||||
mod writer;
|
||||
pub mod entities;
|
||||
pub mod builders;
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
use std::fs;
|
||||
use crate::schemas::entities::Table;
|
||||
|
||||
/// SchemaParsingFacade aim to hide the logic of reading and parsing the schema json file
|
||||
pub struct SchemaParsingFacade {}
|
||||
|
||||
impl SchemaParsingFacade {
|
||||
pub fn new() -> SchemaParsingFacade {
|
||||
SchemaParsingFacade{}
|
||||
}
|
||||
|
||||
pub fn parse(&self, path: String) -> Vec<Table> {
|
||||
let json_schema = self.read_schema_file(path);
|
||||
self.parse_schema_file(json_schema)
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use crate::schemas::entities::Table;
|
||||
|
||||
pub struct SchemaWriter {
|
||||
tables: Vec<Table>
|
||||
}
|
||||
|
||||
impl SchemaWriter {
|
||||
pub fn new() -> Self {
|
||||
SchemaWriter{
|
||||
tables: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_table(mut self, table: Table) -> Self {
|
||||
self.tables.push(table);
|
||||
self
|
||||
}
|
||||
|
||||
// create_schemas create the schema as a JSON file. the name doesn't need to have the ".json"
|
||||
pub fn create_schemas(self, schema_name: String) -> Result<(), String> {
|
||||
let schema_file_path = format!("{}.json", schema_name);
|
||||
|
||||
let content_result = serde_json::to_string(self.tables.as_slice()).expect("Error serializing tables");
|
||||
|
||||
let mut schema_file = File::create(schema_file_path).expect("Failed to create schema file");
|
||||
schema_file.write_all(content_result.as_bytes()).expect("Error writing in schema file");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user