11namespace d2 ;
22
3- public record class D2Diagram (
4- IEnumerable < D2Shape > Shapes ,
5- IEnumerable < D2Connection > Connections
6- )
3+ /// <summary>
4+ /// An ordered D2 document. Statement order is retained exactly, which is
5+ /// significant for features such as sequence diagrams and composition boards.
6+ /// </summary>
7+ public record class D2Diagram
78{
8- public D2Diagram Add ( D2Shape shape )
9- => this with { Shapes = Shapes . Append ( shape ) } ;
10- public D2Diagram Add ( D2Connection connection )
11- => this with { Connections = Connections . Append ( connection ) } ;
9+ private readonly IReadOnlyList < D2Statement > _statements ;
1210
13- internal IEnumerable < string > Lines ( )
11+ public IReadOnlyList < D2Statement > Statements => _statements ;
12+
13+ public IEnumerable < D2Shape > Shapes => _statements . OfType < D2Shape > ( ) ;
14+
15+ public IEnumerable < D2Connection > Connections => _statements . OfType < D2Connection > ( ) ;
16+
17+ public D2Diagram ( IEnumerable < D2Shape > Shapes , IEnumerable < D2Connection > Connections )
18+ : this ( Combine ( Shapes , Connections ) )
19+ {
20+ }
21+
22+ public D2Diagram ( IEnumerable < D2Statement > statements )
1423 {
15- var shapes = Shapes . SelectMany ( s => s . Lines ( ) ) . ToList ( ) ;
16- var connections = Connections . SelectMany ( c => c . Lines ( ) ) . ToList ( ) ;
24+ if ( statements is null )
25+ {
26+ throw new ArgumentNullException ( nameof ( statements ) ) ;
27+ }
28+
29+ var materialized = statements . ToList ( ) ;
30+ if ( materialized . Any ( statement => statement is null ) )
31+ {
32+ throw new ArgumentException ( "A diagram cannot contain a null statement." , nameof ( statements ) ) ;
33+ }
34+
35+ _statements = materialized ;
36+ }
1737
18- return shapes . Concat ( connections ) ;
38+ public D2Diagram Add ( D2Shape shape ) => Add ( ( D2Statement ) shape ) ;
39+
40+ public D2Diagram Add ( D2Connection connection ) => Add ( ( D2Statement ) connection ) ;
41+
42+ public D2Diagram Add ( D2Statement statement )
43+ {
44+ if ( statement is null )
45+ {
46+ throw new ArgumentNullException ( nameof ( statement ) ) ;
47+ }
48+ return new D2Diagram ( _statements . Append ( statement ) ) ;
1949 }
2050
21- public override string ToString ( )
22- => string . Join ( Environment . NewLine , Lines ( ) ) ;
23- }
51+ internal IEnumerable < string > Lines ( )
52+ => _statements . SelectMany ( statement => statement . Lines ( ) ) ;
53+
54+ public override string ToString ( ) => string . Join ( Environment . NewLine , Lines ( ) ) ;
55+
56+ private static IEnumerable < D2Statement > Combine (
57+ IEnumerable < D2Shape > shapes ,
58+ IEnumerable < D2Connection > connections )
59+ {
60+ if ( shapes is null )
61+ {
62+ throw new ArgumentNullException ( nameof ( shapes ) ) ;
63+ }
64+
65+ if ( connections is null )
66+ {
67+ throw new ArgumentNullException ( nameof ( connections ) ) ;
68+ }
69+
70+ return shapes . Cast < D2Statement > ( ) . Concat ( connections ) ;
71+ }
72+ }
0 commit comments