Skip to content

Commit

Permalink
Update with new schema format
Browse files Browse the repository at this point in the history
  • Loading branch information
jssblck committed Oct 19, 2024
1 parent e59dc4e commit 103d383
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 72 deletions.
48 changes: 24 additions & 24 deletions src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use getset::{CopyGetters, Getters};
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::json;
use utoipa::{
openapi::{ObjectBuilder, SchemaType},
ToSchema,
};
use utoipa::ToSchema;

use crate::{
parse_org_package, Error, Fetcher, OrgId, Package, PackageLocator, ParseError, Revision,
Expand Down Expand Up @@ -115,11 +111,30 @@ 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, Builder, Getters, CopyGetters, Documented,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
Builder,
Getters,
CopyGetters,
Documented,
ToSchema,
)]
#[schema(
examples(
json!("git+github.com/fossas/example$1234"),
json!("npm+lodash$1.0.0"),
json!("npm+lodash"),
json!("mvn+1234/org.custom.mylib:mylib$1.0.0:jar")),
)]
pub struct Locator {
/// Determines which fetcher is used to download this package.
#[getset(get_copy = "pub")]
#[schema(ignore)]
fetcher: Fetcher,

/// Specifies the organization ID to which this package is namespaced.
Expand All @@ -142,6 +157,7 @@ pub struct Locator {
/// - A private NPM package that is hosted on NPM but requires credentials is namespaced.
#[builder(into)]
#[getset(get_copy = "pub")]
#[schema(ignore)]
org_id: Option<OrgId>,

/// Specifies the unique identifier for the package by fetcher.
Expand All @@ -150,6 +166,7 @@ pub struct Locator {
/// uses a value in the form of `{user_name}/{package_name}`.
#[builder(into)]
#[getset(get = "pub")]
#[schema(ignore)]
package: Package,

/// Specifies the version for the package by fetcher.
Expand All @@ -159,6 +176,7 @@ pub struct Locator {
/// and the fetcher disambiguates.
#[builder(into)]
#[getset(get = "pub")]
#[schema(ignore)]
revision: Option<Revision>,
}

Expand Down Expand Up @@ -316,24 +334,6 @@ impl Serialize for Locator {
}
}

impl<'a> ToSchema<'a> for Locator {
fn schema() -> (
&'a str,
utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
) {
(
"Locator",
ObjectBuilder::new()
.description(Some(Self::DOCS))
.example(Some(json!("git+github.com/fossas/example$1234")))
.min_length(Some(3))
.schema_type(SchemaType::String)
.build()
.into(),
)
}
}

impl From<PackageLocator> for Locator {
fn from(package: PackageLocator) -> Self {
let (fetcher, org_id, package) = package.explode();
Expand Down
46 changes: 22 additions & 24 deletions src/locator_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use bon::Builder;
use documented::Documented;
use getset::{CopyGetters, Getters};
use serde::{Deserialize, Serialize};
use serde_json::json;
use utoipa::{
openapi::{ObjectBuilder, SchemaType},
ToSchema,
};
use utoipa::ToSchema;

use crate::{Error, Fetcher, Locator, OrgId, Package, StrictLocator};

Expand Down Expand Up @@ -72,11 +68,29 @@ 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, Builder, Getters, CopyGetters, Documented,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
Builder,
Getters,
CopyGetters,
Documented,
ToSchema,
)]
#[schema(
examples(
json!("git+github.com/fossas/example"),
json!("npm+lodash"),
json!("mvn+1234/org.custom.mylib:mylib")),
)]
pub struct PackageLocator {
/// Determines which fetcher is used to download this package.
#[getset(get_copy = "pub")]
#[schema(ignore)]
fetcher: Fetcher,

/// Specifies the organization ID to which this package is namespaced.
Expand All @@ -99,6 +113,7 @@ pub struct PackageLocator {
/// - A private NPM package that is hosted on NPM but requires credentials is namespaced.
#[builder(into)]
#[getset(get_copy = "pub")]
#[schema(ignore)]
org_id: Option<OrgId>,

/// Specifies the unique identifier for the package by fetcher.
Expand All @@ -107,6 +122,7 @@ pub struct PackageLocator {
/// uses a value in the form of `{user_name}/{package_name}`.
#[builder(into)]
#[getset(get = "pub")]
#[schema(ignore)]
package: Package,
}

Expand Down Expand Up @@ -169,24 +185,6 @@ impl<'de> Deserialize<'de> for PackageLocator {
}
}

impl<'a> ToSchema<'a> for PackageLocator {
fn schema() -> (
&'a str,
utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
) {
(
"PackageLocator",
ObjectBuilder::new()
.description(Some(Self::DOCS))
.example(Some(json!("git+github.com/fossas/example")))
.min_length(Some(3))
.schema_type(SchemaType::String)
.build()
.into(),
)
}
}

impl Serialize for PackageLocator {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
47 changes: 23 additions & 24 deletions src/locator_strict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use bon::Builder;
use documented::Documented;
use getset::{CopyGetters, Getters};
use serde::{Deserialize, Serialize};
use serde_json::json;
use utoipa::{
openapi::{ObjectBuilder, SchemaType},
ToSchema,
};
use utoipa::ToSchema;

use crate::{Error, Fetcher, Locator, OrgId, Package, PackageLocator, ParseError, Revision};

Expand Down Expand Up @@ -72,11 +68,29 @@ macro_rules! strict {
/// {fetcher}+{org_id}/{package}${revision}
/// ```
#[derive(
Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
Debug,
Builder,
Getters,
CopyGetters,
Documented,
ToSchema,
)]
#[schema(
examples(
json!("git+github.com/fossas/example$1234"),
json!("npm+lodash$1.0.0"),
json!("mvn+1234/org.custom.mylib:mylib$1.0.0:jar")),
)]
pub struct StrictLocator {
/// Determines which fetcher is used to download this package.
#[getset(get_copy = "pub")]
#[schema(ignore)]
fetcher: Fetcher,

/// Specifies the organization ID to which this package is namespaced.
Expand All @@ -99,6 +113,7 @@ pub struct StrictLocator {
/// - A private NPM package that is hosted on NPM but requires credentials is namespaced.
#[builder(into)]
#[getset(get_copy = "pub")]
#[schema(ignore)]
org_id: Option<OrgId>,

/// Specifies the unique identifier for the package by fetcher.
Expand All @@ -107,6 +122,7 @@ pub struct StrictLocator {
/// uses a value in the form of `{user_name}/{package_name}`.
#[builder(into)]
#[getset(get = "pub")]
#[schema(ignore)]
package: Package,

/// Specifies the version for the package by fetcher.
Expand All @@ -116,6 +132,7 @@ pub struct StrictLocator {
/// and the fetcher disambiguates.
#[builder(into)]
#[getset(get = "pub")]
#[schema(ignore)]
revision: Revision,
}

Expand Down Expand Up @@ -195,24 +212,6 @@ impl Serialize for StrictLocator {
}
}

impl<'a> ToSchema<'a> for StrictLocator {
fn schema() -> (
&'a str,
utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
) {
(
"StrictLocator",
ObjectBuilder::new()
.description(Some(Self::DOCS))
.example(Some(json!("git+github.com/fossas/example$1234")))
.min_length(Some(3))
.schema_type(SchemaType::String)
.build()
.into(),
)
}
}

impl AsRef<StrictLocator> for StrictLocator {
fn as_ref(&self) -> &StrictLocator {
self
Expand Down

0 comments on commit 103d383

Please sign in to comment.