Skip to main content

pre_commit_config/
lib.rs

1mod pre_commit_elements;
2pub use pre_commit_elements::*;
3
4use merge_it::*;
5#[cfg(feature = "schemars")]
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::collections::{BTreeMap, BTreeSet};
9
10type StringBTreeMap = BTreeMap<String, String>;
11
12/// Configuration settings for [`pre-commit`](https://pre-commit.com)
13#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Merge, Default)]
14#[cfg_attr(feature = "schemars", derive(JsonSchema))]
15#[serde(default)]
16pub struct PreCommitConfig {
17	/// A minimum version of pre-commit https://pre-commit.com/#pre-commit-configyaml---top-level
18	#[serde(skip_serializing_if = "Option::is_none")]
19	pub minimum_pre_commit_version: Option<String>,
20
21	/// A list of hook types which will be used by default when running `pre-commit install` https://pre-commit.com/#pre-commit-configyaml---top-level
22	#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
23	pub default_install_hook_types: BTreeSet<String>,
24
25	/// Mappings for the default language versions of the current project https://pre-commit.com/#pre-commit-configyaml---top-level
26	#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
27	pub default_language_version: StringBTreeMap,
28
29	/// The default stages of the current project https://pre-commit.com/#pre-commit-configyaml---top-level
30	#[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
31	pub default_stages: BTreeSet<Stage>,
32
33	/// A file include pattern of the current project https://pre-commit.com/#pre-commit-configyaml---top-level
34	#[serde(skip_serializing_if = "Option::is_none")]
35	pub files: Option<String>,
36
37	/// A file exclude pattern of the current project https://pre-commit.com/#pre-commit-configyaml---top-level
38	#[serde(skip_serializing_if = "Option::is_none")]
39	pub exclude: Option<String>,
40
41	/// Whether stop running hooks after a first failure https://pre-commit.com/#pre-commit-configyaml---top-level
42	#[serde(skip_serializing_if = "Option::is_none")]
43	pub fail_fast: Option<bool>,
44
45	/// pre-commit.ci specific settings https://pre-commit.ci/#configuration
46	#[serde(skip_serializing_if = "Option::is_none")]
47	pub ci: Option<CiSettings>,
48
49	/// Repository mappings of the current project https://pre-commit.com/#pre-commit-configyaml---top-level
50	#[serde(skip_serializing_if = "BTreeSet::is_empty")]
51	pub repos: BTreeSet<Repo>,
52}
53
54/// A pre-commit repo.
55#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
56#[cfg_attr(feature = "schemars", derive(JsonSchema))]
57#[serde(untagged)]
58pub enum Repo {
59	/// Hooks for checking the pre-commit configuration itself. https://pre-commit.com/#meta-hooks
60	Meta {
61		repo: MetaRepo,
62		#[serde(skip_serializing_if = "BTreeSet::is_empty")]
63		hooks: BTreeSet<MetaRepoHook>,
64	},
65	/// Hooks for the local repo https://pre-commit.com/#repository-local-hooks
66	Local {
67		repo: LocalRepo,
68		/// A list of local hooks https://pre-commit.com/#2-add-a-pre-commit-configuration
69		#[serde(skip_serializing_if = "BTreeSet::is_empty")]
70		hooks: BTreeSet<PreCommitHook>,
71	},
72	/// A remote repo
73	Uri {
74		/// A repository url https://pre-commit.com/#2-add-a-pre-commit-configuration
75		repo: String,
76		/// A revision or tag to clone at https://pre-commit.com/#2-add-a-pre-commit-configuration
77		#[serde(skip_serializing_if = "Option::is_none")]
78		rev: Option<String>,
79		/// A list of hook mappings https://pre-commit.com/#pre-commit-configyaml---hooks.
80		#[serde(skip_serializing_if = "BTreeSet::is_empty")]
81		hooks: BTreeSet<PreCommitHook>,
82	},
83}
84
85#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
86#[cfg_attr(feature = "schemars", derive(JsonSchema))]
87#[serde(rename_all = "lowercase")]
88pub enum MetaRepo {
89	Meta,
90}
91
92#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
93#[cfg_attr(feature = "schemars", derive(JsonSchema))]
94#[serde(rename_all = "kebab-case")]
95pub enum MetaRepoId {
96	CheckHooksApply,
97	CheckUselessExcludes,
98	Identity,
99}
100
101#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
102#[cfg_attr(feature = "schemars", derive(JsonSchema))]
103#[serde(deny_unknown_fields)]
104pub struct MetaRepoHook {
105	pub id: MetaRepoId,
106}
107
108#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
109#[cfg_attr(feature = "schemars", derive(JsonSchema))]
110#[serde(rename_all = "lowercase")]
111pub enum LocalRepo {
112	Local,
113}
114
115/// Description for a pre-commit hook. https://pre-commit.com/#pre-commit-configyaml---hooks
116#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default, Ord, PartialOrd)]
117#[cfg_attr(feature = "schemars", derive(JsonSchema))]
118#[serde(default)]
119#[serde(deny_unknown_fields)]
120pub struct PreCommitHook {
121	/// An identifier of the current hook https://pre-commit.com/#pre-commit-configyaml---hooks
122	pub id: String,
123
124	/// A list of additional_dependencies of the current hook https://pre-commit.com/#pre-commit-configyaml---hooks
125	#[serde(skip_serializing_if = "Option::is_none")]
126	pub additional_dependencies: Option<BTreeSet<String>>,
127
128	/// An additional identifier of the current hook for `pre-commit run <hookid>` https://pre-commit.com/#pre-commit-configyaml---hooks
129	#[serde(skip_serializing_if = "Option::is_none")]
130	pub alias: Option<String>,
131
132	/// Run the current hook when no files matched https://pre-commit.com/#pre-commit-configyaml---hooks
133	#[serde(skip_serializing_if = "Option::is_none")]
134	pub always_run: Option<bool>,
135
136	/// List of additional parameters to pass to the current hook https://pre-commit.com/#pre-commit-configyaml---hooks
137	#[serde(skip_serializing_if = "Option::is_none")]
138	pub args: Option<Vec<String>>,
139
140	/// A command of the current hook https://pre-commit.com/#pre-commit-configyaml---hooks
141	#[serde(skip_serializing_if = "Option::is_none")]
142	pub entry: Option<String>,
143
144	/// Exclude files that were matched by files.
145	#[serde(skip_serializing_if = "Option::is_none")]
146	pub exclude: Option<String>,
147
148	/// A list of file types to exclude of the current hook https://pre-commit.com/#pre-commit-configyaml---hooks
149	#[serde(skip_serializing_if = "Option::is_none")]
150	pub exclude_types: Option<BTreeSet<FileType>>,
151
152	/// Description of the hook. used for metadata purposes only.
153	#[serde(skip_serializing_if = "Option::is_none")]
154	pub description: Option<String>,
155
156	/// The pattern of files to run on.
157	#[serde(skip_serializing_if = "Option::is_none")]
158	pub files: Option<String>,
159
160	/// A language the current hook is written in https://pre-commit.com/#pre-commit-configyaml---hooks
161	#[serde(skip_serializing_if = "Option::is_none")]
162	pub language: Option<Language>,
163
164	/// Mappings for the default language versions of the current project https://pre-commit.com/#pre-commit-configyaml---top-level
165	#[serde(skip_serializing_if = "Option::is_none")]
166	pub language_version: Option<String>,
167
168	/// A log file of the current hook
169	#[serde(skip_serializing_if = "Option::is_none")]
170	pub log_file: Option<String>,
171
172	/// Allows one to indicate a minimum compatible pre-commit version.
173	#[serde(skip_serializing_if = "Option::is_none")]
174	pub minimum_pre_commit_version: Option<usize>,
175
176	/// Name of the hook - shown during hook execution.
177	#[serde(skip_serializing_if = "Option::is_none")]
178	pub name: Option<String>,
179
180	/// Whether to pass filenames to the current hook or not https://pre-commit.com/#pre-commit-configyaml---hooks
181	#[serde(skip_serializing_if = "Option::is_none")]
182	pub pass_filenames: Option<bool>,
183
184	/// If true this hook will execute using a single process instead of in parallel.
185	#[serde(skip_serializing_if = "Option::is_none")]
186	pub require_serial: Option<bool>,
187
188	/// A stage of the current hook https://pre-commit.com/#pre-commit-configyaml---hooks
189	#[serde(skip_serializing_if = "Option::is_none")]
190	pub stages: Option<BTreeSet<Stage>>,
191
192	/// List of file types to run on (AND).
193	#[serde(skip_serializing_if = "Option::is_none")]
194	pub types: Option<BTreeSet<FileType>>,
195
196	/// List of file types to run on (OR).
197	#[serde(skip_serializing_if = "Option::is_none")]
198	pub types_or: Option<BTreeSet<FileType>>,
199
200	/// Display an output of the current hook even it passes https://pre-commit.com/#pre-commit-configyaml---hooks
201	#[serde(skip_serializing_if = "Option::is_none")]
202	pub verbose: Option<bool>,
203}