@@ -28,6 +28,7 @@ use helpers::{
2828 stmt_data_loading:: { FileStagingCommand , StageLoadSelectItemKind } ,
2929} ;
3030
31+ use core:: cmp:: Ordering ;
3132use core:: ops:: Deref ;
3233use core:: {
3334 fmt:: { self , Display } ,
@@ -173,7 +174,7 @@ fn format_statement_list(f: &mut fmt::Formatter, statements: &[Statement]) -> fm
173174}
174175
175176/// An identifier, decomposed into its value or character data and the quote style.
176- #[ derive( Debug , Clone , PartialOrd , Ord ) ]
177+ #[ derive( Debug , Clone ) ]
177178#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
178179#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
179180pub struct Ident {
@@ -215,6 +216,35 @@ impl core::hash::Hash for Ident {
215216
216217impl Eq for Ident { }
217218
219+ impl PartialOrd for Ident {
220+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
221+ Some ( self . cmp ( other) )
222+ }
223+ }
224+
225+ impl Ord for Ident {
226+ fn cmp ( & self , other : & Self ) -> Ordering {
227+ let Ident {
228+ value,
229+ quote_style,
230+ // exhaustiveness check; we ignore spans in ordering
231+ span : _,
232+ } = self ;
233+
234+ let Ident {
235+ value : other_value,
236+ quote_style : other_quote_style,
237+ // exhaustiveness check; we ignore spans in ordering
238+ span : _,
239+ } = other;
240+
241+ // First compare by value, then by quote_style
242+ value
243+ . cmp ( other_value)
244+ . then_with ( || quote_style. cmp ( other_quote_style) )
245+ }
246+ }
247+
218248impl Ident {
219249 /// Create a new identifier with the given value and no quotes and an empty span.
220250 pub fn new < S > ( value : S ) -> Self
@@ -4215,7 +4245,7 @@ pub enum Statement {
42154245 /// ```sql
42164246 /// NOTIFY channel [ , payload ]
42174247 /// ```
4218- /// send a notification event together with an optional “ payload” string to channel
4248+ /// send a notification event together with an optional " payload" string to channel
42194249 ///
42204250 /// See Postgres <https://www.postgresql.org/docs/current/sql-notify.html>
42214251 NOTIFY {
@@ -9772,6 +9802,8 @@ impl fmt::Display for NullInclusion {
97729802
97739803#[ cfg( test) ]
97749804mod tests {
9805+ use crate :: tokenizer:: Location ;
9806+
97759807 use super :: * ;
97769808
97779809 #[ test]
@@ -10067,4 +10099,16 @@ mod tests {
1006710099 test_steps ( OneOrManyWithParens :: Many ( vec ! [ 2 ] ) , vec ! [ 2 ] , 3 ) ;
1006810100 test_steps ( OneOrManyWithParens :: Many ( vec ! [ 3 , 4 ] ) , vec ! [ 3 , 4 ] , 4 ) ;
1006910101 }
10102+
10103+ // Tests that the position in the code of an `Ident` does not affect its
10104+ // ordering.
10105+ #[ test]
10106+ fn test_ident_ord ( ) {
10107+ let mut a = Ident :: with_span ( Span :: new ( Location :: new ( 1 , 1 ) , Location :: new ( 1 , 1 ) ) , "a" ) ;
10108+ let mut b = Ident :: with_span ( Span :: new ( Location :: new ( 2 , 2 ) , Location :: new ( 2 , 2 ) ) , "b" ) ;
10109+
10110+ assert ! ( a < b) ;
10111+ std:: mem:: swap ( & mut a. span , & mut b. span ) ;
10112+ assert ! ( a < b) ;
10113+ }
1007010114}
0 commit comments