Skip to content

Commit

Permalink
Replace typed_builder with bon
Browse files Browse the repository at this point in the history
  • Loading branch information
jssblck committed Oct 10, 2024
1 parent bb2bfb0 commit 97d182d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 59 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "locator"
version = "2.1.0"
version = "2.2.0"
edition = "2021"

[dependencies]
Expand All @@ -12,11 +12,12 @@ regex = "1.6.0"
serde = { version = "1.0.140", features = ["derive"] }
strum = { version = "0.24.1", features = ["derive"] }
thiserror = "1.0.31"
typed-builder = "0.10.0"
utoipa = "4.2.3"
serde_json = "1.0.95"
documented = "0.4.1"
semver = "1.0.23"
bon = "2.3.0"
duplicate = "2.0.0"

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
38 changes: 26 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

use duplicate::duplicate;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -178,27 +179,40 @@ pub enum Fetcher {
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct OrgId(usize);

impl From<OrgId> for u64 {
fn from(value: OrgId) -> Self {
value.0 as u64
}
}

impl From<OrgId> for usize {
fn from(value: OrgId) -> Self {
value.0
}
}

impl From<OrgId> for i64 {
fn from(value: OrgId) -> Self {
value.0 as i64
impl From<usize> for OrgId {
fn from(value: usize) -> Self {
Self(value)
}
}

impl From<OrgId> for isize {
fn from(value: OrgId) -> Self {
value.0 as isize
duplicate! {
[
number;
[ u64 ];
[ u32 ];
[ u16 ];
[ u8 ];
[ i64 ];
[ i32 ];
[ i16 ];
[ i8 ];
[ isize ];
]
impl From<OrgId> for number {
fn from(value: OrgId) -> Self {
value.0 as number
}
}
impl From<number> for OrgId {
fn from(value: number) -> Self {
Self(value as usize)
}
}
}

Expand Down
20 changes: 5 additions & 15 deletions src/locator.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::{fmt::Display, str::FromStr};

use bon::Builder;
use documented::Documented;
use getset::{CopyGetters, Getters};
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::json;
use typed_builder::TypedBuilder;
use utoipa::{
openapi::{ObjectBuilder, SchemaType},
ToSchema,
Expand Down Expand Up @@ -115,17 +115,7 @@ macro_rules! locator {
///
/// This parse function is based on the function used in FOSSA Core for maximal compatibility.
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
TypedBuilder,
Getters,
CopyGetters,
Documented,
Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented,
)]
pub struct Locator {
/// Determines which fetcher is used to download this package.
Expand All @@ -150,15 +140,15 @@ pub struct Locator {
/// - A private Maven package that is hosted on a private host is namespaced.
/// - A public NPM package that is hosted on NPM is not namespaced.
/// - A private NPM package that is hosted on NPM but requires credentials is namespaced.
#[builder(default, setter(transform = |id: usize| Some(OrgId(id))))]
#[builder(into)]
#[getset(get_copy = "pub")]
org_id: Option<OrgId>,

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

Expand All @@ -167,7 +157,7 @@ pub struct Locator {
/// For example, the `git` fetcher fetching a github package
/// uses a value in the form of `{git_sha}` or `{git_tag}`,
/// and the fetcher disambiguates.
#[builder(default, setter(transform = |revision: impl ToString| Some(Revision::from(revision.to_string()))))]
#[builder(into)]
#[getset(get = "pub")]
revision: Option<Revision>,
}
Expand Down
20 changes: 5 additions & 15 deletions src/locator_package.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{fmt::Display, str::FromStr};

use bon::Builder;
use documented::Documented;
use getset::{CopyGetters, Getters};
use serde::{Deserialize, Serialize};
use serde_json::json;
use typed_builder::TypedBuilder;
use utoipa::{
openapi::{ObjectBuilder, SchemaType},
ToSchema,
Expand Down Expand Up @@ -72,17 +72,7 @@ macro_rules! package {
///
/// This implementation ignores the `revision` segment if it exists. If this is not preferred, use [`Locator`] instead.
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
TypedBuilder,
Getters,
CopyGetters,
Documented,
Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented,
)]
pub struct PackageLocator {
/// Determines which fetcher is used to download this package.
Expand All @@ -107,15 +97,15 @@ pub struct PackageLocator {
/// - A private Maven package that is hosted on a private host is namespaced.
/// - A public NPM package that is hosted on NPM is not namespaced.
/// - A private NPM package that is hosted on NPM but requires credentials is namespaced.
#[builder(default, setter(transform = |id: usize| Some(OrgId(id))))]
#[builder(into)]
#[getset(get_copy = "pub")]
org_id: Option<OrgId>,

/// Specifies the unique identifier for the package by fetcher.
///
/// For example, the `git` fetcher fetching a github package
/// uses a value in the form of `{user_name}/{package_name}`.
#[builder(setter(transform = |package: impl ToString| Package(package.to_string())))]
#[builder(into)]
#[getset(get = "pub")]
package: Package,
}
Expand Down Expand Up @@ -147,7 +137,7 @@ impl PackageLocator {
let locator = StrictLocator::builder()
.fetcher(self.fetcher)
.package(self.package)
.revision(revision);
.revision(revision.to_string());

match self.org_id {
None => locator.build(),
Expand Down
20 changes: 5 additions & 15 deletions src/locator_strict.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{fmt::Display, str::FromStr};

use bon::Builder;
use documented::Documented;
use getset::{CopyGetters, Getters};
use serde::{Deserialize, Serialize};
use serde_json::json;
use typed_builder::TypedBuilder;
use utoipa::{
openapi::{ObjectBuilder, SchemaType},
ToSchema,
Expand Down Expand Up @@ -72,17 +72,7 @@ macro_rules! strict {
/// {fetcher}+{org_id}/{package}${revision}
/// ```
#[derive(
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
TypedBuilder,
Getters,
CopyGetters,
Documented,
Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented,
)]
pub struct StrictLocator {
/// Determines which fetcher is used to download this package.
Expand All @@ -107,15 +97,15 @@ pub struct StrictLocator {
/// - A private Maven package that is hosted on a private host is namespaced.
/// - A public NPM package that is hosted on NPM is not namespaced.
/// - A private NPM package that is hosted on NPM but requires credentials is namespaced.
#[builder(default, setter(transform = |id: usize| Some(OrgId(id))))]
#[builder(into)]
#[getset(get_copy = "pub")]
org_id: Option<OrgId>,

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

Expand All @@ -124,7 +114,7 @@ pub struct StrictLocator {
/// For example, the `git` fetcher fetching a github package
/// uses a value in the form of `{git_sha}` or `{git_tag}`,
/// and the fetcher disambiguates.
#[builder(setter(transform = |revision: impl ToString| Revision::from(revision.to_string())))]
#[builder(into)]
#[getset(get = "pub")]
revision: Revision,
}
Expand Down

0 comments on commit 97d182d

Please sign in to comment.