risingwave_sqlparser/ast/
legacy_source.rs1use std::fmt;
19
20use winnow::ModalResult;
21
22use crate::ast::FormatEncodeOptions;
23use crate::parser::{Parser, StrError};
24use crate::parser_err;
25
26#[derive(Debug, Clone, PartialEq, Eq, Hash)]
27pub enum CompatibleFormatEncode {
28 V2(FormatEncodeOptions),
29}
30
31impl fmt::Display for CompatibleFormatEncode {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 CompatibleFormatEncode::V2(inner) => {
35 write!(f, "{}", inner)
36 }
37 }
38 }
39}
40
41impl CompatibleFormatEncode {
42 pub(crate) fn into_v2(self) -> FormatEncodeOptions {
43 match self {
44 CompatibleFormatEncode::V2(inner) => inner,
45 }
46 }
47}
48
49impl From<FormatEncodeOptions> for CompatibleFormatEncode {
50 fn from(value: FormatEncodeOptions) -> Self {
51 Self::V2(value)
52 }
53}
54
55pub fn parse_format_encode(p: &mut Parser<'_>) -> ModalResult<CompatibleFormatEncode> {
56 if let Some(schema_v2) = p.parse_schema()? {
57 if schema_v2.key_encode.is_some() {
58 parser_err!("key encode clause is not supported in source schema");
59 }
60 Ok(CompatibleFormatEncode::V2(schema_v2))
61 } else {
62 p.expected("description of the format")
63 }
64}
65
66pub fn get_delimiter(chars: &str) -> Result<u8, StrError> {
67 match chars {
68 "," => Ok(b','), ";" => Ok(b';'), "\t" => Ok(b'\t'), other => Err(StrError(format!(
72 "The delimiter should be one of ',', ';', E'\\t', but got {other:?}",
73 ))),
74 }
75}