Skip to content

Use #[diesel::dsl::auto_type] in some places #8970

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
Jul 3, 2024
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
7 changes: 4 additions & 3 deletions src/models/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub struct Category {
}

type WithSlug<'a> = diesel::dsl::Eq<categories::slug, crate::sql::lower<&'a str>>;
type BySlug<'a> = diesel::dsl::Filter<categories::table, WithSlug<'a>>;

#[derive(Associations, Insertable, Identifiable, Debug, Clone, Copy)]
#[diesel(
Expand All @@ -36,8 +35,10 @@ impl Category {
categories::slug.eq(crate::sql::lower(slug))
}

pub fn by_slug(slug: &str) -> BySlug<'_> {
categories::table.filter(Self::with_slug(slug))
#[dsl::auto_type(no_type_alias)]
pub fn by_slug<'a>(slug: &'a str) -> _ {
let filter: WithSlug<'a> = Self::with_slug(slug);
categories::table.filter(filter)
}

pub fn update_crate(
Expand Down
16 changes: 10 additions & 6 deletions src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::BTreeMap;

use chrono::NaiveDateTime;
use diesel::associations::Identifiable;
use diesel::dsl;
use diesel::pg::Pg;
use diesel::prelude::*;
use diesel::sql_types::{Bool, Text};
Expand Down Expand Up @@ -80,8 +81,6 @@ pub const MAX_NAME_LENGTH: usize = 64;

type All = diesel::dsl::Select<crates::table, diesel::dsl::AsSelect<Crate, diesel::pg::Pg>>;
type WithName<'a> = diesel::dsl::Eq<canon_crate_name<crates::name>, canon_crate_name<&'a str>>;
type ByName<'a> = diesel::dsl::Filter<All, WithName<'a>>;
type ByExactName<'a> = diesel::dsl::Filter<All, diesel::dsl::Eq<crates::name, &'a str>>;

#[derive(Insertable, AsChangeset, Default, Debug)]
#[diesel(
Expand Down Expand Up @@ -170,12 +169,17 @@ impl Crate {
canon_crate_name(crates::name).eq(canon_crate_name(name))
}

pub fn by_name(name: &str) -> ByName<'_> {
Crate::all().filter(Self::with_name(name))
#[dsl::auto_type(no_type_alias)]
pub fn by_name<'a>(name: &'a str) -> _ {
let all: All = Crate::all();
let filter: WithName<'a> = Self::with_name(name);
all.filter(filter)
}

pub fn by_exact_name(name: &str) -> ByExactName<'_> {
Crate::all().filter(crates::name.eq(name))
#[dsl::auto_type(no_type_alias)]
pub fn by_exact_name<'a>(name: &'a str) -> _ {
let all: All = Crate::all();
all.filter(crates::name.eq(name))
}

pub fn all() -> All {
Expand Down