diff options
-rw-r--r-- | examples/tutorial01/main.rs | 7 | ||||
-rw-r--r-- | examples/tutorial01/polls/migrations/mod.rs | 2 | ||||
-rw-r--r-- | examples/tutorial01/polls/models.rs | 2 | ||||
-rw-r--r-- | examples/tutorial01/polls/urls.rs | 2 | ||||
-rw-r--r-- | examples/tutorial02/main.rs | 19 | ||||
-rw-r--r-- | examples/tutorial02/polls/migrations/mod.rs | 2 | ||||
-rw-r--r-- | examples/tutorial02/polls/models.rs | 6 | ||||
-rw-r--r-- | examples/tutorial02/polls/urls.rs | 2 | ||||
-rw-r--r-- | src/bin/tosin-admin.rs | 13 | ||||
-rw-r--r-- | src/cli/make_migrations.rs | 44 | ||||
-rw-r--r-- | src/cli/migrate.rs | 11 | ||||
-rw-r--r-- | src/cli/mod.rs | 2 | ||||
-rw-r--r-- | src/cli/run_server.rs | 2 | ||||
-rw-r--r-- | src/contrib/admin/migrations/mod.rs | 2 | ||||
-rw-r--r-- | src/contrib/admin/models.rs | 6 | ||||
-rw-r--r-- | src/contrib/admin/site/urls.rs | 2 | ||||
-rw-r--r-- | src/db/migration/change.rs | 43 | ||||
-rw-r--r-- | src/db/migration/mod.rs | 16 | ||||
-rw-r--r-- | src/db/mod.rs | 2 | ||||
-rw-r--r-- | src/db/models/mod.rs | 2 | ||||
-rw-r--r-- | src/http.rs | 2 | ||||
-rw-r--r-- | tests/tutorial/mod.rs | 9 |
22 files changed, 108 insertions, 90 deletions
diff --git a/examples/tutorial01/main.rs b/examples/tutorial01/main.rs index 52ae922..af2f14d 100644 --- a/examples/tutorial01/main.rs +++ b/examples/tutorial01/main.rs @@ -1,9 +1,10 @@ -#[macro_use] extern crate diesel; +#[macro_use] +extern crate diesel; -use tosin::Settings; use tosin::contrib::admin; use tosin::db::backend::Connectable; -use tosin::urls::{UrlMap, url_map}; +use tosin::urls::{url_map, UrlMap}; +use tosin::Settings; mod polls; diff --git a/examples/tutorial01/polls/migrations/mod.rs b/examples/tutorial01/polls/migrations/mod.rs index 64efe8d..56fe76d 100644 --- a/examples/tutorial01/polls/migrations/mod.rs +++ b/examples/tutorial01/polls/migrations/mod.rs @@ -1,3 +1,3 @@ -use tosin::db::migration::{Migration, gather}; +use tosin::db::migration::{gather, Migration}; gather!(); diff --git a/examples/tutorial01/polls/models.rs b/examples/tutorial01/polls/models.rs index bc1cd9b..987e4b0 100644 --- a/examples/tutorial01/polls/models.rs +++ b/examples/tutorial01/polls/models.rs @@ -1,5 +1,5 @@ #[allow(unused_imports)] -use tosin::db::models::{Model, Id, gather}; +use tosin::db::models::{gather, Id, Model}; // TODO define models diff --git a/examples/tutorial01/polls/urls.rs b/examples/tutorial01/polls/urls.rs index 04d93cc..83f3ecb 100644 --- a/examples/tutorial01/polls/urls.rs +++ b/examples/tutorial01/polls/urls.rs @@ -1,4 +1,4 @@ -use tosin::urls::{UrlMap, url_map}; +use tosin::urls::{url_map, UrlMap}; use super::views; diff --git a/examples/tutorial02/main.rs b/examples/tutorial02/main.rs index bac20e1..2abac0c 100644 --- a/examples/tutorial02/main.rs +++ b/examples/tutorial02/main.rs @@ -1,9 +1,10 @@ -#[macro_use] extern crate diesel; +#[macro_use] +extern crate diesel; -use tosin::Settings; use tosin::contrib::admin; use tosin::db::backend::Connectable; -use tosin::urls::{UrlMap, url_map}; +use tosin::urls::{url_map, UrlMap}; +use tosin::Settings; mod polls; @@ -16,10 +17,7 @@ fn urls() -> UrlMap { fn settings() -> Settings<impl Connectable> { Settings { - installed_apps: &[ - &polls::APP, - &admin::APP, - ], + installed_apps: &[&polls::APP, &admin::APP], ..Settings::default() } } @@ -33,7 +31,7 @@ mod test { #[test] fn test_models() { - use polls::models::{Choice, Question, choice, question}; + use polls::models::{choice, question, Choice, Question}; let settings = settings(); let connection = settings.database.connect().unwrap(); @@ -43,10 +41,7 @@ mod test { assert!(all_questions.is_empty()); // make a new one - let mut q = Question::new( - "What's new?".to_string(), - time::PrimitiveDateTime::now(), - ); + let mut q = Question::new("What's new?".to_string(), time::PrimitiveDateTime::now()); // save it q.save_mut(); // it's got an id now! diff --git a/examples/tutorial02/polls/migrations/mod.rs b/examples/tutorial02/polls/migrations/mod.rs index 64efe8d..56fe76d 100644 --- a/examples/tutorial02/polls/migrations/mod.rs +++ b/examples/tutorial02/polls/migrations/mod.rs @@ -1,3 +1,3 @@ -use tosin::db::migration::{Migration, gather}; +use tosin::db::migration::{gather, Migration}; gather!(); diff --git a/examples/tutorial02/polls/models.rs b/examples/tutorial02/polls/models.rs index c9de156..48e58cc 100644 --- a/examples/tutorial02/polls/models.rs +++ b/examples/tutorial02/polls/models.rs @@ -1,9 +1,9 @@ -use tosin::db::models::{Model, Id, gather}; +use tosin::db::models::{gather, Id, Model}; #[derive(Model, PartialEq, Debug)] pub struct Question { id: Option<Id>, - #[model(max_length=200)] + #[model(max_length = 200)] question_text: String, /// date published pub_date: chrono::NaiveDateTime, @@ -14,7 +14,7 @@ pub struct Choice { id: Option<Id>, #[model(Question, on_delete=Cascade)] question: Id, - #[model(max_length=200)] + #[model(max_length = 200)] choice_text: String, #[model(default = 0)] votes: usize, diff --git a/examples/tutorial02/polls/urls.rs b/examples/tutorial02/polls/urls.rs index 04d93cc..83f3ecb 100644 --- a/examples/tutorial02/polls/urls.rs +++ b/examples/tutorial02/polls/urls.rs @@ -1,4 +1,4 @@ -use tosin::urls::{UrlMap, url_map}; +use tosin::urls::{url_map, UrlMap}; use super::views; diff --git a/src/bin/tosin-admin.rs b/src/bin/tosin-admin.rs index e108207..b4fc1d4 100644 --- a/src/bin/tosin-admin.rs +++ b/src/bin/tosin-admin.rs @@ -13,12 +13,13 @@ const TOSIN_DEPENDENCY: &str = concat!( diesel = { version = "1.4.4", features = ["sqlite"] }"# ); const PROJECT_MAIN: &str = r#" -#[macro_use] extern crate diesel; +#[macro_use] +extern crate diesel; -use tosin::Settings; use tosin::contrib::admin; use tosin::db::backend::Connectable; -use tosin::urls::{UrlMap, url_map}; +use tosin::urls::{url_map, UrlMap}; +use tosin::Settings; fn urls() -> UrlMap { url_map! { @@ -53,20 +54,20 @@ pub const APP: AppConfig = AppConfig { }; "#; const APP_MIGRATIONS: &str = r#" -use tosin::db::migration::{Migration, gather}; +use tosin::db::migration::{gather, Migration}; gather!(); "#; const APP_MODELS: &str = r#" #[allow(unused_imports)] -use tosin::db::models::{Model, Id, gather}; +use tosin::db::models::{gather, Id, Model}; // TODO define models gather!(); "#; const APP_URLS: &str = r#" -use tosin::urls::{UrlMap, url_map}; +use tosin::urls::{url_map, UrlMap}; use super::views; diff --git a/src/cli/make_migrations.rs b/src/cli/make_migrations.rs index 83e760c..b0cd341 100644 --- a/src/cli/make_migrations.rs +++ b/src/cli/make_migrations.rs @@ -5,14 +5,13 @@ use std::path::PathBuf; use quote::{quote, ToTokens}; use structopt::StructOpt; -use crate::{Settings, UrlMap, db::backend::Connectable}; -use crate::db::migration::{Migration, DatabaseChange, CreateModelOption}; +use crate::db::migration::{CreateModelOption, DatabaseChange, Migration}; use crate::db::models::{Field, ModelMeta}; +use crate::{db::backend::Connectable, Settings, UrlMap}; #[derive(StructOpt)] /// Generate migrations -pub struct MakeMigrations { -} +pub struct MakeMigrations {} #[derive(Debug)] struct AppTablesState { @@ -35,7 +34,7 @@ impl AppTablesState { Field::CharField { name, max_length: None } => quote! { Field::CharField { name: #name, max_length: None } }, Field::DateTimeField { name } => quote! { Field::DateTimeField { name: #name } }, - + Field::IntField { name } => quote! { Field::IntField { name: #name } }, } }).collect::<Vec<_>>(); @@ -58,7 +57,12 @@ impl From<&[ModelMeta]> for AppTablesState { fn from(models: &[ModelMeta]) -> Self { let mut db = HashMap::new(); for model in models { - db.insert(model.name, TableState { fields: model.fields.into() }); + db.insert( + model.name, + TableState { + fields: model.fields.into(), + }, + ); } Self { db } } @@ -70,7 +74,11 @@ impl From<&[Migration]> for AppTablesState { for migration in migrations { for change in migration.changes { match change { - DatabaseChange::CreateModel { name, fields, options } => { + DatabaseChange::CreateModel { + name, + fields, + options, + } => { if db.contains_key(name) { if options.contains(&CreateModelOption::IfNotExist) { continue; @@ -78,7 +86,12 @@ impl From<&[Migration]> for AppTablesState { panic!("double-created table {}", name); } } - db.insert(*name, TableState { fields: (*fields).into() }); + db.insert( + *name, + TableState { + fields: (*fields).into(), + }, + ); } } } @@ -92,10 +105,17 @@ impl MakeMigrations { for app in settings.installed_apps { let expected_table_state = AppTablesState::from(app.models); let actual_table_state = AppTablesState::from(app.migrations); - let next_id = app.migrations.iter().map(|m| m.id).max().map_or(1, |x| x + 1); + let next_id = app + .migrations + .iter() + .map(|m| m.id) + .max() + .map_or(1, |x| x + 1); let name = "auto"; // TODO names let changes = actual_table_state.changes_to(expected_table_state); - if changes.is_empty() { continue; } + if changes.is_empty() { + continue; + } let migration = quote! { Migration { id: #next_id, @@ -111,9 +131,7 @@ impl MakeMigrations { }; let file_name = format!("m_{:04}_{}.rs", next_id, name); let file_text = file.into_token_stream().to_string(); - let app_folder = app.name.split("::") - .skip(1) - .collect::<PathBuf>(); + let app_folder = app.name.split("::").skip(1).collect::<PathBuf>(); // TODO don't explode if running in a weird place let file_path = PathBuf::from("src") .join(app_folder) diff --git a/src/cli/migrate.rs b/src/cli/migrate.rs index a6efbb5..acc59d3 100644 --- a/src/cli/migrate.rs +++ b/src/cli/migrate.rs @@ -1,14 +1,13 @@ use structopt::StructOpt; -use crate::{Settings, UrlMap}; use crate::db::backend::Connectable; -use crate::db::migration::{DatabaseChange, CreateModelOption}; +use crate::db::migration::{CreateModelOption, DatabaseChange}; use crate::db::models::Field; +use crate::{Settings, UrlMap}; #[derive(StructOpt)] /// Perform database migrations -pub struct Migrate { -} +pub struct Migrate {} const CREATE_MIGRATION_TABLE: DatabaseChange = DatabaseChange::CreateModel { name: "Migration", @@ -24,9 +23,7 @@ const CREATE_MIGRATION_TABLE: DatabaseChange = DatabaseChange::CreateModel { name: "migration_name", max_length: None, }, - Field::DateTimeField { - name: "applied_at", - } + Field::DateTimeField { name: "applied_at" }, ], options: &[CreateModelOption::IfNotExist], }; diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 851812f..5bff8ae 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,6 +1,6 @@ use structopt::StructOpt; -use crate::{Settings, UrlMap, db::backend::Connectable}; +use crate::{db::backend::Connectable, Settings, UrlMap}; mod make_migrations; mod migrate; diff --git a/src/cli/run_server.rs b/src/cli/run_server.rs index 66e6fe1..d20ac14 100644 --- a/src/cli/run_server.rs +++ b/src/cli/run_server.rs @@ -1,6 +1,6 @@ use structopt::StructOpt; -use crate::{Settings, UrlMap, db::backend::Connectable}; +use crate::{db::backend::Connectable, Settings, UrlMap}; #[derive(StructOpt)] /// Run an HTTP server diff --git a/src/contrib/admin/migrations/mod.rs b/src/contrib/admin/migrations/mod.rs index 5ecbb82..01e246b 100644 --- a/src/contrib/admin/migrations/mod.rs +++ b/src/contrib/admin/migrations/mod.rs @@ -1,3 +1,3 @@ -use crate::db::migration::{Migration, gather}; +use crate::db::migration::{gather, Migration}; gather!(); diff --git a/src/contrib/admin/models.rs b/src/contrib/admin/models.rs index 7fde44a..eb85f7b 100644 --- a/src/contrib/admin/models.rs +++ b/src/contrib/admin/models.rs @@ -1,6 +1,4 @@ #[allow(unused_imports)] -use crate::db::models::{Model, Id, ModelMeta}; +use crate::db::models::{Id, Model, ModelMeta}; -pub const ALL: &[ModelMeta] = &[ - -]; +pub const ALL: &[ModelMeta] = &[]; diff --git a/src/contrib/admin/site/urls.rs b/src/contrib/admin/site/urls.rs index d6537bc..72b65d2 100644 --- a/src/contrib/admin/site/urls.rs +++ b/src/contrib/admin/site/urls.rs @@ -1,5 +1,5 @@ use crate::http::Reply; -use crate::urls::{UrlMap, url_map}; +use crate::urls::{url_map, UrlMap}; pub fn urls() -> UrlMap { url_map! { diff --git a/src/db/migration/change.rs b/src/db/migration/change.rs index b5d0dd5..8bb8bf6 100644 --- a/src/db/migration/change.rs +++ b/src/db/migration/change.rs @@ -7,34 +7,39 @@ pub enum DatabaseChange { CreateModel { name: &'static str, fields: &'static [Field], - options: &'static [CreateModelOption] + options: &'static [CreateModelOption], }, } impl DatabaseChange { pub fn apply<C: Connectable>(&self, app_name: &str, connection: &C::Connection) { - use barrel::{Migration, Table, types}; + use barrel::{types, Migration, Table}; match self { - DatabaseChange::CreateModel { name, fields, options } => { + DatabaseChange::CreateModel { + name, + fields, + options, + } => { let mut m = Migration::new(); - let columns: Vec<(&'static str, _)> = fields.iter().map(|field| match field { - Field::CharField { name, max_length } => { - let name = *name; - let _type = match max_length { - None => types::text(), - Some(max_length) => types::varchar(*max_length), - }; - (name, _type) - } - Field::DateTimeField { name } => { - (*name, types::text()) // TODO do smart things on non-sqlite - } - Field::IntField { name } => { - (*name, types::integer()) - } - }).collect(); + let columns: Vec<(&'static str, _)> = fields + .iter() + .map(|field| match field { + Field::CharField { name, max_length } => { + let name = *name; + let _type = match max_length { + None => types::text(), + Some(max_length) => types::varchar(*max_length), + }; + (name, _type) + } + Field::DateTimeField { name } => { + (*name, types::text()) // TODO do smart things on non-sqlite + } + Field::IntField { name } => (*name, types::integer()), + }) + .collect(); let callback = move |t: &mut Table| { for (name, _type) in &columns { diff --git a/src/db/migration/mod.rs b/src/db/migration/mod.rs index d511f88..839608f 100644 --- a/src/db/migration/mod.rs +++ b/src/db/migration/mod.rs @@ -1,6 +1,6 @@ pub use tosin_macros::gather_migrations as gather; -use diesel::{Connection, result::Error as DieselError}; +use diesel::{result::Error as DieselError, Connection}; use crate::db::backend::Connectable; @@ -18,12 +18,14 @@ pub struct Migration { impl Migration { pub fn apply<C: Connectable>(&self, app_name: &str, connection: &C::Connection) { // TODO prevent double-applying - connection.transaction::<_, DieselError, _>(|| { - for change in self.changes { - change.apply::<C>(app_name, connection); - } + connection + .transaction::<_, DieselError, _>(|| { + for change in self.changes { + change.apply::<C>(app_name, connection); + } - Ok(()) - }).unwrap(); + Ok(()) + }) + .unwrap(); } } diff --git a/src/db/mod.rs b/src/db/mod.rs index e04fe06..dd21406 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,5 +2,5 @@ pub mod backend; pub mod migration; pub mod models; -pub use diesel::*; pub use diesel::backend as diesel_backend; +pub use diesel::*; diff --git a/src/db/models/mod.rs b/src/db/models/mod.rs index 3f39ccd..4133cf2 100644 --- a/src/db/models/mod.rs +++ b/src/db/models/mod.rs @@ -1,4 +1,4 @@ -pub use tosin_macros::{Model, gather_models as gather}; +pub use tosin_macros::{gather_models as gather, Model}; mod meta; pub use meta::*; diff --git a/src/http.rs b/src/http.rs index f8f1066..59278cd 100644 --- a/src/http.rs +++ b/src/http.rs @@ -1 +1 @@ -pub use warp::{Filter, Reply, reply::Response}; +pub use warp::{reply::Response, Filter, Reply}; diff --git a/tests/tutorial/mod.rs b/tests/tutorial/mod.rs index 5d969cf..f101283 100644 --- a/tests/tutorial/mod.rs +++ b/tests/tutorial/mod.rs @@ -116,7 +116,7 @@ pub fn index() -> Response { fs::write( "src/polls/urls.rs", r#" -use tosin::urls::{UrlMap, url_map}; +use tosin::urls::{url_map, UrlMap}; use super::views; @@ -133,12 +133,13 @@ pub fn urls() -> UrlMap { fs::write( "src/main.rs", r#" -#[macro_use] extern crate diesel; +#[macro_use] +extern crate diesel; -use tosin::Settings; use tosin::contrib::admin; use tosin::db::backend::Connectable; -use tosin::urls::{UrlMap, url_map}; +use tosin::urls::{url_map, UrlMap}; +use tosin::Settings; mod polls; |