Repository: zhiburt/tabled
Pull request #305 (2023-03-13 19:34:06)

static_table/ Fix output


Patch:
diff --git a/static_table/Cargo.toml b/static_table/Cargo.toml
index 5ceff4d..ce4d17d 100644
--- a/static_table/Cargo.toml
+++ b/static_table/Cargo.toml
@@ -1,9 +1,9 @@
 [package]
 name = "static_table"
-version = "0.1.0"
+version = "0.0.0"
 authors = ["Maxim Zhiburt <zhiburt@gmail.com>"]
 edition = "2018"
-description = "Derive macros which is used by tabled crate"
+description = "Library creates pretty tables at compiler time"
 repository = "https://github.com/zhiburt/tabled"
 license = "Unlicense/MIT"
 
diff --git a/static_table/README.md b/static_table/README.md
index 96fa80f..33988c2 100644
--- a/static_table/README.md
+++ b/static_table/README.md
@@ -34,3 +34,20 @@ Output
 | Rust | Graydon Hoare  | 2010          |
 +------+----------------+---------------+
 ```
+
+You can even use the macros in the documentation
+
+```rust
+/// Add joins 2 integers together to get a sum.
+/// 
+/// ```
+#[doc = static_table::static_table!([
+    ["a", "b", "result"],
+    ["1", '2', '3'],
+    ["2", '2', '4']
+])]
+/// ```
+pub fn add(left: usize, right: usize) -> usize {
+    left + right
+}
+```
diff --git a/static_table/examples/use_in_docs/Cargo.toml b/static_table/examples/use_in_docs/Cargo.toml
new file mode 100644
index 0000000..4e1cfa3
--- /dev/null
+++ b/static_table/examples/use_in_docs/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "use_in_docs"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[workspace]
+
+[dependencies]
+static_table = { path = "../.." }
\ No newline at end of file
diff --git a/static_table/examples/use_in_docs/src/lib.rs b/static_table/examples/use_in_docs/src/lib.rs
new file mode 100644
index 0000000..aedd081
--- /dev/null
+++ b/static_table/examples/use_in_docs/src/lib.rs
@@ -0,0 +1,22 @@
+//! # The table
+//!
+//! ```
+#![doc = static_table::static_table!([
+    ["a", "b", "result"],
+    ["1", '2', '3'],
+    ["2", '2', '4']
+])]
+//! ```
+
+/// Add joins 2 integers together to get a sum.
+/// 
+/// ```
+#[doc = static_table::static_table!([
+    ["a", "b", "result"],
+    ["1", '2', '3'],
+    ["2", '2', '4']
+])]
+/// ```
+pub fn add(left: usize, right: usize) -> usize {
+    left + right
+}
diff --git a/static_table/src/lib.rs b/static_table/src/lib.rs
index 644af5a..ad7dc0c 100644
--- a/static_table/src/lib.rs
+++ b/static_table/src/lib.rs
@@ -489,9 +489,14 @@ impl<T: Parse> Parse for Pad<T> {
 
 fn expr_lit_to_string(expr_lit: &ExprLit) -> String {
     match &expr_lit.lit {
-        Lit::Str(s) => s.value(),
-        Lit::ByteStr(s) => format!("{:?}", s.value()),
-        lit => lit.to_token_stream().to_string(),
+        Lit::Str(val) => val.value(),
+        Lit::ByteStr(val) => format!("{:?}", val.value()),
+        Lit::Int(val) => val.base10_digits().to_string(),
+        Lit::Float(val) => val.base10_digits().to_string(),
+        Lit::Char(val) => val.value().to_string(),
+        Lit::Byte(val) => val.value().to_string(),
+        Lit::Bool(val) => val.value().to_string(),
+        Lit::Verbatim(val) => val.to_token_stream().to_string(),
     }
 }
 


Test patch:
diff --git a/static_table/tests/static_table.rs b/static_table/tests/static_table.rs
index 6ce10b5..43778d9 100644
--- a/static_table/tests/static_table.rs
+++ b/static_table/tests/static_table.rs
@@ -100,6 +100,193 @@ fn static_table_byte_string() {
     assert_eq!(table, expected);
 }
 
+#[test]
+fn static_table_byte_char() {
+    let table = static_table!([[b'a'], [b'b']]);
+    let expected = "+----+\n\
+                          | 97 |\n\
+                          +----+\n\
+                          | 98 |\n\
+                          +----+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_char() {
+    let table = static_table!([['a'], ['b']]);
+    let expected = "+---+\n\
+                          | a |\n\
+                          +---+\n\
+                          | b |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_bool() {
+    let table = static_table!([[true], [false]]);
+    let expected = "+-------+\n\
+                          | true  |\n\
+                          +-------+\n\
+                          | false |\n\
+                          +-------+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_u64() {
+    let table = static_table!([[0u64], [1u64]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_u32() {
+    let table = static_table!([[0u32], [1u32]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_u16() {
+    let table = static_table!([[0u16], [1u16]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_u8() {
+    let table = static_table!([[0u8], [1u8]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_i64() {
+    let table = static_table!([[0u64], [1u64]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_i32() {
+    let table = static_table!([[0u32], [1u32]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_i16() {
+    let table = static_table!([[0u16], [1u16]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_i8() {
+    let table = static_table!([[0u8], [1u8]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_isize() {
+    let table = static_table!([[0isize], [1isize]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_isize_minus() {
+    let table = static_table!([[-0], [-1isize]]);
+    let expected = "+----+\n\
+                          | -0 |\n\
+                          +----+\n\
+                          | -1 |\n\
+                          +----+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_usize() {
+    let table = static_table!([[0usize], [1usize]]);
+    let expected = "+---+\n\
+                          | 0 |\n\
+                          +---+\n\
+                          | 1 |\n\
+                          +---+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_float32() {
+    let table = static_table!([[0.0f32], [0.1f32]]);
+    let expected = "+-----+\n\
+                          | 0.0 |\n\
+                          +-----+\n\
+                          | 0.1 |\n\
+                          +-----+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_float64() {
+    let table = static_table!([[0.0f64], [0.1f64]]);
+    let expected = "+-----+\n\
+                          | 0.0 |\n\
+                          +-----+\n\
+                          | 0.1 |\n\
+                          +-----+";
+    assert_eq!(table, expected);
+}
+
+#[test]
+fn static_table_float64_minus() {
+    let table = static_table!([[-0.0f64], [0.1f64]]);
+    let expected = "+------+\n\
+                          | -0.0 |\n\
+                          +------+\n\
+                          | 0.1  |\n\
+                          +------+";
+    assert_eq!(table, expected);
+}
+
 #[test]
 fn static_table_dims_differences_0() {
     let table = static_table!([["Hello World"; 2], ["hello"]]);
diff --git a/static_table/tests/syntax.rs b/static_table/tests/syntax.rs
index 30086b0..072fa4a 100644
--- a/static_table/tests/syntax.rs
+++ b/static_table/tests/syntax.rs
@@ -94,6 +94,11 @@ fn static_table_style() {
     static_table!([[1, 2, 123], [1, 2, 123], [1, 2, 123]], THEME = "MODERN");
 }
 
+// #[test(compail_fail)]
+// fn static_table_verbatim() {
+//     static_table!([[asd], [zxc], [1, 2, 123]]);
+// }
+
 // #[test]
 // fn static_table_with_init_rows() {
 //     static_table::static_table!([[1, 1, 3]; 3]);