Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions crates/prek/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use strum::EnumCount;

use crate::fs::Simplified;
use crate::install_source::InstallSource;
#[cfg(feature = "schemars")]
use crate::schema::{schema_repo_builtin, schema_repo_local, schema_repo_meta, schema_repo_remote};
use crate::version;
use crate::warn_user;
use crate::warn_user_once;
Expand Down Expand Up @@ -845,9 +843,7 @@ impl TryFrom<RemoteHook> for BuiltinHook {
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub(crate) struct RemoteRepo {
#[cfg_attr(feature = "schemars", schemars(schema_with = "schema_repo_remote"))]
pub repo: String,
pub rev: String,
#[serde(skip_serializing)]
Expand Down Expand Up @@ -890,9 +886,7 @@ impl Display for RemoteRepo {
}

#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub(crate) struct LocalRepo {
#[cfg_attr(feature = "schemars", schemars(schema_with = "schema_repo_local"))]
pub repo: String,
pub hooks: Vec<LocalHook>,

Expand All @@ -907,9 +901,7 @@ impl Display for LocalRepo {
}

#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub(crate) struct MetaRepo {
#[cfg_attr(feature = "schemars", schemars(schema_with = "schema_repo_meta"))]
pub repo: String,
pub hooks: Vec<MetaHook>,

Expand All @@ -924,9 +916,7 @@ impl Display for MetaRepo {
}

#[derive(Debug, Clone, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub(crate) struct BuiltinRepo {
#[cfg_attr(feature = "schemars", schemars(schema_with = "schema_repo_builtin"))]
pub repo: String,
pub hooks: Vec<BuiltinHook>,

Expand Down
149 changes: 111 additions & 38 deletions crates/prek/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::config::{
BuiltinHook, BuiltinRepo, FilePattern, LocalRepo, MetaHook, MetaRepo, PassFilenames,
BuiltinHook, BuiltinRepo, FilePattern, LocalHook, LocalRepo, MetaHook, MetaRepo, PassFilenames,
RemoteHook, RemoteRepo, Repo, Stage, Stages,
};
use std::borrow::Cow;
Expand Down Expand Up @@ -235,6 +235,65 @@ fn predefined_hook_schema(
schema
}

impl schemars::JsonSchema for RemoteRepo {
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("RemoteRepo")
}

fn json_schema(schema_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema {
let hook_schema = schema_gen.subschema_for::<RemoteHook>();
schemars::json_schema!({
"type": "object",
"properties": {
"repo": {
"type": "string",
"not": {
"enum": ["local", "meta", "builtin"],
},
"description": "Remote repository location. Must not be `local`, `meta`, or `builtin`.",
},
"rev": {
"type": "string",
},
"hooks": {
"type": "array",
"items": hook_schema,
"writeOnly": true,
},
},
"required": ["repo", "rev", "hooks"],
"additionalProperties": true,
})
}
}

impl schemars::JsonSchema for LocalRepo {
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("LocalRepo")
}

fn json_schema(schema_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema {
let hook_schema = schema_gen.subschema_for::<LocalHook>();
schemars::json_schema!({
"type": "object",
"properties": {
"repo": {
"type": "string",
"const": "local",
"description": "Must be `local`.",
},
"hooks": {
"type": "array",
"items": hook_schema,
},
"rev": false,
},
"required": ["repo", "hooks"],
"additionalProperties": true,
})
}
}

impl schemars::JsonSchema for MetaHook {
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("MetaHook")
Expand All @@ -248,6 +307,33 @@ impl schemars::JsonSchema for MetaHook {
}
}

impl schemars::JsonSchema for MetaRepo {
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("MetaRepo")
}

fn json_schema(schema_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema {
let hook_schema = schema_gen.subschema_for::<MetaHook>();
schemars::json_schema!({
"type": "object",
"properties": {
"repo": {
"type": "string",
"const": "meta",
"description": "Must be `meta`.",
},
"hooks": {
"type": "array",
"items": hook_schema,
},
"rev": false,
},
"required": ["repo", "hooks"],
"additionalProperties": true,
})
}
}

impl schemars::JsonSchema for BuiltinHook {
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("BuiltinHook")
Expand All @@ -261,44 +347,31 @@ impl schemars::JsonSchema for BuiltinHook {
}
}

pub(crate) fn schema_repo_local(
_gen: &mut schemars::generate::SchemaGenerator,
) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"const": "local",
"description": "Must be `local`.",
})
}

pub(crate) fn schema_repo_meta(_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"const": "meta",
"description": "Must be `meta`.",
})
}

pub(crate) fn schema_repo_builtin(
_gen: &mut schemars::generate::SchemaGenerator,
) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"const": "builtin",
"description": "Must be `builtin`.",
})
}
impl schemars::JsonSchema for BuiltinRepo {
fn schema_name() -> Cow<'static, str> {
Cow::Borrowed("BuiltinRepo")
}

pub(crate) fn schema_repo_remote(
_gen: &mut schemars::generate::SchemaGenerator,
) -> schemars::Schema {
schemars::json_schema!({
"type": "string",
"not": {
"enum": ["local", "meta", "builtin"],
},
"description": "Remote repository location. Must not be `local`, `meta`, or `builtin`.",
})
fn json_schema(schema_gen: &mut schemars::generate::SchemaGenerator) -> schemars::Schema {
let hook_schema = schema_gen.subschema_for::<BuiltinHook>();
schemars::json_schema!({
"type": "object",
"properties": {
"repo": {
"type": "string",
"const": "builtin",
"description": "Must be `builtin`.",
},
"hooks": {
"type": "array",
"items": hook_schema,
},
"rev": false,
},
"required": ["repo", "hooks"],
"additionalProperties": true,
})
}
}

impl schemars::JsonSchema for Repo {
Expand Down
9 changes: 6 additions & 3 deletions prek.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading