1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
use super::*;
#[cfg(feature = "pretty-print")]
mod display;
/// `guard a > 0 { ... }`
///
/// The else block must use control.
///
/// ```vk
///
/// assert a > 0 {
/// panic!("a must be greater than 0");
/// }
///
/// assert let Some(a) = b {
/// panic!("b must be Some");
/// }
///
///
///
/// if a < 0 {
/// return error;
/// }
/// do_something_else();
/// ```
///
/// `guard let Failure(error) = e if xxx then { ... }`
///
/// The else block must use control.
///
/// ```vk
/// if e.is_failure() {
/// let error = x.as_failure();
/// return error;
/// }
/// do_something_else();
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GuardStatement {
/// The condition to check
pub negative: bool,
/// The condition to check
pub condition: ExpressionNode,
/// same as if condition
pub main_body: GuardPattern,
/// The range of the node
pub span: Range<u32>,
}
impl ValkyrieNode for GuardStatement {
fn get_range(&self) -> Range<usize> {
Range { start: self.span.start as usize, end: self.span.end as usize }
}
}
/// `guard <CONDITION> then { ... } else { ... }`
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum GuardPattern {
/// Same as if condition
Expression(ExpressionNode),
/// Same as if !condition
List(ElseStatement),
/// Same as if !condition
Dict(ElseStatement),
}
impl GuardStatement {}