Skip to content

Commit

Permalink
Next iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Veetaha committed Apr 7, 2024
1 parent e49c05c commit 484e1fe
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 160 deletions.
48 changes: 28 additions & 20 deletions deny.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ description: Full documentation is at https://embarkstudios.github.io/cargo-deny

type: object
properties:
advisories: { $ref: '#/definitions/advisories' }
graph: { $ref: '#/definitions/graph' }
output: { $ref: '#/definitions/output' }
advisories: { $ref: "#/definitions/advisories" }
graph: { $ref: "#/definitions/graph" }
output: { $ref: "#/definitions/output" }

definitions:
advisories:
Expand Down Expand Up @@ -110,7 +110,7 @@ definitions:
vulnerability:
deprecated: true
$ref: '#/definitions/lint-level'
$ref: "#/definitions/lint-level"
default: deny
description: |
**DEPRECATED** (see `version` field)
Expand All @@ -119,7 +119,7 @@ definitions:
unmaintained:
deprecated: true
$ref: '#/definitions/lint-level'
$ref: "#/definitions/lint-level"
default: warn
description: |
**DEPRECATED** (see `version` field)
Expand All @@ -128,7 +128,7 @@ definitions:
unsound:
deprecated: true
$ref: '#/definitions/lint-level'
$ref: "#/definitions/lint-level"
default: warn
description: |
**DEPRECATED** (see `version` field)
Expand All @@ -137,7 +137,7 @@ definitions:
notice:
deprecated: true
$ref: '#/definitions/lint-level'
$ref: "#/definitions/lint-level"
default: warn
description: |
**DEPRECATED** (see `version` field)
Expand All @@ -148,15 +148,15 @@ definitions:
[RustSec Advisory DB](https://github.com/RustSec/advisory-db)
yanked:
$ref: '#/definitions/lint-level'
$ref: "#/definitions/lint-level"
default: warn
description: |
Determines what happens when a crate with a version that has been yanked from its source
registry is encountered.
ignore:
type: array
items: { $ref: '#/definitions/advisories-ignore-item' }
items: { $ref: "#/definitions/advisories-ignore-item" }
examples:
- - RUSTSEC-0000-0000
- id: "RUSTSEC-0000-0000"
Expand All @@ -175,10 +175,15 @@ definitions:
advisories-ignore-item:
oneOf:
- type: string
- variant: String
type: string
description: Either an advisory ID (e.g. `RUSTSEC-2019-0001`) or a package spec (e.g. `yanked@0.1.1`).
- { $ref: '#/definitions/advisories-ignore-advisory' }
- { $ref: '#/definitions/advisories-ignore-yanked' }

- variant: Advisory
$ref: "#/definitions/advisories-ignore-advisory"

- variant: Yanked
$ref: "#/definitions/advisories-ignore-yanked"

advisories-ignore-advisory:
type: object
Expand All @@ -188,14 +193,14 @@ definitions:
type: string
examples: [RUSTSEC-2019-0001]
description: The unique identifier of the advisory to ignore
reason: { $ref: '#/definitions/ignore-reason' }
reason: { $ref: "#/definitions/ignore-reason" }

advisories-ignore-yanked:
type: object
required: [crate]
properties:
crate: { $ref: '#/definitions/package-spec' }
reason: { $ref: '#/definitions/ignore-reason' }
crate: { $ref: "#/definitions/package-spec" }
reason: { $ref: "#/definitions/ignore-reason" }

ignore-reason:
type: string
Expand Down Expand Up @@ -299,7 +304,7 @@ definitions:
properties:
targets:
type: array
items: { $ref: '#/definitions/target' }
items: { $ref: "#/definitions/target" }
description: |
By default, cargo-deny will consider every single crate that is resolved by cargo, including
target specific dependencies e.g.
Expand Down Expand Up @@ -361,15 +366,18 @@ definitions:
target:
oneOf:
- $ref: '#/definitions/target-string'
- $ref: '#/definitions/target-complex'
- variant: String
$ref: "#/definitions/target-string"

- variant: Advanced
$ref: "#/definitions/target-advanced"

target-complex:
target-advanced:
description: Advanced configurations to apply for the target triple
type: object
required: [triple]
properties:
triple: { $ref: '#/definitions/target-string' }
triple: { $ref: "#/definitions/target-string" }
features:
type: string
description: |
Expand Down
80 changes: 70 additions & 10 deletions xtask/src/cli/codegen/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) struct Schema {
pub(crate) enum_schema: Option<EnumSchema>,

#[serde(skip_serializing_if = "Option::is_none", rename = "oneOf")]
pub(crate) one_of: Option<Vec<Schema>>,
pub(crate) one_of: Option<Vec<VariantSchema>>,

#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) title: Option<String>,
Expand Down Expand Up @@ -78,7 +78,16 @@ pub(crate) struct CustomEnumSchema {
pub(crate) description: String,
}

pub(crate) struct OneOfSchema {}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct VariantSchema {
/// Name of the variant. It's main use case is when generating a Rust enum,
/// which requires a name for each variant. However, it's also useful for
/// documentation purposes as a succinct display name for the variant.
pub(crate) name: String,

#[serde(flatten)]
pub(crate) schema: Schema,
}

// /// Unfortunately we can't use an internally-tagged enum here with associated data
// /// because when flattening such enum with `#[serde(flatten)]` we end up with
Expand Down Expand Up @@ -129,14 +138,25 @@ impl Schema {
std::iter::from_fn(move || {
let schema = stack.pop()?;

let properties = schema
let object_properties = schema
.object_schema
.iter()
.flat_map(|object| object.properties.values());

stack.extend(properties);
stack.extend(schema.array_schema.iter().map(|array| array.items.as_ref()));
stack.extend(schema.one_of.iter().flatten());
let one_of_variants = schema
.one_of
.iter()
.flatten()
.map(|variant| &variant.schema);

let array_items = schema.array_schema.iter().map(|array| array.items.as_ref());

stack.extend(itertools::chain!(
object_properties,
one_of_variants,
array_items
));

Some(schema)
})
}
Expand All @@ -153,16 +173,56 @@ impl Schema {
}

if let Some(one_of) = &mut self.one_of {
one_of.iter_mut().try_for_each(&visit)?;
one_of
.iter_mut()
.map(|variant| &mut variant.schema)
.try_for_each(&visit)?;
}

Ok(())
}

pub(crate) fn try_as_object(&self) -> Result<&ObjectSchema> {
self.object_schema
fn try_downcast_as<T>(&self, schema: &Option<T>, label: &str) -> Result<&T> {
schema
.as_ref()
.with_context(|| format!("Expected object schema, but got {self:#?}"))
.with_context(|| format!("Expected {label} schema, but got {self:#?}"))
}

fn try_downcast_into<T>(self, schema: Option<T>, label: &str) -> Result<T> {
schema.with_context(|| format!("Expected {label} schema, but got {self:#?}"))
}

pub(crate) fn try_as_array(&self) -> Result<&ArraySchema> {
self.try_downcast_as(&self.array_schema, "array")
}

pub(crate) fn try_into_array(self) -> Result<ArraySchema> {
self.try_downcast_into(self.array_schema, "array")
}

pub(crate) fn try_as_object(&self) -> Result<&ObjectSchema> {
self.try_downcast_as(&self.object_schema, "object")
}

pub(crate) fn try_into_object(self) -> Result<ObjectSchema> {
self.try_downcast_into(self.object_schema, "object")
}

pub(crate) fn try_as_enum(&self) -> Result<&EnumSchema> {
self.try_downcast_as(&self.enum_schema, "enum")
}

pub(crate) fn try_into_enum(self) -> Result<EnumSchema> {
self.try_downcast_into(self.enum_schema, "enum")
}

pub(crate) fn try_as_one_of(&self) -> Result<&[VariantSchema]> {
self.try_downcast_as(&self.one_of, "one-of")
.map(Vec::as_slice)
}

pub(crate) fn try_into_one_of(self) -> Result<Vec<VariantSchema>> {
self.try_downcast_into(self.one_of, "one-of")
}

pub(crate) fn try_description(&self) -> Result<&str> {
Expand Down
Loading

0 comments on commit 484e1fe

Please sign in to comment.