From 9829bbfcd57c57e237c6aacb96a78d0b9f5bab68 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Tue, 29 Jun 2021 22:44:11 -0600 Subject: lol no generics --- Cargo.toml | 10 ++++++---- examples/tutorial01/main.rs | 3 +-- examples/tutorial02/main.rs | 3 +-- src/bin/tosin-admin.rs | 7 +++---- src/cli/make_migrations.rs | 4 ++-- src/cli/migrate.rs | 7 +++---- src/cli/mod.rs | 6 +++--- src/cli/run_server.rs | 4 ++-- src/db/backend.rs | 27 --------------------------- src/db/backend/mod.rs | 7 +++++++ src/db/backend/sqlite.rs | 22 ++++++++++++++++++++++ src/db/migration/change.rs | 8 ++++---- src/db/migration/mod.rs | 8 ++++---- src/settings.rs | 12 +++++------- tests/tutorial/mod.rs | 13 +++++-------- 15 files changed, 68 insertions(+), 73 deletions(-) delete mode 100644 src/db/backend.rs create mode 100644 src/db/backend/mod.rs create mode 100644 src/db/backend/sqlite.rs diff --git a/Cargo.toml b/Cargo.toml index ddb4cdb..5e0cf6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,12 +7,11 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -barrel = { version = "0.6.5", features = ["sqlite3"] } +barrel = { version = "0.6.5" } chrono = { version = "0.4", features = ["clock"] } -diesel = { version = "1.4.4", features = ["sqlite", "chrono"] } +diesel = { version = "1.4.4", features = ["chrono"] } hyper = { version = "0.14", features = ["full"] } -# TODO not this -libsqlite3-sys = { version = "0.22.2", features = ["bundled-windows"] } +libsqlite3-sys = { version = "0.22.2", features = ["bundled-windows"], optional = true } quote = "1.0" structopt = "0.3.21" syn = { version = "1.0", features = ["full", "extra-traits"] } @@ -22,3 +21,6 @@ warp = "0.3" [dev-dependencies] rand = "0.8.3" + +[features] +sqlite = ["barrel/sqlite3", "diesel/sqlite", "libsqlite3-sys"] diff --git a/examples/tutorial01/main.rs b/examples/tutorial01/main.rs index af2f14d..47623e6 100644 --- a/examples/tutorial01/main.rs +++ b/examples/tutorial01/main.rs @@ -2,7 +2,6 @@ extern crate diesel; use tosin::contrib::admin; -use tosin::db::backend::Connectable; use tosin::urls::{url_map, UrlMap}; use tosin::Settings; @@ -15,7 +14,7 @@ fn urls() -> UrlMap { } } -fn settings() -> Settings { +fn settings() -> Settings { Settings { ..Settings::default() } diff --git a/examples/tutorial02/main.rs b/examples/tutorial02/main.rs index 7c244dd..5d78e5b 100644 --- a/examples/tutorial02/main.rs +++ b/examples/tutorial02/main.rs @@ -2,7 +2,6 @@ extern crate diesel; use tosin::contrib::admin; -use tosin::db::backend::Connectable; use tosin::urls::{url_map, UrlMap}; use tosin::Settings; @@ -15,7 +14,7 @@ fn urls() -> UrlMap { } } -fn settings() -> Settings { +fn settings() -> Settings { Settings { installed_apps: &[&polls::APP, &admin::APP], ..Settings::default() diff --git a/src/bin/tosin-admin.rs b/src/bin/tosin-admin.rs index b4fc1d4..0f19795 100644 --- a/src/bin/tosin-admin.rs +++ b/src/bin/tosin-admin.rs @@ -9,15 +9,14 @@ use structopt::StructOpt; const TOSIN_DEPENDENCY: &str = concat!( r#"tosin = { path = '"#, env!("CARGO_MANIFEST_DIR"), - r#"' } -diesel = { version = "1.4.4", features = ["sqlite"] }"# + r#"', features = ["sqlite"] } +diesel = "1.4.4""# ); const PROJECT_MAIN: &str = r#" #[macro_use] extern crate diesel; use tosin::contrib::admin; -use tosin::db::backend::Connectable; use tosin::urls::{url_map, UrlMap}; use tosin::Settings; @@ -27,7 +26,7 @@ fn urls() -> UrlMap { } } -fn settings() -> Settings { +fn settings() -> Settings { Settings { ..Settings::default() } diff --git a/src/cli/make_migrations.rs b/src/cli/make_migrations.rs index b0cd341..e0653b6 100644 --- a/src/cli/make_migrations.rs +++ b/src/cli/make_migrations.rs @@ -7,7 +7,7 @@ use structopt::StructOpt; use crate::db::migration::{CreateModelOption, DatabaseChange, Migration}; use crate::db::models::{Field, ModelMeta}; -use crate::{db::backend::Connectable, Settings, UrlMap}; +use crate::{Settings, UrlMap}; #[derive(StructOpt)] /// Generate migrations @@ -101,7 +101,7 @@ impl From<&[Migration]> for AppTablesState { } impl MakeMigrations { - pub fn execute(self, _urls: UrlMap, settings: Settings) { + pub fn execute(self, _urls: UrlMap, settings: Settings) { for app in settings.installed_apps { let expected_table_state = AppTablesState::from(app.models); let actual_table_state = AppTablesState::from(app.migrations); diff --git a/src/cli/migrate.rs b/src/cli/migrate.rs index acc59d3..4085fe1 100644 --- a/src/cli/migrate.rs +++ b/src/cli/migrate.rs @@ -1,6 +1,5 @@ use structopt::StructOpt; -use crate::db::backend::Connectable; use crate::db::migration::{CreateModelOption, DatabaseChange}; use crate::db::models::Field; use crate::{Settings, UrlMap}; @@ -29,15 +28,15 @@ const CREATE_MIGRATION_TABLE: DatabaseChange = DatabaseChange::CreateModel { }; impl Migrate { - pub fn execute(self, _urls: UrlMap, settings: Settings) { + pub fn execute(self, _urls: UrlMap, settings: Settings) { let database = settings.database; let connection = database.connect().unwrap(); - CREATE_MIGRATION_TABLE.apply::("tosin_meta", &connection); + CREATE_MIGRATION_TABLE.apply("tosin_meta", &connection); for app in settings.installed_apps { for migration in app.migrations { - migration.apply::(app.name, &connection); + migration.apply(app.name, &connection); } } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 5bff8ae..347430c 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,6 +1,6 @@ use structopt::StructOpt; -use crate::{db::backend::Connectable, Settings, UrlMap}; +use crate::{Settings, UrlMap}; mod make_migrations; mod migrate; @@ -14,7 +14,7 @@ enum Command { } impl Command { - fn execute(self, urls: UrlMap, settings: Settings) { + fn execute(self, urls: UrlMap, settings: Settings) { match self { Command::MakeMigrations(command) => command.execute(urls, settings), Command::Migrate(command) => command.execute(urls, settings), @@ -23,7 +23,7 @@ impl Command { } } -pub fn main(urls: UrlMap, settings: Settings) { +pub fn main(urls: UrlMap, settings: Settings) { let command = Command::from_args(); command.execute(urls, settings); } diff --git a/src/cli/run_server.rs b/src/cli/run_server.rs index d20ac14..242c824 100644 --- a/src/cli/run_server.rs +++ b/src/cli/run_server.rs @@ -1,6 +1,6 @@ use structopt::StructOpt; -use crate::{db::backend::Connectable, Settings, UrlMap}; +use crate::{Settings, UrlMap}; #[derive(StructOpt)] /// Run an HTTP server @@ -11,7 +11,7 @@ pub struct RunServer { } impl RunServer { - pub fn execute(self, urls: UrlMap, _settings: Settings) { + pub fn execute(self, urls: UrlMap, _settings: Settings) { println!("Starting server at http://127.0.0.1:{}/", self.port); let server_task = warp::serve(urls).run(([127, 0, 0, 1], self.port)); diff --git a/src/db/backend.rs b/src/db/backend.rs deleted file mode 100644 index 5950747..0000000 --- a/src/db/backend.rs +++ /dev/null @@ -1,27 +0,0 @@ -use barrel::backend::SqlGenerator; -pub use diesel::connection::Connection; - -pub trait UsableBackend: diesel::backend::Backend where *const str: diesel::deserialize::FromSql {} -impl UsableBackend for diesel::sqlite::Sqlite {} - -pub trait UsableConnection: Connection where ::Backend: UsableBackend {} -impl UsableConnection for diesel::sqlite::SqliteConnection {} - -pub trait Connectable { - type Connection: UsableConnection; - type SqlGenerator: SqlGenerator; - fn connect(&self) -> diesel::ConnectionResult; -} - -pub struct Sqlite { - pub db_file: &'static str, -} - -impl Connectable for Sqlite { - type Connection = diesel::sqlite::SqliteConnection; - type SqlGenerator = barrel::backend::Sqlite; - - fn connect(&self) -> diesel::ConnectionResult { - Self::Connection::establish(self.db_file) - } -} diff --git a/src/db/backend/mod.rs b/src/db/backend/mod.rs new file mode 100644 index 0000000..b266f1e --- /dev/null +++ b/src/db/backend/mod.rs @@ -0,0 +1,7 @@ +#[cfg(feature = "sqlite")] +mod sqlite; +#[cfg(feature = "sqlite")] +pub use sqlite::*; + +#[cfg(not(feature = "sqlite"))] +compile_error!("pick a backend pls"); diff --git a/src/db/backend/sqlite.rs b/src/db/backend/sqlite.rs new file mode 100644 index 0000000..002d0d3 --- /dev/null +++ b/src/db/backend/sqlite.rs @@ -0,0 +1,22 @@ +use diesel::connection::Connection as _; + +pub use barrel::backend::Sqlite as SqlGenerator; +pub use diesel::sqlite::SqliteConnection as Connection; + +pub struct Database { + pub db_file: &'static str, +} + +impl Database { + pub fn connect(&self) -> diesel::ConnectionResult { + Connection::establish(self.db_file) + } +} + +impl Default for Database { + fn default() -> Self { + Self { + db_file: "db.sqlite3", + } + } +} diff --git a/src/db/migration/change.rs b/src/db/migration/change.rs index 8bb8bf6..a07ef24 100644 --- a/src/db/migration/change.rs +++ b/src/db/migration/change.rs @@ -1,6 +1,6 @@ -use diesel::Connection; +use diesel::Connection as _; -use crate::db::backend::Connectable; +use crate::db::backend::{Connection, SqlGenerator}; use crate::db::models::Field; pub enum DatabaseChange { @@ -12,7 +12,7 @@ pub enum DatabaseChange { } impl DatabaseChange { - pub fn apply(&self, app_name: &str, connection: &C::Connection) { + pub fn apply(&self, app_name: &str, connection: &Connection) { use barrel::{types, Migration, Table}; match self { @@ -55,7 +55,7 @@ impl DatabaseChange { m.create_table(table_name, callback); } - connection.execute(&m.make::()).unwrap(); + connection.execute(&m.make::()).unwrap(); } } } diff --git a/src/db/migration/mod.rs b/src/db/migration/mod.rs index 839608f..e6b3266 100644 --- a/src/db/migration/mod.rs +++ b/src/db/migration/mod.rs @@ -1,8 +1,8 @@ pub use tosin_macros::gather_migrations as gather; -use diesel::{result::Error as DieselError, Connection}; +use diesel::{result::Error as DieselError, Connection as _}; -use crate::db::backend::Connectable; +use crate::db::backend::Connection; mod change; @@ -16,12 +16,12 @@ pub struct Migration { } impl Migration { - pub fn apply(&self, app_name: &str, connection: &C::Connection) { + pub fn apply(&self, app_name: &str, connection: &Connection) { // TODO prevent double-applying connection .transaction::<_, DieselError, _>(|| { for change in self.changes { - change.apply::(app_name, connection); + change.apply(app_name, connection); } Ok(()) diff --git a/src/settings.rs b/src/settings.rs index 4b140a6..0ae1136 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -1,17 +1,15 @@ -use crate::db::backend::{self, Connectable}; +use crate::db::backend::Database; -pub struct Settings { +pub struct Settings { pub installed_apps: &'static [&'static crate::apps::AppConfig], - pub database: C, + pub database: Database, } -impl Default for Settings { +impl Default for Settings { fn default() -> Self { Self { installed_apps: &[], - database: backend::Sqlite { - db_file: "db.sqlite3", - }, + database: Database::default(), } } } diff --git a/tests/tutorial/mod.rs b/tests/tutorial/mod.rs index f101283..aeb3135 100644 --- a/tests/tutorial/mod.rs +++ b/tests/tutorial/mod.rs @@ -137,7 +137,6 @@ pub fn urls() -> UrlMap { extern crate diesel; use tosin::contrib::admin; -use tosin::db::backend::Connectable; use tosin::urls::{url_map, UrlMap}; use tosin::Settings; @@ -150,7 +149,7 @@ fn urls() -> UrlMap { } } -fn settings() -> Settings { +fn settings() -> Settings { Settings { ..Settings::default() } @@ -208,7 +207,6 @@ pub fn step2() { use tosin::Settings; use tosin::contrib::admin; -use tosin::db::backend::Connectable; use tosin::urls::{UrlMap, url_map}; mod polls; @@ -220,7 +218,7 @@ fn urls() -> UrlMap { } } -fn settings() -> Settings { +fn settings() -> Settings { Settings { installed_apps: &[ &admin::APP, @@ -254,8 +252,8 @@ edition = "2018" [dependencies] chrono = "0.4" -diesel = { version = "1.4.4", features = ["sqlite"] } -tosin = { path = "../../.." } +diesel = "1.4.4" +tosin = { path = "../../..", features = ["sqlite"] } "#, ) .unwrap(); @@ -299,7 +297,6 @@ gather!(); use tosin::Settings; use tosin::contrib::admin; -use tosin::db::backend::Connectable; use tosin::urls::{UrlMap, url_map}; mod polls; @@ -311,7 +308,7 @@ fn urls() -> UrlMap { } } -fn settings() -> Settings { +fn settings() -> Settings { Settings { installed_apps: &[ &polls::APP, -- cgit v1.2.3