1 module gherkin.datatable;
2 
3 import std.array : array, empty, replace;
4 
5 import asdf : serializationIgnore, serializationIgnoreOutIf;
6 import gherkin.comment : Comment;
7 import gherkin.location : Location;
8 
9 ///
10 struct Cell
11 {
12     ///
13     @serializationIgnoreOutIf!`a.empty` string value;
14     ///
15     Location location;
16 
17     ///
18     void replace(string from, string to)
19     {
20         this.value = this.value.replace(from, to);
21     }
22 }
23 
24 ///
25 struct TableRow
26 {
27     ///
28     string id;
29     ///
30     Cell[] cells;
31     ///
32     Location location;
33     ///
34     @serializationIgnore Comment[] comments;
35 
36     ///
37     @serializationIgnore @property bool empty() const
38     {
39         return cells.empty;
40     }
41 
42     ///
43     void replace(string from, string to)
44     {
45         foreach (ref cell; this.cells)
46         {
47             cell.replace(from, to);
48         }
49     }
50 
51     ///
52     this(ref return scope TableRow rhs)
53     {
54         this.id = rhs.id;
55         this.cells = rhs.cells.array.dup;
56         this.location = rhs.location;
57         this.comments = rhs.comments.dup;
58     }
59 
60     ///
61     this(ref return scope inout TableRow rhs) inout
62     {
63         foreach (i, ref inout field; rhs.tupleof)
64         {
65             this.tupleof[i] = field;
66         }
67     }
68 
69     ///
70     this(string id, Cell[] cells, Location location, ref Comment[] comments)
71     {
72         this.id = id;
73         this.cells = cells.array.dup;
74         this.location = location;
75         this.comments = comments.dup;
76         comments = [];
77     }
78 }
79 
80 ///
81 struct DataTable
82 {
83     ///
84     TableRow[] rows;
85     ///
86     Location location;
87 
88     ///
89     @serializationIgnore @property bool empty() const
90     {
91         return rows.empty;
92     }
93 
94     ///
95     void replace(string from, string to)
96     {
97         foreach (ref tableRow; this.rows)
98         {
99             tableRow.replace(from, to);
100         }
101     }
102 
103     ///
104     this(ref return scope DataTable rhs)
105     {
106         this.rows = rhs.rows.array.dup;
107         this.location = rhs.location;
108     }
109 
110     ///
111     this(ref return scope inout DataTable rhs) inout
112     {
113         foreach (i, ref inout field; rhs.tupleof)
114         {
115             this.tupleof[i] = field;
116         }
117     }
118 
119     ///
120     this(TableRow[] rows, Location location)
121     {
122         this.rows = rows.array.dup;
123         this.location = location;
124     }
125 }