substrait_validator/output/
parse_result.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Module for the toplevel type representing a parse/validation result.
4
5use crate::export;
6use crate::output::diagnostic;
7use crate::output::tree;
8
9/// Validity of a plan.
10///
11/// Note that there is a one-to-one correspondence with Level. The only
12/// difference between Level and Validity is that the variant names for Level
13/// are more sensible in the context of a diagnostic, while the names for
14/// Validity are more sensible when talking about a validation result as a
15/// whole.
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum Validity {
18    /// The plan is valid.
19    Valid,
20
21    /// The plan may or may not be valid; the validator was not able to prove
22    /// or disprove validity.
23    MaybeValid,
24
25    /// The plan is invalid.
26    Invalid,
27}
28
29impl From<diagnostic::Level> for Validity {
30    fn from(level: diagnostic::Level) -> Self {
31        match level {
32            diagnostic::Level::Info => Validity::Valid,
33            diagnostic::Level::Warning => Validity::MaybeValid,
34            diagnostic::Level::Error => Validity::Invalid,
35        }
36    }
37}
38
39impl From<Validity> for diagnostic::Level {
40    fn from(validity: Validity) -> Self {
41        match validity {
42            Validity::Valid => diagnostic::Level::Info,
43            Validity::MaybeValid => diagnostic::Level::Warning,
44            Validity::Invalid => diagnostic::Level::Error,
45        }
46    }
47}
48
49/// Representation of a parse/validation result.
50pub struct ParseResult {
51    /// The root node of the tree.
52    pub root: tree::Node,
53}
54
55impl ParseResult {
56    /// Iterates over all diagnostic messages in the tree.
57    pub fn iter_diagnostics(&self) -> impl Iterator<Item = &diagnostic::Diagnostic> + '_ {
58        self.root.iter_diagnostics()
59    }
60
61    /// Returns the first diagnostic of the highest severity level in the tree.
62    pub fn get_diagnostic(&self) -> Option<&diagnostic::Diagnostic> {
63        self.root.get_diagnostic()
64    }
65
66    /// Returns whether the plan represented by the given parse tree is valid.
67    pub fn check(&self) -> Validity {
68        if let Some(diag) = self.get_diagnostic() {
69            diag.adjusted_level.into()
70        } else {
71            Validity::Valid
72        }
73    }
74
75    /// Exports a parse tree to a file or other output device using the specified
76    /// data format.
77    pub fn export<T: std::io::Write>(
78        &self,
79        out: &mut T,
80        format: export::Format,
81    ) -> std::io::Result<()> {
82        export::export(out, format, "plan", self)
83    }
84}