uqa_sql/semantics/parameters/
definition.rs1use super::units::ParameterUnit;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq)]
13pub enum ParameterContext {
14 Internal,
16 Superuser,
18 User,
20}
21
22impl ParameterContext {
23 pub const fn name(self) -> &'static str {
25 match self {
26 Self::Internal => "internal",
27 Self::Superuser => "superuser",
28 Self::User => "user",
29 }
30 }
31}
32
33#[derive(Clone, Copy, Debug)]
35pub struct EnumOption {
36 pub name: &'static str,
37 pub value: u8,
38 pub hidden: bool,
40}
41
42impl EnumOption {
43 pub const fn listed(name: &'static str, value: u8) -> Self {
44 Self {
45 name,
46 value,
47 hidden: false,
48 }
49 }
50
51 pub const fn hidden(name: &'static str, value: u8) -> Self {
52 Self {
53 name,
54 value,
55 hidden: true,
56 }
57 }
58}
59
60#[derive(Clone, Copy, Debug)]
62pub enum ParameterKind {
63 Bool {
64 boot: bool,
65 },
66 Integer {
67 boot: i32,
68 min: i32,
69 max: i32,
70 unit: Option<ParameterUnit>,
71 },
72 Enum {
73 boot: u8,
74 options: &'static [EnumOption],
75 },
76 String {
77 boot: &'static str,
78 },
79}
80
81impl ParameterKind {
82 pub const fn type_name(&self) -> &'static str {
84 match self {
85 Self::Bool { .. } => "bool",
86 Self::Integer { .. } => "integer",
87 Self::Enum { .. } => "enum",
88 Self::String { .. } => "string",
89 }
90 }
91}
92
93#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
95pub struct ParameterFlags(u16);
96
97impl ParameterFlags {
98 pub const NONE: Self = Self(0);
99 pub const LIST_INPUT: Self = Self(1 << 0);
101 pub const LIST_QUOTE: Self = Self(1 << 1);
103 pub const NO_SHOW_ALL: Self = Self(1 << 2);
105 pub const NO_RESET: Self = Self(1 << 3);
107 pub const NO_RESET_ALL: Self = Self(1 << 4);
109 pub const REPORT: Self = Self(1 << 5);
111 pub const IS_NAME: Self = Self(1 << 6);
113 pub const EXPLAIN: Self = Self(1 << 7);
115
116 pub const fn union(self, other: Self) -> Self {
117 Self(self.0 | other.0)
118 }
119
120 pub const fn contains(self, other: Self) -> bool {
121 self.0 & other.0 == other.0
122 }
123}
124
125#[derive(Clone, Copy, Debug)]
127pub struct ParameterDefinition {
128 pub name: &'static str,
130 pub kind: ParameterKind,
131 pub context: ParameterContext,
132 pub category: &'static str,
133 pub short_desc: &'static str,
134 pub extra_desc: Option<&'static str>,
135 pub flags: ParameterFlags,
136 pub library: Option<&'static str>,
138}
139
140impl ParameterDefinition {
141 pub fn has_flag(&self, flag: ParameterFlags) -> bool {
142 self.flags.contains(flag)
143 }
144
145 pub fn boot_setting(&self) -> String {
147 self.boot_setting_with_control(&uqa_core::memory::ProductionControl::uncontrolled())
148 .expect("ordinary boot setting")
149 .into_uncontrolled()
150 .expect("ordinary boot setting")
151 }
152
153 pub fn boot_setting_with_control(
154 &self,
155 control: &uqa_core::memory::ProductionControl<'_>,
156 ) -> Result<uqa_core::memory::Produced<String>, crate::SQLError> {
157 Ok(match self.kind {
158 ParameterKind::Bool { boot } => control.copy_text(if boot { "on" } else { "off" })?,
159 ParameterKind::Integer { boot, .. } => control.format(format_args!("{boot}"))?,
160 ParameterKind::Enum { boot, options } => {
161 control.copy_text(super::value::enum_display(options, boot))?
162 }
163 ParameterKind::String { boot } => control.copy_text(boot)?,
164 })
165 }
166
167 pub fn unit(&self) -> Option<ParameterUnit> {
169 match self.kind {
170 ParameterKind::Integer { unit, .. } => unit,
171 _ => None,
172 }
173 }
174}