sqltk_parser/ast/helpers/
key_value_options.rs1#[cfg(not(feature = "std"))]
22use alloc::string::String;
23#[cfg(not(feature = "std"))]
24use alloc::vec::Vec;
25use core::fmt;
26use core::fmt::Formatter;
27
28#[cfg(feature = "serde")]
29use serde::{Deserialize, Serialize};
30
31#[cfg(feature = "visitor")]
32use sqltk_parser_derive::{Visit, VisitMut};
33
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
35#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
36#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
37pub struct KeyValueOptions {
38 pub options: Vec<KeyValueOption>,
39}
40
41#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
42#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
43#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
44pub enum KeyValueOptionType {
45 STRING,
46 BOOLEAN,
47 ENUM,
48 NUMBER,
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
52#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
53#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
54pub struct KeyValueOption {
55 pub option_name: String,
56 pub option_type: KeyValueOptionType,
57 pub value: String,
58}
59
60impl fmt::Display for KeyValueOptions {
61 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
62 if !self.options.is_empty() {
63 let mut first = false;
64 for option in &self.options {
65 if !first {
66 first = true;
67 } else {
68 f.write_str(" ")?;
69 }
70 write!(f, "{}", option)?;
71 }
72 }
73 Ok(())
74 }
75}
76
77impl fmt::Display for KeyValueOption {
78 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
79 match self.option_type {
80 KeyValueOptionType::STRING => {
81 write!(f, "{}='{}'", self.option_name, self.value)?;
82 }
83 KeyValueOptionType::ENUM | KeyValueOptionType::BOOLEAN | KeyValueOptionType::NUMBER => {
84 write!(f, "{}={}", self.option_name, self.value)?;
85 }
86 }
87 Ok(())
88 }
89}