diff options
| author | Melody Horn <melody@boringcactus.com> | 2021-06-29 22:44:11 -0600 | 
|---|---|---|
| committer | Melody Horn <melody@boringcactus.com> | 2021-06-29 22:44:11 -0600 | 
| commit | 9829bbfcd57c57e237c6aacb96a78d0b9f5bab68 (patch) | |
| tree | 43dc643d7b592d9abc4c5f89121e249ccb597d7d /src | |
| parent | 9dd7f2256a82f95096d928aaef70d15603152ebc (diff) | |
| download | tosin-9829bbfcd57c57e237c6aacb96a78d0b9f5bab68.tar.gz tosin-9829bbfcd57c57e237c6aacb96a78d0b9f5bab68.zip  | |
lol no generics
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/tosin-admin.rs | 7 | ||||
| -rw-r--r-- | src/cli/make_migrations.rs | 4 | ||||
| -rw-r--r-- | src/cli/migrate.rs | 7 | ||||
| -rw-r--r-- | src/cli/mod.rs | 6 | ||||
| -rw-r--r-- | src/cli/run_server.rs | 4 | ||||
| -rw-r--r-- | src/db/backend.rs | 27 | ||||
| -rw-r--r-- | src/db/backend/mod.rs | 7 | ||||
| -rw-r--r-- | src/db/backend/sqlite.rs | 22 | ||||
| -rw-r--r-- | src/db/migration/change.rs | 8 | ||||
| -rw-r--r-- | src/db/migration/mod.rs | 8 | ||||
| -rw-r--r-- | src/settings.rs | 12 | 
11 files changed, 55 insertions, 57 deletions
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<impl Connectable> { +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<impl Connectable>) { +    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<C: Connectable>(self, _urls: UrlMap, settings: Settings<C>) { +    pub fn execute(self, _urls: UrlMap, settings: Settings) {          let database = settings.database;          let connection = database.connect().unwrap(); -        CREATE_MIGRATION_TABLE.apply::<C>("tosin_meta", &connection); +        CREATE_MIGRATION_TABLE.apply("tosin_meta", &connection);          for app in settings.installed_apps {              for migration in app.migrations { -                migration.apply::<C>(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<impl Connectable>) { +    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<impl Connectable>) { +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<impl Connectable>) { +    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<diesel::sql_types::Text, Self> {} -impl UsableBackend for diesel::sqlite::Sqlite {} - -pub trait UsableConnection: Connection where <Self as Connection>::Backend: UsableBackend {} -impl UsableConnection for diesel::sqlite::SqliteConnection {} - -pub trait Connectable { -    type Connection: UsableConnection; -    type SqlGenerator: SqlGenerator; -    fn connect(&self) -> diesel::ConnectionResult<Self::Connection>; -} - -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> { -        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> { +        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<C: Connectable>(&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::<C::SqlGenerator>()).unwrap(); +                connection.execute(&m.make::<SqlGenerator>()).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<C: Connectable>(&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::<C>(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<C: Connectable> { +pub struct Settings {      pub installed_apps: &'static [&'static crate::apps::AppConfig], -    pub database: C, +    pub database: Database,  } -impl Default for Settings<backend::Sqlite> { +impl Default for Settings {      fn default() -> Self {          Self {              installed_apps: &[], -            database: backend::Sqlite { -                db_file: "db.sqlite3", -            }, +            database: Database::default(),          }      }  }  |