1+ # =========================================================================
2+ # Ceedling - Test-Centered Build System for C
3+ # ThrowTheSwitch.org
4+ # Copyright (c) 2010-25 Mike Karlesky, Mark VanderVoord, & Greg Williams
5+ # SPDX-License-Identifier: MIT
6+ # =========================================================================
7+
8+ class MixinStandardizer
9+
10+ constructor :reportinator
11+
12+ def setup
13+ # ...
14+ end
15+
16+ def smart_standardize ( config :, mixin :, notices :)
17+ modified = false
18+ modified |= smart_standardize_defines ( config , mixin , notices )
19+ modified |= smart_standardize_flags ( config , mixin , notices )
20+ return modified
21+ end
22+
23+ ### Private
24+
25+ private
26+
27+ def smart_standardize_defines ( config , mixin , notices )
28+ modified = false
29+
30+ # Bail out if config and mixin do noth both have :defines
31+ return false unless config [ :defines ] && mixin [ :defines ]
32+
33+ # Iterate over :defines ↳ <context> keys
34+ # If both config and mixin contain the same key paths, process their (matcher) values
35+ config [ :defines ] . each do |context , context_hash |
36+ if mixin [ :defines ] [ context ]
37+
38+ # Standardize :defines ↳ <context> matcher conventions if they differ so they can be merged later
39+ standardized , notice = standardize_matchers (
40+ config [ :defines ] [ context ] ,
41+ mixin [ :defines ] [ context ] ,
42+ config [ :defines ] ,
43+ mixin [ :defines ] ,
44+ context
45+ )
46+
47+ if standardized
48+ path , _ = @reportinator . generate_config_walk ( [ :defines , context ] )
49+ _notice = "At #{ path } : #{ notice } "
50+ notices . push ( _notice )
51+ end
52+
53+ modified |= standardized
54+ end
55+ end
56+
57+ return modified
58+ end
59+
60+ def smart_standardize_flags ( config , mixin , notices )
61+ modified = false
62+
63+ # Bail out if config and mixin do noth both have :flags
64+ return false unless config [ :flags ] && mixin [ :flags ]
65+
66+ # Iterate over :flags ↳ <context> ↳ <operation> keys
67+ # If both config and mixin contain the same key paths, process their (matcher) values
68+ config [ :flags ] . each do |context , context_hash |
69+ next unless mixin [ :flags ] [ context ]
70+
71+ context_hash . each do |operation , operation_hash |
72+ if mixin [ :flags ] [ context ] [ operation ]
73+
74+ # Standardize :flags ↳ <context> ↳ <operation> matcher conventions if they differ so they can be merged later
75+ standardized , notice = standardize_matchers (
76+ config [ :flags ] [ context ] [ operation ] ,
77+ mixin [ :flags ] [ context ] [ operation ] ,
78+ config [ :flags ] [ context ] ,
79+ mixin [ :flags ] [ context ] ,
80+ operation
81+ )
82+
83+ if standardized
84+ path , _ = @reportinator . generate_config_walk ( [ :flags , context , operation ] )
85+ _notice = "At #{ path } : #{ notice } "
86+ notices . push ( _notice )
87+ end
88+
89+ modified |= standardized
90+ end
91+ end
92+ end
93+
94+ return modified
95+ end
96+
97+ def standardize_matchers ( config_value , mixin_value , config_parent , mixin_parent , key )
98+ # If both values are the same type, do nothing
99+ return false , nil if ( config_value . class == mixin_value . class )
100+
101+ # Promote mixin value list to all-matches matcher hash
102+ if config_value . is_a? ( Hash ) && mixin_value . is_a? ( Array )
103+ # Ensure all-matches matcher key is a symbol and not a string
104+ config_value [ :* ] = value if value = config_value . delete ( '*' )
105+
106+ # Replace the value of a simple array list with a matcher hash that stores the original list
107+ mixin_parent [ key ] = { :* => mixin_value }
108+ return true , 'Converted mixin list to matcher hash to facilitate merging with configuration'
109+ end
110+
111+ # Promote config value list to all-matches matcher hash
112+ if config_value . is_a? ( Array ) && mixin_value . is_a? ( Hash )
113+ # Ensure all-matches matcher key is a symbol and not a string
114+ mixin_value [ :* ] = value if value = mixin_value . delete ( '*' )
115+
116+ # Replace the value of a simple array list with a matcher hash that stores the original list
117+ config_parent [ key ] = { :* => config_value }
118+ return true , 'Converted configuration list to matcher hash to facilitate merging with mixin'
119+ end
120+
121+ return false , nil
122+ end
123+ end
0 commit comments