Skip to content

Use derive_deref #1235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ docopt = "0.8.1"
itertools = "0.6.0"
lettre = "0.6"
scheduled-thread-pool = "0.2.0"
derive_deref = "1.0.0"

conduit = "0.8"
conduit-conditional-get = "0.8"
Expand Down
6 changes: 3 additions & 3 deletions src/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ pub fn publish(req: &mut Request) -> CargoResult<Response> {
let keywords = new_crate
.keywords
.as_ref()
.map(|kws| kws.iter().map(|kw| &**kw).collect())
.map(|kws| kws.iter().map(|kw| &***kw).collect())
.unwrap_or_else(Vec::new);

let categories = new_crate.categories.as_ref().map(|s| &s[..]).unwrap_or(&[]);
let categories: Vec<_> = categories.iter().map(|k| &**k).collect();
let categories: Vec<_> = categories.iter().map(|k| &***k).collect();

let conn = req.db_conn()?;
// Create a transaction on the database, if there are no errors,
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn publish(req: &mut Request) -> CargoResult<Response> {
));
}

if krate.name != name {
if &krate.name != name {
return Err(human(&format_args!(
"crate was previously named `{}`",
krate.name
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ extern crate chrono;
extern crate comrak;
extern crate curl;
#[macro_use]
extern crate derive_deref;
#[macro_use]
extern crate diesel;
extern crate diesel_full_text_search;
extern crate dotenv;
Expand Down
74 changes: 9 additions & 65 deletions src/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! to and from structs. The serlializing is only utilised in
//! integration tests.
use std::collections::HashMap;
use std::ops::Deref;

use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use semver;
Expand Down Expand Up @@ -32,21 +31,21 @@ pub struct NewCrate {
pub badges: Option<HashMap<String, HashMap<String, String>>>,
}

#[derive(PartialEq, Eq, Hash, Serialize, Debug)]
#[derive(PartialEq, Eq, Hash, Serialize, Debug, Deref)]
pub struct CrateName(pub String);
#[derive(Debug)]
#[derive(Debug, Deref)]
pub struct CrateVersion(pub semver::Version);
#[derive(Debug)]
#[derive(Debug, Deref)]
pub struct CrateVersionReq(pub semver::VersionReq);
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Deref)]
pub struct KeywordList(pub Vec<Keyword>);
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Deref)]
pub struct Keyword(pub String);
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Deref)]
pub struct CategoryList(pub Vec<Category>);
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Deref)]
pub struct Category(pub String);
#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Deref)]
pub struct Feature(pub String);

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -177,6 +176,7 @@ impl<'de> Deserialize<'de> for CategoryList {
}
}
}

impl Serialize for CrateVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -195,62 +195,6 @@ impl Serialize for CrateVersionReq {
}
}

impl Deref for CrateName {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}

impl Deref for Keyword {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}

impl Deref for Category {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}

impl Deref for Feature {
type Target = str;
fn deref(&self) -> &str {
&self.0
}
}

impl Deref for CrateVersion {
type Target = semver::Version;
fn deref(&self) -> &semver::Version {
&self.0
}
}

impl Deref for CrateVersionReq {
type Target = semver::VersionReq;
fn deref(&self) -> &semver::VersionReq {
&self.0
}
}

impl Deref for KeywordList {
type Target = [Keyword];
fn deref(&self) -> &[Keyword] {
&self.0
}
}

impl Deref for CategoryList {
type Target = [Category];
fn deref(&self) -> &[Category] {
&self.0
}
}

use diesel::pg::Pg;
use diesel::types::{IsNull, Text, ToSql, ToSqlOutput};
use std::error::Error;
Expand Down