Skip to content

Commit b75d3e5

Browse files
author
Ilson Roberto Balliego Junior
committed
implement display for WrappedCollection
1 parent eb833cc commit b75d3e5

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/ast/mod.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3500,15 +3500,7 @@ impl fmt::Display for Statement {
35003500
write!(f, " PARTITION BY {partition_by}")?;
35013501
}
35023502
if let Some(cluster_by) = cluster_by.as_ref() {
3503-
write!(f, " CLUSTER BY ")?;
3504-
match cluster_by {
3505-
WrappedCollection::NoWrapping(cluster_by) => {
3506-
write!(f, "{}", display_comma_separated(cluster_by.as_slice()))?;
3507-
}
3508-
WrappedCollection::Parentheses(cluster_by) => {
3509-
write!(f, "({})", display_comma_separated(cluster_by.as_slice()))?;
3510-
}
3511-
}
3503+
write!(f, " CLUSTER BY {cluster_by}")?;
35123504
}
35133505
if let Some(options) = options.as_ref() {
35143506
write!(
@@ -6533,15 +6525,46 @@ impl Display for CommentDef {
65336525
}
65346526
}
65356527

6536-
/// Helper to indicate if a collection should be wrapped by a symbol when displaying
6528+
/// Helper to indicate if a collection should be wrapped by a symbol in the display form
6529+
///
6530+
/// [`Display`] is implemented for every [Vec<T>] where `T: Display`.
6531+
/// The string output is a comma separated list for the vec items
6532+
///
6533+
/// # Examples
6534+
/// ```
6535+
/// # use sqlparser::ast::WrappedCollection;
6536+
/// let items = WrappedCollection::Parentheses(vec!["one", "two", "three"]);
6537+
/// assert_eq!("(one, two, three)", items.to_string());
6538+
///
6539+
/// let items = WrappedCollection::NoWrapping(vec!["one", "two", "three"]);
6540+
/// assert_eq!("one, two, three", items.to_string());
6541+
/// ```
65376542
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
65386543
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
65396544
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
65406545
pub enum WrappedCollection<T> {
6546+
/// Print the collection without wrapping symbols, as `item, item, item`
65416547
NoWrapping(T),
6548+
/// Wraps the collection in Parentheses, as `(item, item, item)`
65426549
Parentheses(T),
65436550
}
65446551

6552+
impl<T> Display for WrappedCollection<Vec<T>>
6553+
where
6554+
T: Display,
6555+
{
6556+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6557+
match self {
6558+
WrappedCollection::NoWrapping(inner) => {
6559+
write!(f, "{}", display_comma_separated(inner.as_slice()))
6560+
}
6561+
WrappedCollection::Parentheses(inner) => {
6562+
write!(f, "({})", display_comma_separated(inner.as_slice()))
6563+
}
6564+
}
6565+
}
6566+
}
6567+
65456568
#[cfg(test)]
65466569
mod tests {
65476570
use super::*;

0 commit comments

Comments
 (0)