json2pdf_client/protocol/elements/
table.rs

1use crate::protocol::elements::Element;
2use crate::BorderSettings;
3use serde::Serialize;
4
5/// A table
6#[derive(Debug, Default, Clone, Serialize)]
7#[serde(rename_all = "camelCase")]
8pub struct Table {
9    rows: Vec<Row>,
10    #[serde(skip_serializing_if = "Option::is_none")]
11    point_layout: Option<Vec<f32>>,
12}
13
14impl Table {
15    /// Create an empty table
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Add a row to the table
21    pub fn add_row(mut self, row: Row) -> Self {
22        self.rows.push(row);
23        self
24    }
25
26    /// Set all rows in the table
27    pub fn set_rows(mut self, rows: Vec<Row>) -> Self {
28        self.rows = rows;
29        self
30    }
31
32    /// The the point layout for the table.
33    /// Refer to the [iText documentation](https://api.itextpdf.com/iText7/java/7.1.1/com/itextpdf/layout/element/Table.html#Table-float:A-)
34    pub fn set_point_layout(mut self, layout: Vec<f32>) -> Self {
35        self.point_layout = Some(layout);
36        self
37    }
38}
39
40#[allow(clippy::from_over_into)]
41impl Into<Element> for Table {
42    fn into(self) -> Element {
43        Element::table(self)
44    }
45}
46
47/// A row in a table
48#[derive(Debug, Default, Clone, Serialize)]
49pub struct Row {
50    columns: Vec<Column>,
51}
52
53impl Row {
54    /// Create an empty row
55    pub fn new() -> Self {
56        Self::default()
57    }
58
59    /// Add a column to the row
60    pub fn add_column(mut self, column: Column) -> Self {
61        self.columns.push(column);
62        self
63    }
64
65    /// Set all columns in the row
66    pub fn set_columns(mut self, columns: Vec<Column>) -> Self {
67        self.columns = columns;
68        self
69    }
70}
71
72/// A column in a row
73#[derive(Debug, Default, Clone, Serialize)]
74#[serde(rename_all = "camelCase")]
75pub struct Column {
76    border_settings: Option<BorderSettings>,
77    elements: Vec<Element>,
78}
79
80impl Column {
81    /// Create an empty column
82    pub fn new() -> Self {
83        Self::default()
84    }
85
86    /// Add an element to the column
87    pub fn add_element(mut self, element: Element) -> Self {
88        self.elements.push(element);
89        self
90    }
91
92    /// Set the elements in the column
93    pub fn set_elements(mut self, elements: Vec<Element>) -> Self {
94        self.elements = elements;
95        self
96    }
97
98    /// Set the border settings for the column
99    pub fn set_border(mut self, border: BorderSettings) -> Self {
100        self.border_settings = Some(border);
101        self
102    }
103}