pub struct ValidationPolicy { /* private fields */ }Expand description
Per-check severity, so a consumer can downgrade or disable a check instead of pinning an old version of this crate when one misfires.
Every check is an error by default.
use kotlin_codegen::{Check, Severity, ValidationPolicy};
let policy = ValidationPolicy::new().warn(Check::DuplicateType);
assert_eq!(
policy.severity_of(Check::DuplicateType),
Some(Severity::Warning)
);Implementations§
Source§impl ValidationPolicy
impl ValidationPolicy
Sourcepub fn new() -> Self
pub fn new() -> Self
Every check an error.
Examples found in repository?
examples/invalid.rs (line 47)
23fn main() {
24 println!("=== diagnostics (default: every check is an error) ===");
25 for file in broken_files() {
26 for d in file.validate() {
27 println!("{d}");
28 }
29 }
30
31 println!("\n=== merging refuses to produce anything ===");
32 match merge_files(broken_files()) {
33 Ok(_) => println!("unexpectedly merged"),
34 // The Display of the error is what a build script would surface.
35 Err(e) => print!("{e}"),
36 }
37
38 println!("\n=== the same model under `warn_all()` ===");
39 let (files, warnings) = merge_files_warning();
40 println!(
41 "merged {} file(s) with {} warning(s); generation continues",
42 files.len(),
43 warnings.len()
44 );
45
46 println!("\n=== one check downgraded, another switched off ===");
47 let policy = ValidationPolicy::new()
48 .warn(Check::DuplicateFunction)
49 .allow(Check::InvalidIdentifier);
50 for file in broken_files() {
51 for d in file.validate_with(&policy) {
52 println!("{d}");
53 }
54 }
55
56 println!("\n=== shapes the model will not build at all ===");
57 for (what, outcome) in refused_shapes() {
58 println!("{what}\n -> {outcome}");
59 }
60}Sourcepub fn warn(self, check: Check) -> Self
pub fn warn(self, check: Check) -> Self
Report check but keep generating.
Examples found in repository?
examples/invalid.rs (line 48)
23fn main() {
24 println!("=== diagnostics (default: every check is an error) ===");
25 for file in broken_files() {
26 for d in file.validate() {
27 println!("{d}");
28 }
29 }
30
31 println!("\n=== merging refuses to produce anything ===");
32 match merge_files(broken_files()) {
33 Ok(_) => println!("unexpectedly merged"),
34 // The Display of the error is what a build script would surface.
35 Err(e) => print!("{e}"),
36 }
37
38 println!("\n=== the same model under `warn_all()` ===");
39 let (files, warnings) = merge_files_warning();
40 println!(
41 "merged {} file(s) with {} warning(s); generation continues",
42 files.len(),
43 warnings.len()
44 );
45
46 println!("\n=== one check downgraded, another switched off ===");
47 let policy = ValidationPolicy::new()
48 .warn(Check::DuplicateFunction)
49 .allow(Check::InvalidIdentifier);
50 for file in broken_files() {
51 for d in file.validate_with(&policy) {
52 println!("{d}");
53 }
54 }
55
56 println!("\n=== shapes the model will not build at all ===");
57 for (what, outcome) in refused_shapes() {
58 println!("{what}\n -> {outcome}");
59 }
60}Sourcepub fn warn_all() -> Self
pub fn warn_all() -> Self
Downgrade every check to a warning.
The way to adopt validation in a generator that already produces output: run it in warning mode first, look at what comes back, and switch to the default once it is quiet. Reaching a clean run and then dropping this call is much less disruptive than having a build start failing on output that was fine yesterday.
use kotlin_codegen::{Check, Severity, ValidationPolicy};
let policy = ValidationPolicy::warn_all();
for check in Check::ALL {
assert_eq!(policy.severity_of(*check), Some(Severity::Warning));
}Sourcepub fn allow(self, check: Check) -> Self
pub fn allow(self, check: Check) -> Self
Turn check off entirely.
Examples found in repository?
examples/invalid.rs (line 49)
23fn main() {
24 println!("=== diagnostics (default: every check is an error) ===");
25 for file in broken_files() {
26 for d in file.validate() {
27 println!("{d}");
28 }
29 }
30
31 println!("\n=== merging refuses to produce anything ===");
32 match merge_files(broken_files()) {
33 Ok(_) => println!("unexpectedly merged"),
34 // The Display of the error is what a build script would surface.
35 Err(e) => print!("{e}"),
36 }
37
38 println!("\n=== the same model under `warn_all()` ===");
39 let (files, warnings) = merge_files_warning();
40 println!(
41 "merged {} file(s) with {} warning(s); generation continues",
42 files.len(),
43 warnings.len()
44 );
45
46 println!("\n=== one check downgraded, another switched off ===");
47 let policy = ValidationPolicy::new()
48 .warn(Check::DuplicateFunction)
49 .allow(Check::InvalidIdentifier);
50 for file in broken_files() {
51 for d in file.validate_with(&policy) {
52 println!("{d}");
53 }
54 }
55
56 println!("\n=== shapes the model will not build at all ===");
57 for (what, outcome) in refused_shapes() {
58 println!("{what}\n -> {outcome}");
59 }
60}Sourcepub fn severity_of(&self, check: Check) -> Option<Severity>
pub fn severity_of(&self, check: Check) -> Option<Severity>
The effective severity of check, or None when it is off.
Trait Implementations§
Source§impl Clone for ValidationPolicy
impl Clone for ValidationPolicy
Source§fn clone(&self) -> ValidationPolicy
fn clone(&self) -> ValidationPolicy
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ValidationPolicy
impl Debug for ValidationPolicy
Source§impl Default for ValidationPolicy
impl Default for ValidationPolicy
Source§fn default() -> ValidationPolicy
fn default() -> ValidationPolicy
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for ValidationPolicy
impl RefUnwindSafe for ValidationPolicy
impl Send for ValidationPolicy
impl Sync for ValidationPolicy
impl Unpin for ValidationPolicy
impl UnsafeUnpin for ValidationPolicy
impl UnwindSafe for ValidationPolicy
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more