summaryrefslogtreecommitdiff
path: root/src/table.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2022-08-25 21:09:25 +1200
committerBen Bridle <bridle.benjamin@gmail.com>2022-08-25 21:09:25 +1200
commit54f5e9fd883e207931baa9c87b6181ca724d6bab (patch)
tree17111a1da036dbc061ae4062ea0716373e16e23d /src/table.rs
downloadmarkdown-54f5e9fd883e207931baa9c87b6181ca724d6bab.zip
Initial commit
Diffstat (limited to 'src/table.rs')
-rw-r--r--src/table.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/table.rs b/src/table.rs
new file mode 100644
index 0000000..cc01ffc
--- /dev/null
+++ b/src/table.rs
@@ -0,0 +1,60 @@
+use crate::Line;
+
+pub struct Table {
+ pub columns: Vec<Column>,
+ pub rows: Vec<Vec<Line>>,
+}
+
+pub struct Column {
+ pub name: Line,
+ pub alignment: Alignment,
+}
+
+pub enum Alignment {
+ Left,
+ Center,
+ Right,
+}
+impl Alignment {
+ pub fn from_str(s: &str) -> Result<Self, ()> {
+ let mut start = false;
+ let mut end = false;
+ for (i, c) in s.chars().enumerate() {
+ if c == ':' {
+ if i == 0 {
+ start = true;
+ } else if i == s.len() - 1 {
+ end = true;
+ } else {
+ return Err(());
+ }
+ } else if c != '-' {
+ return Err(());
+ }
+ }
+ Ok(match (start, end) {
+ (false, false) => Self::Left,
+ (true, false) => Self::Left,
+ (false, true) => Self::Right,
+ (true, true) => Self::Center,
+ })
+ }
+}
+impl std::fmt::Display for Alignment {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+ f.write_str(match self {
+ Self::Left => "left",
+ Self::Center => "center",
+ Self::Right => "right",
+ })
+ }
+}
+impl std::fmt::Debug for Alignment {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+ f.write_str(match self {
+ Self::Left => "Left",
+ Self::Center => "Center",
+ Self::Right => "Right",
+ })
+ }
+}