From 3cd43b9374d0a3f90d972e96e11065bc3696a4ae Mon Sep 17 00:00:00 2001 From: TcMits Date: Wed, 27 Aug 2025 13:22:40 +0700 Subject: [PATCH] no need to return error in fmt --- parser/src/ast/fmt.rs | 41 ++++++++++++++++++++++++----------------- parser/src/error.rs | 3 --- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/parser/src/ast/fmt.rs b/parser/src/ast/fmt.rs index b15cbb12a..224d942cf 100644 --- a/parser/src/ast/fmt.rs +++ b/parser/src/ast/fmt.rs @@ -2,7 +2,6 @@ use std::fmt::{self, Display, Formatter, Write}; use crate::ast::*; -use crate::error::Error; use crate::token::TokenType; use crate::token::TokenType::*; use crate::Result; @@ -26,15 +25,26 @@ pub trait ToSqlContext { None } - /// Helper to get a flat column name - /// If the column exists and has a name, return the name - /// If the column exists but has no name, return the index as string - fn get_flat_column_name(&self, table_id: TableInternalId, col_idx: usize) -> Option { - match self.get_column_name(table_id, col_idx) { - Some(Some(name)) => Some(name.to_string()), - Some(None) => Some(format!("{col_idx}")), - None => None, - } + // help function to handle missing table/column names + fn get_table_and_column_names( + &self, + table_id: TableInternalId, + col_idx: usize, + ) -> (String, String) { + let table_name = self + .get_table_name(table_id) + .map(|s| s.to_owned()) + .unwrap_or_else(|| format!("t{}", table_id.0)); + + let column_name = self + .get_column_name(table_id, col_idx) + .map(|opt| { + opt.map(|s| s.to_owned()) + .unwrap_or_else(|| format!("c{}", col_idx)) + }) + .unwrap_or_else(|| format!("c{}", col_idx)); + + (table_name, column_name) } } @@ -54,9 +64,9 @@ impl<'a, T: Write> WriteTokenStream<'a, T> { } impl TokenStream for WriteTokenStream<'_, T> { - type Error = Error; + type Error = fmt::Error; - fn append(&mut self, ty: TokenType, value: Option<&str>) -> Result<()> { + fn append(&mut self, ty: TokenType, value: Option<&str>) -> fmt::Result { if !self.spaced { match ty { TK_COMMA | TK_SEMI | TK_RP | TK_DOT => {} @@ -77,9 +87,6 @@ impl TokenStream for WriteTokenStream<'_, T> { self.write.write_char('\'')?; Ok(()) } - (_, None, None) => Err(Error::Custom( - "can not format both none ty and none value".to_string(), - )), (_, ty_str, value) => { if let Some(str) = ty_str { self.write.write_str(str)?; @@ -139,7 +146,7 @@ impl<'a, 'b, C: ToSqlContext, T: ToTokens> SqlDisplayer<'a, 'b, C, T> { } // Return string representation with context - pub fn to_string(&self) -> Result { + pub fn to_string(&self) -> Result { let mut s = String::new(); let mut stream = WriteTokenStream::new(&mut s); self.start_node.to_tokens(&mut stream, self.ctx)?; @@ -174,7 +181,7 @@ pub trait ToTokens { } // Return string representation with blank context - fn format(&self) -> Result + fn format(&self) -> Result where Self: Sized, { diff --git a/parser/src/error.rs b/parser/src/error.rs index 23e0f4b4b..27fe46956 100644 --- a/parser/src/error.rs +++ b/parser/src/error.rs @@ -52,7 +52,4 @@ pub enum Error { // Custom error message #[error("{0}")] Custom(String), - // formatting error - #[error("{0}")] - FormatError(#[from] std::fmt::Error), }