11use std:: sync:: Arc ;
22
33use super :: sql_entities:: { SqlERData , Table , TableColumn } ;
4- use crate :: { sql_entities :: View , GeneratorConfigOptions , ViewGenerator } ;
4+ use crate :: { Direction , GeneratorConfigOptions , ViewGenerator , sql_entities :: View } ;
55use serde:: Serialize ;
6- use tinytemplate:: { format_unescaped , TinyTemplate } ;
6+ use tinytemplate:: { TinyTemplate , format_unescaped } ;
77
88pub struct PlantUmlDefaultGenerator < ' a > {
99 str_templates : TinyTemplate < ' a > ,
1010}
1111
1212static PUML_TEMPLATE : & str = "@startuml\n \n \
13+ {{ if direction }}{ direction }\n {{ endif }}\
1314 hide circle\n \
1415 hide empty members\n \
1516 skinparam linetype ortho\n \n \
@@ -27,8 +28,7 @@ static VIEW_TEMPLATE: &str =
2728
2829static COLUMN_TEMPLATE : & str = " column({col.name}, \" {col.datatype}\" {{ if is_pk }}, $pk=true{{ endif }}{{ if is_fk }}, $fk=true{{ endif }}{{if is_nn}}, $nn=true{{ endif }})\n " ;
2930
30- static REL_TEMPLATE : & str =
31- "{source_table_name} {{ if is_zero_one_to_one }}|o--||{{else}}}o--||{{ endif }} {target_table_name}\n " ;
31+ static REL_TEMPLATE : & str = "{source_table_name} {{ if is_zero_one_to_one }}|o--||{{else}}}o--||{{ endif }} {target_table_name}\n " ;
3232
3333static ENUM_TEMPLATE : & str =
3434 "enum({name}, \" {{ for v in values}}{{if @last}}{v}{{else}}{v}, {{ endif }}{{ endfor }}\" )\n " ;
@@ -69,6 +69,25 @@ struct SEntity {
6969#[ derive( Serialize ) ]
7070struct SLegend ( String ) ;
7171
72+ #[ derive( Serialize ) ]
73+ struct SDirection ( String ) ;
74+
75+ impl TryFrom < & Direction > for SDirection {
76+ type Error = crate :: SqlantError ;
77+ fn try_from ( value : & Direction ) -> Result < Self , crate :: SqlantError > {
78+ match value {
79+ Direction :: TB => Ok ( Self ( "top to bottom direction" . into ( ) ) ) ,
80+ Direction :: BT => Err ( crate :: SqlantError :: Generator (
81+ "bt (bottom-to-top) direction is not available in plantuml." . into ( ) ,
82+ ) ) ,
83+ Direction :: LR => Ok ( Self ( "left to right direction" . into ( ) ) ) ,
84+ Direction :: RL => Err ( crate :: SqlantError :: Generator (
85+ "rl (right-to-left) direction is not available in plantuml." . into ( ) ,
86+ ) ) ,
87+ }
88+ }
89+ }
90+
7291#[ derive( Serialize ) ]
7392struct SPuml {
7493 puml_lib : String ,
@@ -78,6 +97,7 @@ struct SPuml {
7897 foreign_keys : Vec < String > ,
7998 enums : Vec < String > ,
8099 legend : Option < SLegend > ,
100+ direction : Option < SDirection > ,
81101}
82102
83103#[ derive( Serialize ) ]
@@ -304,6 +324,12 @@ impl ViewGenerator for PlantUmlDefaultGenerator<'_> {
304324 PUML_LIB_INCLUDE . into ( )
305325 } ;
306326
327+ let direction = if let Some ( direction) = & opts. direction {
328+ Some ( SDirection :: try_from ( direction) ?)
329+ } else {
330+ None
331+ } ;
332+
307333 Ok ( self . str_templates . render (
308334 "puml" ,
309335 & SPuml {
@@ -313,6 +339,7 @@ impl ViewGenerator for PlantUmlDefaultGenerator<'_> {
313339 enums,
314340 legend,
315341 views,
342+ direction,
316343 } ,
317344 ) ?)
318345 }
0 commit comments