diff --git a/Cargo.lock b/Cargo.lock index 1690174..ec8d1ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -146,6 +146,14 @@ dependencies = [ "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "codegen" version = "0.1.0" @@ -699,6 +707,31 @@ dependencies = [ "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "rand" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "redox_syscall" version = "0.1.40" @@ -781,6 +814,7 @@ dependencies = [ "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "seahash 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1343,6 +1377,7 @@ dependencies = [ "checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18" "checksum chrono 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a81892f0d5a53f46fc05ef0b917305a81c13f1f13bb59ac91ff595817f0764b1" "checksum clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536" +"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum crossbeam-deque 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fe8153ef04a7594ded05b427ffad46ddeaf22e63fd48d42b3e1e3bb4db07cae7" "checksum crossbeam-epoch 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2af0e75710d6181e234c8ecc79f14a97907850a541b13b0be1dd10992f2e4620" "checksum crossbeam-utils 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d636a8b3bcc1b409d7ffd3facef8f21dcb4009626adbd0c5e6c4305c07253c7b" @@ -1410,6 +1445,9 @@ dependencies = [ "checksum r2d2-diesel 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "eb9c29bad92da76d02bc2c020452ebc3a3fe6fa74cfab91e711c43116e4fb1a3" "checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" +"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" +"checksum rand_core 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1961a422c4d189dfb50ffa9320bf1f2a9bd54ecb92792fb9477f99a1045f3372" +"checksum rand_core 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0905b6b7079ec73b314d4c748701f6931eb79fd97c668caa3f1899b22b32c6db" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" diff --git a/Cargo.toml b/Cargo.toml index 6b2a794..e8ba98c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ tokio-io = "0.1" tokio-proto = "0.1" tokio-service = "0.1" serde_plain = "0.3.0" +rand = "0.5.5" [dependencies.codegen] path = "libs/codegen" diff --git a/src/lib.rs b/src/lib.rs index 28240a4..0967154 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ extern crate percent_encoding; extern crate pulldown_cmark; extern crate r2d2_diesel; extern crate r2d2; +extern crate rand; extern crate seahash; extern crate serde_json; extern crate serde_urlencoded; diff --git a/src/resources/new_article_resource.rs b/src/resources/new_article_resource.rs index 40f2fb0..5cb0a30 100644 --- a/src/resources/new_article_resource.rs +++ b/src/resources/new_article_resource.rs @@ -36,7 +36,7 @@ struct CreateArticle { base_revision: String, title: String, body: String, - theme: Theme, + theme: Option, } impl NewArticleResource { @@ -148,7 +148,8 @@ impl Resource for NewArticleResource { if arg.base_revision != NEW { unimplemented!("Version update conflict"); } - self.state.create_article(self.slug.clone(), arg.title, arg.body, identity, arg.theme) + let theme = arg.theme.unwrap_or_else(theme::random); + self.state.create_article(self.slug.clone(), arg.title, arg.body, identity, theme) }) .and_then(|updated| { futures::finished(Response::new() @@ -193,7 +194,8 @@ impl Resource for NewArticleResource { if arg.base_revision != NEW { unimplemented!("Version update conflict"); } - self.state.create_article(self.slug.clone(), arg.title, arg.body, identity, arg.theme) + let theme = arg.theme.unwrap_or_else(theme::random); + self.state.create_article(self.slug.clone(), arg.title, arg.body, identity, theme) }) .and_then(|updated| { futures::finished(Response::new() diff --git a/src/theme.rs b/src/theme.rs index 7dae7a4..19604b4 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -5,6 +5,7 @@ use diesel::deserialize::{self, FromSql}; use diesel::serialize::{self, Output, ToSql}; use diesel::sql_types::Text; use diesel::sqlite::Sqlite; +use rand; use seahash; #[derive(Debug, PartialEq, Eq, Clone, Copy)] @@ -49,6 +50,12 @@ pub fn theme_from_str_hash(x: &str) -> Theme { THEMES[choice] } +pub fn random() -> Theme { + use rand::Rng; + *rand::thread_rng().choose(&THEMES) + .expect("Could only fail for an empty slice") +} + impl ToSql for Theme { fn to_sql(&self, out: &mut Output) -> serialize::Result { ToSql::::to_sql(&self.to_string(), out)