Skip to main content

ValidationPolicy

Struct ValidationPolicy 

Source
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

Source

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}
Source

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}
Source

pub fn deny(self, check: Check) -> Self

Stop generating on check (the default).

Source

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));
}
Examples found in repository?
examples/invalid.rs (line 176)
175fn merge_files_warning() -> (Vec<KtFile>, Vec<kotlin_codegen::Diagnostic>) {
176    kotlin_codegen::merge_files_with(broken_files(), &ValidationPolicy::warn_all())
177        .expect("warnings never stop generation")
178}
Source

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}
Source

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

Source§

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)

Performs copy-assignment from source. Read more
Source§

impl Debug for ValidationPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ValidationPolicy

Source§

fn default() -> ValidationPolicy

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.