2626use super :: geom:: GeomType ;
2727use super :: Layer ;
2828use crate :: plot:: scale:: ScaleTypeKind ;
29- use crate :: plot:: { Mappings , Scale } ;
29+ use crate :: plot:: { AestheticValue , Mappings , Scale } ;
3030
3131/// Orientation value for aligned/vertical orientation.
3232pub const ALIGNED : & str = "aligned" ;
@@ -215,7 +215,7 @@ fn is_discrete_scale(scale: &Scale) -> bool {
215215 } )
216216}
217217
218- /// Swap positional aesthetic pairs in layer mappings .
218+ /// Swap positional aesthetic pairs in an aesthetics map .
219219///
220220/// Swaps the following pairs:
221221/// - pos1 ↔ pos2
@@ -224,52 +224,27 @@ fn is_discrete_scale(scale: &Scale) -> bool {
224224/// - pos1end ↔ pos2end
225225/// - pos1offset ↔ pos2offset
226226///
227- /// This is called before stat transforms for Secondary orientation layers,
228- /// so stats always see "standard" orientation. After stat transforms,
229- /// this is called again to flip back to the correct output positions.
230- pub fn flip_mappings ( layer : & mut Layer ) {
231- let pairs = [
227+ /// Used for both mappings and remappings when handling transposed orientation.
228+ pub fn flip_positional_aesthetics (
229+ aesthetics : & mut std :: collections :: HashMap < String , AestheticValue > ,
230+ ) {
231+ const PAIRS : [ ( & str , & str ) ; 5 ] = [
232232 ( "pos1" , "pos2" ) ,
233233 ( "pos1min" , "pos2min" ) ,
234234 ( "pos1max" , "pos2max" ) ,
235235 ( "pos1end" , "pos2end" ) ,
236236 ( "pos1offset" , "pos2offset" ) ,
237237 ] ;
238238
239- for ( a, b) in pairs {
240- let val_a = layer . mappings . aesthetics . remove ( a) ;
241- let val_b = layer . mappings . aesthetics . remove ( b) ;
239+ for ( a, b) in PAIRS {
240+ let val_a = aesthetics. remove ( a) ;
241+ let val_b = aesthetics. remove ( b) ;
242242
243243 if let Some ( v) = val_a {
244- layer . mappings . aesthetics . insert ( b. to_string ( ) , v) ;
244+ aesthetics. insert ( b. to_string ( ) , v) ;
245245 }
246246 if let Some ( v) = val_b {
247- layer. mappings . aesthetics . insert ( a. to_string ( ) , v) ;
248- }
249- }
250- }
251-
252- /// Swap positional aesthetic pairs in remappings.
253- ///
254- /// Same as flip_mappings but for remappings (stat output mappings).
255- pub fn flip_remappings ( layer : & mut Layer ) {
256- let pairs = [
257- ( "pos1" , "pos2" ) ,
258- ( "pos1min" , "pos2min" ) ,
259- ( "pos1max" , "pos2max" ) ,
260- ( "pos1end" , "pos2end" ) ,
261- ( "pos1offset" , "pos2offset" ) ,
262- ] ;
263-
264- for ( a, b) in pairs {
265- let val_a = layer. remappings . aesthetics . remove ( a) ;
266- let val_b = layer. remappings . aesthetics . remove ( b) ;
267-
268- if let Some ( v) = val_a {
269- layer. remappings . aesthetics . insert ( b. to_string ( ) , v) ;
270- }
271- if let Some ( v) = val_b {
272- layer. remappings . aesthetics . insert ( a. to_string ( ) , v) ;
247+ aesthetics. insert ( a. to_string ( ) , v) ;
273248 }
274249 }
275250}
@@ -411,7 +386,7 @@ mod tests {
411386 }
412387
413388 #[ test]
414- fn test_flip_mappings ( ) {
389+ fn test_flip_positional_aesthetics ( ) {
415390 let mut layer = Layer :: new ( Geom :: bar ( ) ) ;
416391 layer. mappings . insert (
417392 "pos1" . to_string ( ) ,
@@ -426,7 +401,7 @@ mod tests {
426401 AestheticValue :: standard_column ( "x2" . to_string ( ) ) ,
427402 ) ;
428403
429- flip_mappings ( & mut layer) ;
404+ flip_positional_aesthetics ( & mut layer. mappings . aesthetics ) ;
430405
431406 // pos1 ↔ pos2
432407 assert_eq ! (
@@ -446,23 +421,23 @@ mod tests {
446421 }
447422
448423 #[ test]
449- fn test_flip_mappings_empty ( ) {
424+ fn test_flip_positional_aesthetics_empty ( ) {
450425 let mut layer = Layer :: new ( Geom :: point ( ) ) ;
451426 // No crash with empty mappings
452- flip_mappings ( & mut layer) ;
427+ flip_positional_aesthetics ( & mut layer. mappings . aesthetics ) ;
453428 assert ! ( layer. mappings. aesthetics. is_empty( ) ) ;
454429 }
455430
456431 #[ test]
457- fn test_flip_mappings_partial ( ) {
432+ fn test_flip_positional_aesthetics_partial ( ) {
458433 let mut layer = Layer :: new ( Geom :: bar ( ) ) ;
459434 // Only pos1 mapped
460435 layer. mappings . insert (
461436 "pos1" . to_string ( ) ,
462437 AestheticValue :: standard_column ( "x" . to_string ( ) ) ,
463438 ) ;
464439
465- flip_mappings ( & mut layer) ;
440+ flip_positional_aesthetics ( & mut layer. mappings . aesthetics ) ;
466441
467442 // pos1 moves to pos2
468443 assert ! ( layer. mappings. get( "pos1" ) . is_none( ) ) ;
0 commit comments