Skip to content

Commit d92577c

Browse files
committed
into_resource_id
1 parent a2bec1a commit d92577c

File tree

45 files changed

+237
-189
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+237
-189
lines changed

lib/bencher_client/src/lib.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,40 @@ macro_rules! from_slug {
247247
};
248248
}
249249

250-
from_slug!(BranchSlug, TestbedSlug, BenchmarkSlug, MeasureSlug);
250+
from_slug!(
251+
OrganizationSlug,
252+
ProjectSlug,
253+
BranchSlug,
254+
TestbedSlug,
255+
BenchmarkSlug,
256+
MeasureSlug,
257+
UserSlug
258+
);
259+
260+
macro_rules! from_resource_id {
261+
($($from:ident),*) => {
262+
$(
263+
impl From<bencher_json::$from> for types::ResourceId {
264+
fn from(json: bencher_json::$from) -> Self {
265+
match json {
266+
bencher_json::$from::Uuid(uuid) => Self::Uuid(uuid.into()),
267+
bencher_json::$from::Slug(slug) => Self::Slug(slug.into()),
268+
}.into()
269+
}
270+
}
271+
)*
272+
};
273+
}
274+
275+
from_resource_id!(
276+
OrganizationResourceId,
277+
ProjectResourceId,
278+
BranchResourceId,
279+
TestbedResourceId,
280+
BenchmarkResourceId,
281+
MeasureResourceId,
282+
UserResourceId
283+
);
251284

252285
macro_rules! from_name_id {
253286
($($from:ident),*) => {

lib/bencher_json/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ pub use bencher_context::RunContext;
44
pub use bencher_valid::{
55
BenchmarkName, Boundary, BranchName, CdfBoundary, DateTime, DateTimeMillis, Email, GitHash,
66
Index, IqrBoundary, Jwt, Model, ModelTest, NameId, NonEmpty, PercentageBoundary, ResourceId,
7-
ResourceName, SampleSize, Sanitize, Search, Secret, Slug, Units, Url, UserName, ValidError,
8-
Window,
7+
ResourceName, SampleSize, Sanitize, Search, Secret, Slug, IntoResourceId, Units, Url, UserName,
8+
ValidError, Window,
99
};
1010
#[cfg(feature = "plus")]
1111
pub use bencher_valid::{

lib/bencher_json/src/typed_slug.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ macro_rules! typed_slug {
5050
}
5151
}
5252

53+
impl<U> $crate::IntoResourceId<U, $slug> for $slug {
54+
fn into_resource_id(self) -> $crate::ResourceId<U, $slug> {
55+
$crate::ResourceId::Slug(self)
56+
}
57+
}
58+
5359
$crate::typed_db::typed_db!($slug);
5460
};
5561
($slug:ident, $name:ident) => {

lib/bencher_json/src/typed_uuid.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ macro_rules! typed_uuid {
4949
}
5050
}
5151

52+
impl<S> $crate::IntoResourceId<$uuid, S> for $uuid {
53+
fn into_resource_id(self) -> $crate::ResourceId<$uuid, S> {
54+
$crate::ResourceId::Uuid(self)
55+
}
56+
}
57+
5258
impl $uuid {
5359
pub fn new() -> Self {
5460
Self(uuid::Uuid::new_v4())

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::string::ToString as _;
22

33
use bencher_json::{
4-
DateTime, JsonNewOrganization, JsonOrganization, Jwt, OrganizationResourceId, OrganizationSlug,
5-
OrganizationUuid, ProjectSlug, ResourceName,
4+
DateTime, IntoResourceId as _, JsonNewOrganization, JsonOrganization, Jwt,
5+
OrganizationResourceId, OrganizationSlug, OrganizationUuid, ProjectSlug, ResourceName,
66
organization::{
77
JsonOrganizationPatch, JsonOrganizationPatchNull, JsonUpdateOrganization,
88
member::OrganizationRole,
@@ -96,7 +96,7 @@ impl QueryOrganization {
9696
// The project organization should be created with the project's slug.
9797
if let Ok(query_organization) = Self::from_resource_id(
9898
conn_lock!(context),
99-
&OrganizationSlug::from(project_slug.clone()).into(),
99+
&OrganizationSlug::from(project_slug.clone()).into_resource_id(),
100100
) {
101101
// If the project is part of an organization that is claimed,
102102
// then the project can not have anonymous reports.

lib/bencher_valid/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub use plus::{
5353
CardBrand, CardCvc, CardNumber, Entitlements, ExpirationMonth, ExpirationYear, LastFour,
5454
LicensedPlanId, MeteredPlanId, PlanLevel, PlanStatus,
5555
};
56-
pub use resource_id::ResourceId;
56+
pub use resource_id::{IntoResourceId, ResourceId};
5757
pub use resource_name::ResourceName;
5858
pub use search::Search;
5959
pub use secret::Secret;

lib/bencher_valid/src/resource_id.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ pub enum ResourceId<U, S> {
1717
Slug(S),
1818
}
1919

20-
impl<U, S> From<S> for ResourceId<U, S> {
21-
fn from(slug: S) -> Self {
22-
Self::Slug(slug)
23-
}
20+
pub trait IntoResourceId<U, S> {
21+
fn into_resource_id(self) -> ResourceId<U, S>;
2422
}
2523

2624
#[cfg(feature = "schema")]

services/cli/src/bencher/sub/organization/member/invite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bencher_client::types::{JsonNewMember, OrganizationRole};
2-
use bencher_json::{Email, ResourceId, UserName};
2+
use bencher_json::{Email, OrganizationResourceId, UserName};
33

44
use crate::{
55
CliError,
@@ -11,7 +11,7 @@ use crate::bencher::SubCmd;
1111

1212
#[derive(Debug, Clone)]
1313
pub struct Invite {
14-
organization: ResourceId,
14+
organization: OrganizationResourceId,
1515
name: Option<UserName>,
1616
email: Email,
1717
role: OrganizationRole,

services/cli/src/bencher/sub/organization/member/list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use bencher_client::types::{JsonDirection, OrgMembersSort};
2-
use bencher_json::{ResourceId, UserName};
2+
use bencher_json::{OrganizationResourceId, UserName};
33

44
use crate::{
55
CliError,
@@ -12,7 +12,7 @@ use crate::{
1212

1313
#[derive(Debug)]
1414
pub struct List {
15-
pub organization: ResourceId,
15+
pub organization: OrganizationResourceId,
1616
pub name: Option<UserName>,
1717
pub search: Option<String>,
1818
pub pagination: Pagination,

services/cli/src/bencher/sub/organization/member/remove.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bencher_json::ResourceId;
1+
use bencher_json::{OrganizationResourceId, UserResourceId};
22

33
use crate::{
44
CliError,
@@ -8,8 +8,8 @@ use crate::{
88

99
#[derive(Debug)]
1010
pub struct Remove {
11-
pub organization: ResourceId,
12-
pub user: ResourceId,
11+
pub organization: OrganizationResourceId,
12+
pub user: UserResourceId,
1313
pub backend: AuthBackend,
1414
}
1515

0 commit comments

Comments
 (0)