Skip to content

Commit d4e16fe

Browse files
committed
ok_slug
1 parent e8f777c commit d4e16fe

File tree

9 files changed

+32
-56
lines changed

9 files changed

+32
-56
lines changed

lib/bencher_json/src/organization/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde::{
99
de::{self, Visitor},
1010
};
1111

12-
use crate::{ProjectSlug, UserUuid};
12+
use crate::{ProjectSlug, UserSlug, UserUuid};
1313

1414
pub mod claim;
1515
pub mod member;
@@ -23,7 +23,7 @@ crate::typed_slug::typed_slug!(OrganizationSlug, ResourceName);
2323
#[typeshare::typeshare]
2424
pub type OrganizationResourceId = ResourceId<OrganizationUuid, OrganizationSlug>;
2525

26-
// Create a personal organization for a user.
26+
// Create an organization for a user.
2727
impl From<UserUuid> for OrganizationUuid {
2828
fn from(uuid: UserUuid) -> Self {
2929
uuid::Uuid::from(uuid).into()
@@ -37,6 +37,13 @@ impl From<ProjectSlug> for OrganizationSlug {
3737
}
3838
}
3939

40+
/// Create an organization for a user.
41+
impl From<UserSlug> for OrganizationSlug {
42+
fn from(slug: UserSlug) -> Self {
43+
Self(slug.into())
44+
}
45+
}
46+
4047
#[derive(Debug, Clone, Deserialize, Serialize)]
4148
#[cfg_attr(feature = "schema", derive(JsonSchema))]
4249
pub struct JsonNewOrganization {

lib/bencher_schema/src/macros/slug.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@ use crate::{context::DbConnection, model::project::ProjectId};
44

55
pub type SlugExistsFn = dyn FnOnce(&mut DbConnection, Option<ProjectId>, &str) -> bool;
66

7-
pub fn validate_slug<S>(
7+
pub fn validate_slug<N, S>(
88
conn: &mut DbConnection,
99
project_id: Option<ProjectId>,
10-
name: S,
11-
slug: Option<Slug>,
10+
name: N,
11+
slug: Option<S>,
1212
exists: Box<SlugExistsFn>,
13-
) -> Slug
13+
) -> S
1414
where
15-
S: AsRef<str> + std::fmt::Display,
15+
N: AsRef<str> + std::fmt::Display,
16+
S: Into<Slug> + From<Slug>,
1617
{
18+
let slug = slug.map(Into::into);
1719
let new_slug = Slug::unwrap_or_new(name, slug);
1820
if exists(conn, project_id, new_slug.as_ref()) {
1921
new_slug.with_rand_suffix()
2022
} else {
2123
new_slug
2224
}
25+
.into()
2326
}
2427

2528
macro_rules! ok_slug {

lib/bencher_schema/src/model/organization/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -356,14 +356,8 @@ impl InsertOrganization {
356356

357357
pub fn from_json(conn: &mut DbConnection, organization: JsonNewOrganization) -> Self {
358358
let JsonNewOrganization { name, slug } = organization;
359-
let slug = ok_slug!(
360-
conn,
361-
&name,
362-
slug.map(Into::into),
363-
organization,
364-
QueryOrganization
365-
);
366-
Self::new(name, slug.into())
359+
let slug = ok_slug!(conn, &name, slug, organization, QueryOrganization);
360+
Self::new(name, slug)
367361
}
368362

369363
pub fn from_user(conn: &mut DbConnection, query_user: &QueryUser) -> Self {
@@ -378,7 +372,7 @@ impl InsertOrganization {
378372
QueryOrganization
379373
);
380374
// The user's organization should be created with the user's UUID.
381-
Self::new_inner(query_user.uuid.into(), name.into(), slug.into())
375+
Self::new_inner(query_user.uuid.into(), name.into(), slug)
382376
}
383377
}
384378

lib/bencher_schema/src/model/project/benchmark.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,13 @@ impl InsertBenchmark {
168168
benchmark: JsonNewBenchmark,
169169
) -> Self {
170170
let JsonNewBenchmark { name, slug } = benchmark;
171-
let slug = ok_slug!(
172-
conn,
173-
project_id,
174-
&name,
175-
slug.map(Into::into),
176-
benchmark,
177-
QueryBenchmark
178-
);
171+
let slug = ok_slug!(conn, project_id, &name, slug, benchmark, QueryBenchmark);
179172
let timestamp = DateTime::now();
180173
Self {
181174
uuid: BenchmarkUuid::new(),
182175
project_id,
183176
name,
184-
slug: slug.into(),
177+
slug,
185178
created: timestamp,
186179
modified: timestamp,
187180
archived: None,

lib/bencher_schema/src/model/project/branch/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -403,20 +403,13 @@ impl InsertBranch {
403403
name: BranchName,
404404
slug: Option<BranchSlug>,
405405
) -> Self {
406-
let slug = ok_slug!(
407-
conn,
408-
project_id,
409-
&name,
410-
slug.map(Into::into),
411-
branch,
412-
QueryBranch
413-
);
406+
let slug = ok_slug!(conn, project_id, &name, slug, branch, QueryBranch);
414407
let timestamp = DateTime::now();
415408
Self {
416409
uuid: BranchUuid::new(),
417410
project_id,
418411
name,
419-
slug: slug.into(),
412+
slug,
420413
head_id: None,
421414
created: timestamp,
422415
modified: timestamp,

lib/bencher_schema/src/model/project/measure.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,13 @@ impl InsertMeasure {
254254

255255
fn from_json(conn: &mut DbConnection, project_id: ProjectId, measure: JsonNewMeasure) -> Self {
256256
let JsonNewMeasure { name, slug, units } = measure;
257-
let slug = ok_slug!(
258-
conn,
259-
project_id,
260-
&name,
261-
slug.map(Into::into),
262-
measure,
263-
QueryMeasure
264-
);
257+
let slug = ok_slug!(conn, project_id, &name, slug, measure, QueryMeasure);
265258
let timestamp = DateTime::now();
266259
Self {
267260
uuid: MeasureUuid::new(),
268261
project_id,
269262
name,
270-
slug: slug.into(),
263+
slug,
271264
units,
272265
created: timestamp,
273266
modified: timestamp,

lib/bencher_schema/src/model/project/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,8 @@ impl InsertProject {
591591
url,
592592
visibility,
593593
} = project;
594-
let slug = ok_slug!(conn, &name, slug.map(Into::into), project, QueryProject);
595-
Self::new(organization.id, name, slug.into(), url, visibility)
594+
let slug = ok_slug!(conn, &name, slug, project, QueryProject);
595+
Self::new(organization.id, name, slug, url, visibility)
596596
}
597597

598598
fn from_organization(

lib/bencher_schema/src/model/project/testbed.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,13 @@ impl InsertTestbed {
156156

157157
fn from_json(conn: &mut DbConnection, project_id: ProjectId, testbed: JsonNewTestbed) -> Self {
158158
let JsonNewTestbed { name, slug } = testbed;
159-
let slug = ok_slug!(
160-
conn,
161-
project_id,
162-
&name,
163-
slug.map(Into::into),
164-
testbed,
165-
QueryTestbed
166-
);
159+
let slug = ok_slug!(conn, project_id, &name, slug, testbed, QueryTestbed);
167160
let timestamp = DateTime::now();
168161
Self {
169162
uuid: TestbedUuid::new(),
170163
project_id,
171164
name,
172-
slug: slug.into(),
165+
slug,
173166
created: timestamp,
174167
modified: timestamp,
175168
archived: None,

lib/bencher_schema/src/model/user/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,12 @@ impl InsertUser {
189189
slug: Option<UserSlug>,
190190
email: Email,
191191
) -> Self {
192-
let slug = ok_slug!(conn, &name, slug.map(Into::into), user, QueryUser);
192+
let slug = ok_slug!(conn, &name, slug, user, QueryUser);
193193
let timestamp = DateTime::now();
194194
Self {
195195
uuid: UserUuid::new(),
196196
name,
197-
slug: slug.into(),
197+
slug,
198198
email,
199199
admin: false,
200200
locked: false,

0 commit comments

Comments
 (0)