@@ -101,49 +101,46 @@ pub trait Style {
101101}
102102
103103/// A style implementation that relegates all formatting to CSS via the "class" style options.
104+ #[ derive( Clone , Copy , Debug , Hash , PartialEq , Eq ) ]
104105pub struct CssStyle ;
105106
106107/// A simple style implementation that doesn't require CSS.
108+ #[ derive( Clone , Copy , Debug , Hash , PartialEq , Eq ) ]
107109pub struct SimpleStyle ;
108110
109111impl Style for CssStyle {
110112 fn node ( & self , info : & StateInfo , active : bool ) -> NodeStyle {
111113 let mut classes = Vec :: new ( ) ;
112- if active {
113- classes. push ( "active" ) ;
114- }
115- if !info. children ( ) . is_empty ( ) {
116- classes. push ( "parent" ) ;
117- }
118114 if info. is_stack_pop {
119115 classes. push ( "stack-pop" ) ;
116+ } else if info. children ( ) . is_empty ( ) {
117+ classes. push ( "simple" ) ;
118+ } else {
119+ classes. push ( "parent" ) ;
120+ }
121+ if active {
122+ classes. push ( "active" ) ;
120123 }
121124 NodeStyle {
122125 // TODO Not sure if the "active" attribute is "semantic" or purely for rendering style.
123126 // If it's purely style, then we should not enable it since we'll set the style in CSS.
124127 // active,
125- class : if classes. is_empty ( ) {
126- None
127- } else {
128- Some ( classes. join ( " " ) )
129- } ,
128+ class : Some ( classes. join ( " " ) ) ,
130129 ..NodeStyle :: default ( )
131130 }
132131 }
133132 fn edge ( & self , info : & TransitionInfo , active : bool ) -> EdgeStyle {
134133 let mut classes = Vec :: new ( ) ;
135- if active {
136- classes. push ( "active" ) ;
137- }
138134 if info. is_change_state ( ) {
139135 classes. push ( "change-state" ) ;
136+ } else {
137+ classes. push ( "standard" ) ;
138+ }
139+ if active {
140+ classes. push ( "active" ) ;
140141 }
141142 EdgeStyle {
142- class : if classes. is_empty ( ) {
143- None
144- } else {
145- Some ( classes. join ( " " ) )
146- } ,
143+ class : Some ( classes. join ( " " ) ) ,
147144 ..EdgeStyle :: default ( )
148145 }
149146 }
@@ -175,20 +172,21 @@ impl Style for SimpleStyle {
175172}
176173
177174/// Generates smcat diagrams from Frame state machines.
178- pub struct Renderer {
179- style : Box < dyn Style > ,
175+ #[ derive( Clone ) ]
176+ pub struct Renderer < S : Style > {
177+ style : S ,
180178}
181179
182- impl Renderer {
180+ impl < S : Style > Renderer < S > {
183181 /// Create a new renderer with the given style configuration.
184- pub fn new ( style : Box < dyn Style > ) -> Self {
182+ pub fn new ( style : S ) -> Self {
185183 Renderer { style }
186184 }
187185
188186 /// Generate an smcat diagram illustrating the structure of a state machine, independent of any
189187 /// particular execution.
190188 pub fn render_static ( & self , machine_info : & MachineInfo ) -> String {
191- self . render_common ( machine_info, None , None )
189+ self . render ( machine_info, None , None )
192190 }
193191
194192 /// Generate an smcat diagram from a snapshot of a running state machine. Depending on the
@@ -207,10 +205,12 @@ impl Renderer {
207205 . transition_history ( )
208206 . newest ( )
209207 . map ( |t| t. info . id ) ;
210- self . render_common ( machine_info, Some ( active_state) , last_transition)
208+ self . render ( machine_info, Some ( active_state) , last_transition)
211209 }
212210
213- pub fn render_common (
211+ /// Generate an smcat diagram, highlighing the given active state and last transition (if
212+ /// provided) according to the associated style configurating.
213+ pub fn render (
214214 & self ,
215215 machine_info : & MachineInfo ,
216216 active_state : Option < & ' static str > ,
@@ -226,11 +226,11 @@ impl Renderer {
226226 & machine_info. top_level_states ( ) ,
227227 & mut output,
228228 ) ;
229- output. push_str ( "; \n " ) ;
229+ output. push ( '\n' ) ;
230230
231231 // render transitions
232232 if let Some ( init) = machine_info. initial_state ( ) {
233- output. push_str ( & format ! ( "initial = > {};\n " , init. name) ) ;
233+ output. push_str ( & format ! ( "initial - > {};\n " , init. name) ) ;
234234 }
235235 for transition in machine_info. transitions {
236236 self . render_transition ( last_transition, transition, & mut output) ;
@@ -259,6 +259,8 @@ impl Renderer {
259259 }
260260 if state_iter. peek ( ) . is_some ( ) {
261261 output. push_str ( ",\n " ) ;
262+ } else {
263+ output. push_str ( ";\n " ) ;
262264 }
263265 }
264266 }
0 commit comments