From 65ae51b7e51e821a28643de57f007bfc9faca6e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Sat, 25 May 2019 21:03:06 +0200 Subject: [PATCH] implement Host more completely by doing less we now use DieselNewType and Shrinkwrap to automatically derive all the things we need. --- plume-models/src/blogs.rs | 29 +++++------------------------ plume-models/src/lib.rs | 4 ++++ 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index 20c63f18..6c5a449d 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -25,30 +25,17 @@ use posts::Post; use safe_string::SafeString; use schema::blogs; use search::Searcher; -use std::fmt; use users::User; use {Connection, Error, PlumeRocket, Result}; pub type CustomGroup = CustomObject; -#[derive(Queryable, Clone)] +#[derive(Clone, Debug, DieselNewType, Shrinkwrap)] pub struct Host(String); impl Host { - pub fn new>(host: T) -> Host { - Host(host.into()) - } -} - -impl Into for Host { - fn into(self) -> String { - self.0.clone() - } -} - -impl From for Host { - fn from(s: String) -> Host { - Host::new(s) + pub fn new(host: impl ToString) -> Host { + Host(host.to_string()) } } @@ -58,12 +45,6 @@ impl AsRef for Host { } } -impl std::fmt::Display for Host { - fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) - } -} - #[derive(Queryable, Identifiable, Clone, AsChangeset)] #[changeset_options(treat_none_as_null = "true")] pub struct Blog { @@ -82,7 +63,7 @@ pub struct Blog { pub summary_html: SafeString, pub icon_id: Option, pub banner_id: Option, - pub custom_domain: Option, + pub custom_domain: Option, } #[derive(Default, Insertable)] @@ -341,7 +322,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Host { .get_one("Host") .and_then(|x| { if x != Instance::get_local().ok()?.public_domain { - Some(Host(x.to_string())) + Some(Host::new(x)) } else { None } diff --git a/plume-models/src/lib.rs b/plume-models/src/lib.rs index 2c30cb8b..5ed0cf3d 100644 --- a/plume-models/src/lib.rs +++ b/plume-models/src/lib.rs @@ -10,6 +10,8 @@ extern crate bcrypt; extern crate chrono; #[macro_use] extern crate diesel; +#[macro_use] +extern crate diesel_derive_newtype; extern crate guid_create; extern crate heck; extern crate itertools; @@ -31,6 +33,8 @@ extern crate serde_derive; #[macro_use] extern crate serde_json; #[macro_use] +extern crate shrinkwraprs; +#[macro_use] extern crate tantivy; extern crate url; extern crate webfinger;