rhai/src/api/options.rs

158 lines
4.9 KiB
Rust
Raw Normal View History

2021-12-03 04:16:35 +01:00
//! Settings for [`Engine`]'s language options.
use crate::Engine;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
/// A type containing all language options for the [`Engine`].
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct LanguageOptions {
/// Is `if`-expression allowed?
pub allow_if_expr: bool,
/// Is `switch` expression allowed?
pub allow_switch_expr: bool,
/// Is statement-expression allowed?
pub allow_stmt_expr: bool,
/// Is anonymous function allowed?
#[cfg(not(feature = "no_function"))]
pub allow_anonymous_fn: bool,
2021-12-03 04:24:38 +01:00
/// Is looping allowed?
2022-02-04 06:20:47 +01:00
pub allow_looping: bool,
/// Is variables shadowing allowed?
pub allow_shadowing: bool,
2022-02-09 06:12:43 +01:00
/// Strict variables mode?
pub strict_var: bool,
/// Raise error if an object map property does not exist?
/// Returns `()` if `false`.
#[cfg(not(feature = "no_object"))]
pub fail_on_invalid_map_property: bool,
2021-12-03 04:16:35 +01:00
}
impl LanguageOptions {
/// Create a new [`Options`] with default values.
#[inline(always)]
pub const fn new() -> Self {
Self {
allow_if_expr: true,
allow_switch_expr: true,
allow_stmt_expr: true,
2021-12-03 04:24:38 +01:00
#[cfg(not(feature = "no_function"))]
2021-12-03 04:16:35 +01:00
allow_anonymous_fn: true,
2022-02-04 06:20:47 +01:00
allow_looping: true,
2021-12-04 10:57:28 +01:00
strict_var: false,
2022-02-04 06:20:47 +01:00
allow_shadowing: true,
2022-02-09 06:12:43 +01:00
#[cfg(not(feature = "no_object"))]
fail_on_invalid_map_property: false,
2021-12-03 04:16:35 +01:00
}
}
}
impl Default for LanguageOptions {
fn default() -> Self {
Self::new()
}
}
impl Engine {
/// Is `if`-expression allowed?
2022-02-09 06:12:43 +01:00
/// Default is `true`.
2021-12-03 04:16:35 +01:00
#[inline(always)]
2022-02-24 03:36:20 +01:00
pub const fn allow_if_expression(&self) -> bool {
2021-12-03 04:16:35 +01:00
self.options.allow_if_expr
}
/// Set whether `if`-expression is allowed.
#[inline(always)]
pub fn set_allow_if_expression(&mut self, enable: bool) {
self.options.allow_if_expr = enable;
}
/// Is `switch` expression allowed?
2022-02-09 06:12:43 +01:00
/// Default is `true`.
2021-12-03 04:16:35 +01:00
#[inline(always)]
2022-02-24 03:36:20 +01:00
pub const fn allow_switch_expression(&self) -> bool {
2021-12-03 04:16:35 +01:00
self.options.allow_switch_expr
}
/// Set whether `switch` expression is allowed.
#[inline(always)]
pub fn set_allow_switch_expression(&mut self, enable: bool) {
self.options.allow_switch_expr = enable;
}
/// Is statement-expression allowed?
2022-02-09 06:12:43 +01:00
/// Default is `true`.
2021-12-03 04:16:35 +01:00
#[inline(always)]
2022-02-24 03:36:20 +01:00
pub const fn allow_statement_expression(&self) -> bool {
2021-12-03 04:16:35 +01:00
self.options.allow_stmt_expr
}
/// Set whether statement-expression is allowed.
#[inline(always)]
pub fn set_allow_statement_expression(&mut self, enable: bool) {
self.options.allow_stmt_expr = enable;
}
/// Is anonymous function allowed?
2022-02-09 06:12:43 +01:00
/// Default is `true`.
2021-12-03 04:16:35 +01:00
///
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
2022-02-24 03:36:20 +01:00
pub const fn allow_anonymous_fn(&self) -> bool {
2021-12-03 04:16:35 +01:00
self.options.allow_anonymous_fn
}
/// Set whether anonymous function is allowed.
///
/// Not available under `no_function`.
#[cfg(not(feature = "no_function"))]
#[inline(always)]
pub fn set_allow_anonymous_fn(&mut self, enable: bool) {
self.options.allow_anonymous_fn = enable;
}
2021-12-03 04:24:38 +01:00
/// Is looping allowed?
2022-02-09 06:12:43 +01:00
/// Default is `true`.
2021-12-03 04:24:38 +01:00
#[inline(always)]
2022-02-24 03:36:20 +01:00
pub const fn allow_looping(&self) -> bool {
2022-02-04 06:20:47 +01:00
self.options.allow_looping
2021-12-03 04:24:38 +01:00
}
/// Set whether looping is allowed.
#[inline(always)]
pub fn set_allow_looping(&mut self, enable: bool) {
2022-02-04 06:20:47 +01:00
self.options.allow_looping = enable;
2021-12-03 04:24:38 +01:00
}
2022-02-09 06:12:43 +01:00
/// Is variables shadowing allowed?
/// Default is `true`.
#[inline(always)]
2022-02-24 03:36:20 +01:00
pub const fn allow_shadowing(&self) -> bool {
2022-02-09 06:12:43 +01:00
self.options.allow_shadowing
}
/// Set whether variables shadowing is allowed.
#[inline(always)]
pub fn set_allow_shadowing(&mut self, enable: bool) {
self.options.allow_shadowing = enable;
}
2021-12-04 10:57:28 +01:00
/// Is strict variables mode enabled?
2022-02-09 06:12:43 +01:00
/// Default is `false`.
2021-12-04 10:57:28 +01:00
#[inline(always)]
2022-02-24 03:36:20 +01:00
pub const fn strict_variables(&self) -> bool {
2021-12-04 10:57:28 +01:00
self.options.strict_var
}
/// Set whether strict variables mode is enabled.
#[inline(always)]
pub fn set_strict_variables(&mut self, enable: bool) {
self.options.strict_var = enable;
}
2022-02-09 06:12:43 +01:00
/// Raise error if an object map property does not exist?
/// Default is `false`.
///
/// Not available under `no_object`.
#[cfg(not(feature = "no_object"))]
2022-02-04 06:20:47 +01:00
#[inline(always)]
2022-02-24 03:36:20 +01:00
pub const fn fail_on_invalid_map_property(&self) -> bool {
2022-02-09 06:12:43 +01:00
self.options.fail_on_invalid_map_property
2022-02-04 06:20:47 +01:00
}
2022-02-09 06:12:43 +01:00
/// Set whether to raise error if an object map property does not exist.
///
/// Not available under `no_object`.
#[cfg(not(feature = "no_object"))]
2022-02-04 06:20:47 +01:00
#[inline(always)]
2022-02-09 06:12:43 +01:00
pub fn set_fail_on_invalid_map_property(&mut self, enable: bool) {
self.options.fail_on_invalid_map_property = enable;
2022-02-04 06:20:47 +01:00
}
2021-12-03 04:16:35 +01:00
}