Skip to content

Commit adb3137

Browse files
authored
Replace typed_builder with bon (#9)
1 parent bb2bfb0 commit adb3137

File tree

5 files changed

+176
-59
lines changed

5 files changed

+176
-59
lines changed

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "locator"
3-
version = "2.1.0"
3+
version = "2.2.0"
44
edition = "2021"
55

66
[dependencies]
@@ -12,11 +12,12 @@ regex = "1.6.0"
1212
serde = { version = "1.0.140", features = ["derive"] }
1313
strum = { version = "0.24.1", features = ["derive"] }
1414
thiserror = "1.0.31"
15-
typed-builder = "0.10.0"
1615
utoipa = "4.2.3"
1716
serde_json = "1.0.95"
1817
documented = "0.4.1"
1918
semver = "1.0.23"
19+
bon = "2.3.0"
20+
duplicate = "2.0.0"
2021

2122
[dev-dependencies]
2223
assert_matches = "1.5.0"

src/lib.rs

+38-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use std::{borrow::Cow, str::FromStr};
77

8+
use duplicate::duplicate;
89
use lazy_static::lazy_static;
910
use regex::Regex;
1011
use serde::{Deserialize, Serialize};
@@ -178,27 +179,40 @@ pub enum Fetcher {
178179
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
179180
pub struct OrgId(usize);
180181

181-
impl From<OrgId> for u64 {
182-
fn from(value: OrgId) -> Self {
183-
value.0 as u64
184-
}
185-
}
186-
187182
impl From<OrgId> for usize {
188183
fn from(value: OrgId) -> Self {
189184
value.0
190185
}
191186
}
192187

193-
impl From<OrgId> for i64 {
194-
fn from(value: OrgId) -> Self {
195-
value.0 as i64
188+
impl From<usize> for OrgId {
189+
fn from(value: usize) -> Self {
190+
Self(value)
196191
}
197192
}
198193

199-
impl From<OrgId> for isize {
200-
fn from(value: OrgId) -> Self {
201-
value.0 as isize
194+
duplicate! {
195+
[
196+
number;
197+
[ u64 ];
198+
[ u32 ];
199+
[ u16 ];
200+
[ u8 ];
201+
[ i64 ];
202+
[ i32 ];
203+
[ i16 ];
204+
[ i8 ];
205+
[ isize ];
206+
]
207+
impl From<OrgId> for number {
208+
fn from(value: OrgId) -> Self {
209+
value.0 as number
210+
}
211+
}
212+
impl From<number> for OrgId {
213+
fn from(value: number) -> Self {
214+
Self(value as usize)
215+
}
202216
}
203217
}
204218

@@ -245,6 +259,12 @@ impl From<&str> for Package {
245259
}
246260
}
247261

262+
impl From<&Package> for Package {
263+
fn from(value: &Package) -> Self {
264+
value.clone()
265+
}
266+
}
267+
248268
impl std::fmt::Display for Package {
249269
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
250270
write!(f, "{}", self.0)
@@ -304,6 +324,12 @@ impl From<&str> for Revision {
304324
}
305325
}
306326

327+
impl From<&Revision> for Revision {
328+
fn from(value: &Revision) -> Self {
329+
value.clone()
330+
}
331+
}
332+
307333
impl std::fmt::Display for Revision {
308334
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
309335
match self {

src/locator.rs

+46-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use std::{fmt::Display, str::FromStr};
22

3+
use bon::Builder;
34
use documented::Documented;
45
use getset::{CopyGetters, Getters};
56
use lazy_static::lazy_static;
67
use regex::Regex;
78
use serde::{Deserialize, Serialize};
89
use serde_json::json;
9-
use typed_builder::TypedBuilder;
1010
use utoipa::{
1111
openapi::{ObjectBuilder, SchemaType},
1212
ToSchema,
@@ -115,17 +115,7 @@ macro_rules! locator {
115115
///
116116
/// This parse function is based on the function used in FOSSA Core for maximal compatibility.
117117
#[derive(
118-
Clone,
119-
Eq,
120-
PartialEq,
121-
Ord,
122-
PartialOrd,
123-
Hash,
124-
Debug,
125-
TypedBuilder,
126-
Getters,
127-
CopyGetters,
128-
Documented,
118+
Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented,
129119
)]
130120
pub struct Locator {
131121
/// Determines which fetcher is used to download this package.
@@ -150,15 +140,15 @@ pub struct Locator {
150140
/// - A private Maven package that is hosted on a private host is namespaced.
151141
/// - A public NPM package that is hosted on NPM is not namespaced.
152142
/// - A private NPM package that is hosted on NPM but requires credentials is namespaced.
153-
#[builder(default, setter(transform = |id: usize| Some(OrgId(id))))]
143+
#[builder(into)]
154144
#[getset(get_copy = "pub")]
155145
org_id: Option<OrgId>,
156146

157147
/// Specifies the unique identifier for the package by fetcher.
158148
///
159149
/// For example, the `git` fetcher fetching a github package
160150
/// uses a value in the form of `{user_name}/{package_name}`.
161-
#[builder(setter(transform = |package: impl ToString| Package(package.to_string())))]
151+
#[builder(into)]
162152
#[getset(get = "pub")]
163153
package: Package,
164154

@@ -167,7 +157,7 @@ pub struct Locator {
167157
/// For example, the `git` fetcher fetching a github package
168158
/// uses a value in the form of `{git_sha}` or `{git_tag}`,
169159
/// and the fetcher disambiguates.
170-
#[builder(default, setter(transform = |revision: impl ToString| Some(Revision::from(revision.to_string()))))]
160+
#[builder(into)]
171161
#[getset(get = "pub")]
172162
revision: Option<Revision>,
173163
}
@@ -419,6 +409,47 @@ mod tests {
419409

420410
use super::*;
421411

412+
#[test]
413+
fn from_existing() {
414+
let first = locator!(Git, "github.com/foo/bar");
415+
let second = Locator::builder()
416+
.fetcher(first.fetcher())
417+
.maybe_org_id(first.org_id())
418+
.package(first.package())
419+
.maybe_revision(first.revision().as_ref())
420+
.build();
421+
assert_eq!(first, second);
422+
}
423+
424+
#[test]
425+
fn optional_fields() {
426+
let with_options = Locator::builder()
427+
.fetcher(Fetcher::Git)
428+
.package("github.com/foo/bar")
429+
.maybe_org_id(Some(1234))
430+
.maybe_revision(Some("abcd"))
431+
.build();
432+
let expected = Locator::builder()
433+
.fetcher(Fetcher::Git)
434+
.package("github.com/foo/bar")
435+
.org_id(1234)
436+
.revision("abcd")
437+
.build();
438+
assert_eq!(expected, with_options);
439+
440+
let without_options = Locator::builder()
441+
.fetcher(Fetcher::Git)
442+
.package("github.com/foo/bar")
443+
.maybe_org_id(None::<usize>)
444+
.maybe_revision(None::<&str>)
445+
.build();
446+
let expected = Locator::builder()
447+
.fetcher(Fetcher::Git)
448+
.package("github.com/foo/bar")
449+
.build();
450+
assert_eq!(expected, without_options);
451+
}
452+
422453
#[test]
423454
fn trait_impls() {
424455
const_assert!(impls!(Locator: AsRef<Locator>));

src/locator_package.rs

+42-15
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{fmt::Display, str::FromStr};
22

3+
use bon::Builder;
34
use documented::Documented;
45
use getset::{CopyGetters, Getters};
56
use serde::{Deserialize, Serialize};
67
use serde_json::json;
7-
use typed_builder::TypedBuilder;
88
use utoipa::{
99
openapi::{ObjectBuilder, SchemaType},
1010
ToSchema,
@@ -72,17 +72,7 @@ macro_rules! package {
7272
///
7373
/// This implementation ignores the `revision` segment if it exists. If this is not preferred, use [`Locator`] instead.
7474
#[derive(
75-
Clone,
76-
Eq,
77-
PartialEq,
78-
Ord,
79-
PartialOrd,
80-
Hash,
81-
Debug,
82-
TypedBuilder,
83-
Getters,
84-
CopyGetters,
85-
Documented,
75+
Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented,
8676
)]
8777
pub struct PackageLocator {
8878
/// Determines which fetcher is used to download this package.
@@ -107,15 +97,15 @@ pub struct PackageLocator {
10797
/// - A private Maven package that is hosted on a private host is namespaced.
10898
/// - A public NPM package that is hosted on NPM is not namespaced.
10999
/// - A private NPM package that is hosted on NPM but requires credentials is namespaced.
110-
#[builder(default, setter(transform = |id: usize| Some(OrgId(id))))]
100+
#[builder(into)]
111101
#[getset(get_copy = "pub")]
112102
org_id: Option<OrgId>,
113103

114104
/// Specifies the unique identifier for the package by fetcher.
115105
///
116106
/// For example, the `git` fetcher fetching a github package
117107
/// uses a value in the form of `{user_name}/{package_name}`.
118-
#[builder(setter(transform = |package: impl ToString| Package(package.to_string())))]
108+
#[builder(into)]
119109
#[getset(get = "pub")]
120110
package: Package,
121111
}
@@ -147,7 +137,7 @@ impl PackageLocator {
147137
let locator = StrictLocator::builder()
148138
.fetcher(self.fetcher)
149139
.package(self.package)
150-
.revision(revision);
140+
.revision(revision.to_string());
151141

152142
match self.org_id {
153143
None => locator.build(),
@@ -276,6 +266,43 @@ mod tests {
276266

277267
use super::*;
278268

269+
#[test]
270+
fn from_existing() {
271+
let first = package!(Git, "github.com/foo/bar");
272+
let second = PackageLocator::builder()
273+
.fetcher(first.fetcher())
274+
.maybe_org_id(first.org_id())
275+
.package(first.package())
276+
.build();
277+
assert_eq!(first, second);
278+
}
279+
280+
#[test]
281+
fn optional_fields() {
282+
let with_options = PackageLocator::builder()
283+
.fetcher(Fetcher::Git)
284+
.package("github.com/foo/bar")
285+
.maybe_org_id(Some(1234))
286+
.build();
287+
let expected = PackageLocator::builder()
288+
.fetcher(Fetcher::Git)
289+
.package("github.com/foo/bar")
290+
.org_id(1234)
291+
.build();
292+
assert_eq!(expected, with_options);
293+
294+
let without_options = PackageLocator::builder()
295+
.fetcher(Fetcher::Git)
296+
.package("github.com/foo/bar")
297+
.maybe_org_id(None::<usize>)
298+
.build();
299+
let expected = PackageLocator::builder()
300+
.fetcher(Fetcher::Git)
301+
.package("github.com/foo/bar")
302+
.build();
303+
assert_eq!(expected, without_options);
304+
}
305+
279306
#[test]
280307
fn trait_impls() {
281308
const_assert!(impls!(PackageLocator: AsRef<PackageLocator>));

0 commit comments

Comments
 (0)