Repository: zhiburt/tabled
Pull request #68 (2022-02-11 13:06:33)

Rename #[header], #[field] to #[tabled]


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index b4d35c6..5bae931 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,7 +24,7 @@ maintenance = { status = "actively-developed" }
 color = ["papergrid/color", "ansi-str"]
 
 [dependencies]
-tabled_derive = "0.2.0"
+tabled_derive = { path = "./tabled_derive" }
 papergrid = { path = "./papergrid" }
 ansi-str = { version = "0.1.1", optional = true }
 
diff --git a/examples/basic.rs b/examples/basic.rs
index 91f9e31..8811ed5 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -6,7 +6,7 @@ use tabled::{object::Rows, Alignment, AlignmentHorizontal, Modify, Style, Table,
 #[derive(Tabled)]
 struct Distribution {
     name: &'static str,
-    #[field(display_with = "Self::display_based_on")]
+    #[tabled(display_with = "Self::display_based_on")]
     based_on: Option<&'static str>,
     is_active: bool,
     is_cool: bool,
diff --git a/examples/expanded_display.rs b/examples/expanded_display.rs
index 973fd7d..b513f06 100644
--- a/examples/expanded_display.rs
+++ b/examples/expanded_display.rs
@@ -6,7 +6,7 @@ use tabled::{display::ExpandedDisplay, Tabled};
 #[derive(Tabled)]
 struct Distribution {
     name: &'static str,
-    #[field(display_with = "Self::display_based_on")]
+    #[tabled(display_with = "Self::display_based_on")]
     based_on: Option<&'static str>,
     is_active: bool,
     is_cool: bool,
diff --git a/examples/inline.rs b/examples/inline.rs
index 6540c9f..b11da3b 100644
--- a/examples/inline.rs
+++ b/examples/inline.rs
@@ -52,9 +52,9 @@ fn main() {
 #[derive(Tabled)]
 struct User {
     id: usize,
-    #[header(inline)]
+    #[tabled(inline)]
     personal_information: Person,
-    #[header(inline)]
+    #[tabled(inline)]
     contact: Contact,
 }
 
@@ -62,7 +62,7 @@ struct User {
 struct Person {
     name: &'static str,
     surname: &'static str,
-    #[field(display_with = "display_age")]
+    #[tabled(display_with = "display_age")]
     age: Option<usize>,
 }
 
@@ -75,13 +75,13 @@ fn display_age(age: &Option<usize>) -> String {
 
 #[derive(Tabled)]
 enum Contact {
-    #[field(inline("telegram::"))]
+    #[tabled(inline("telegram::"))]
     Telegram {
         username: &'static str,
         number: &'static str,
     },
-    #[header(hidden)]
+    #[tabled(hidden)]
     No,
-    #[header(inline)]
-    Number(#[header("number")] &'static str),
+    #[tabled(inline)]
+    Number(#[tabled("number")] &'static str),
 }
diff --git a/src/lib.rs b/src/lib.rs
index 295e7e4..5563522 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -121,7 +121,7 @@
 //! }
 //!
 //! #[derive(Tabled)]
-//! struct Developer(#[header("name")] &'static str);
+//! struct Developer(#[tabled(rename = "name")] &'static str);
 //!     
 //! let data = vec![
 //!     (Developer("Terri Kshlerin"), Domain::Embeded),
diff --git a/tabled_derive/src/lib.rs b/tabled_derive/src/lib.rs
index 55ac58e..84071a4 100644
--- a/tabled_derive/src/lib.rs
+++ b/tabled_derive/src/lib.rs
@@ -8,7 +8,7 @@ use syn::{
     Ident, Index, Lit, Meta, NestedMeta, Type, Variant,
 };
 
-#[proc_macro_derive(Tabled, attributes(header, field))]
+#[proc_macro_derive(Tabled, attributes(tabled))]
 pub fn tabled(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
     let input = parse_macro_input!(input as DeriveInput);
     let ast = impl_tabled(&input);
@@ -166,7 +166,11 @@ fn field_headers(
     prefix: &str,
 ) -> TokenStream {
     if attributes.inline {
-        return get_type_headers(&field.ty, &attributes.inline_prefix, "");
+        let prefix = attributes
+            .inline_prefix
+            .as_ref()
+            .map_or_else(|| "", |s| s.as_str());
+        return get_type_headers(&field.ty, prefix, "");
     }
 
     let header_name = field_header_name(field, attributes, index);
@@ -206,7 +210,11 @@ fn collect_info_enum(ast: &DataEnum) -> Result<Impl, String> {
 
 fn info_from_variant(variant: &Variant, attributes: &Attributes) -> Result<Impl, String> {
     if attributes.inline {
-        return info_from_fields(&variant.fields, variant_var_name, &attributes.inline_prefix);
+        let prefix = attributes
+            .inline_prefix
+            .as_ref()
+            .map_or_else(|| "", |s| s.as_str());
+        return info_from_fields(&variant.fields, variant_var_name, prefix);
     }
 
     let variant_name = variant_name(variant, attributes);
@@ -385,12 +393,12 @@ fn field_header_name(f: &Field, attr: &Attributes, index: usize) -> String {
     }
 }
 
-// It would be cool to create a library for a parsing attributes
+// todo: make String a &static str
 #[derive(Debug)]
 struct Attributes {
-    hidden: bool,
+    is_ignored: bool,
     inline: bool,
-    inline_prefix: String,
+    inline_prefix: Option<String>,
     name: Option<String>,
     display_with: Option<String>,
 }
@@ -405,7 +413,7 @@ impl Attributes {
 
         Self {
             display_with,
-            hidden: is_ignored,
+            is_ignored,
             inline: should_be_inlined,
             inline_prefix,
             name: override_header_name,
@@ -413,41 +421,33 @@ impl Attributes {
     }
 
     fn is_ignored(&self) -> bool {
-        self.hidden
+        self.is_ignored
     }
 }
 
 fn override_header_name(attrs: &[Attribute]) -> Option<String> {
-    find_name_attribute(attrs, "header", "name", look_up_nested_meta_str)
-        .or_else(|| find_name_attribute(attrs, "header", "name", look_up_nested_meta_flag_str))
+    find_name_attribute(attrs, "tabled", "rename", look_up_nested_meta_str)
 }
 
 fn check_display_with_func(attrs: &[Attribute]) -> Option<String> {
-    find_name_attribute(attrs, "field", "display_with", look_up_nested_meta_str)
+    find_name_attribute(attrs, "tabled", "display_with", look_up_nested_meta_str)
 }
 
 fn should_be_inlined(attrs: &[Attribute]) -> bool {
-    let inline_attr = find_name_attribute(attrs, "header", "inline", look_up_nested_meta_bool)
-        .or_else(|| find_name_attribute(attrs, "field", "inline", look_up_nested_meta_bool))
+    let inline_attr = find_name_attribute(attrs, "tabled", "inline", look_up_nested_meta_bool)
         .or_else(|| {
-            find_name_attribute(attrs, "header", "inline", look_up_nested_flag_str_in_attr)
-                .map(|_| true)
-        })
-        .or_else(|| {
-            find_name_attribute(attrs, "field", "inline", look_up_nested_flag_str_in_attr)
+            find_name_attribute(attrs, "tabled", "inline", look_up_nested_flag_str_in_attr)
                 .map(|_| true)
         });
     inline_attr == Some(true)
 }
 
-fn look_for_inline_prefix(attrs: &[Attribute]) -> String {
-    find_name_attribute(attrs, "header", "inline", look_up_nested_flag_str_in_attr)
-        .or_else(|| find_name_attribute(attrs, "field", "inline", look_up_nested_flag_str_in_attr))
-        .unwrap_or_else(|| "".to_owned())
+fn look_for_inline_prefix(attrs: &[Attribute]) -> Option<String> {
+    find_name_attribute(attrs, "tabled", "inline", look_up_nested_flag_str_in_attr)
 }
 
 fn attrs_has_ignore_sign(attrs: &[Attribute]) -> bool {
-    let is_ignored = find_name_attribute(attrs, "header", "hidden", look_up_nested_meta_bool);
+    let is_ignored = find_name_attribute(attrs, "tabled", "skip", look_up_nested_meta_bool);
     is_ignored == Some(true)
 }
 


Test patch:
diff --git a/tests/derive_test.rs b/tests/derive_test.rs
index 9ca9688..0087375 100644
--- a/tests/derive_test.rs
+++ b/tests/derive_test.rs
@@ -4,9 +4,9 @@ mod tupple_structure {
     use super::*;
 
     #[test]
-    fn rename_field() {
+    fn rename_tabled() {
         #[derive(Tabled)]
-        struct St(u8, #[header("field 2")] &'static str);
+        struct St(u8, #[tabled(rename = "field 2")] &'static str);
 
         let st = St(0, "123");
 
@@ -15,11 +15,11 @@ mod tupple_structure {
     }
 
     #[test]
-    fn hide_field() {
+    fn skip_tabled() {
         #[derive(Tabled)]
         struct St(
-            #[header(hidden = true)] u8,
-            #[header("field 2", hidden)] &'static str,
+            #[tabled(skip)] u8,
+            #[tabled(rename = "field 2", skip)] &'static str,
             &'static str,
         );
 
@@ -43,7 +43,7 @@ mod tupple_structure {
         #[derive(Tabled)]
         struct St(
             u8,
-            #[field(display_with = "display_option")] Option<&'static str>,
+            #[tabled(display_with = "display_option")] Option<&'static str>,
         );
 
         let st = St(0, Some("v2"));
@@ -58,7 +58,7 @@ mod tupple_structure {
         #[derive(Tabled)]
         struct St(
             u8,
-            #[field(display_with = "Self::display_option")] Option<&'static str>,
+            #[tabled(display_with = "Self::display_option")] Option<&'static str>,
         );
 
         impl St {
@@ -193,12 +193,12 @@ mod enum_ {
         #[allow(dead_code)]
         #[derive(Tabled)]
         enum E {
-            #[header("Variant 1")]
+            #[tabled(rename = "Variant 1")]
             A {
                 a: u8,
                 b: i32,
             },
-            #[header(name = "Variant 2")]
+            #[tabled(rename = "Variant 2")]
             B(String),
             K,
         }
@@ -214,7 +214,7 @@ mod enum_ {
     }
 
     #[test]
-    fn hide_variant() {
+    fn skip_variant() {
         #[allow(dead_code)]
         #[derive(Tabled)]
         enum E {
@@ -222,7 +222,7 @@ mod enum_ {
                 a: u8,
                 b: i32,
             },
-            #[header(hidden = true)]
+            #[tabled(skip)]
             B(String),
             K,
         }
@@ -241,14 +241,17 @@ mod enum_ {
     fn inline_variant() {
         #[derive(Tabled)]
         enum Vehicle {
-            #[header(inline("Auto::"))]
+            #[tabled(inline("Auto::"))]
             Auto {
-                #[header("mod")]
+                #[tabled(rename = "mod")]
                 model: &'static str,
                 engine: &'static str,
             },
-            #[header(inline)]
-            Bikecycle(#[header("name")] &'static str, #[header(inline)] Bike),
+            #[tabled(inline)]
+            Bikecycle(
+                #[tabled(rename = "name")] &'static str,
+                #[tabled(inline)] Bike,
+            ),
             Skateboard,
         }
 
@@ -323,10 +326,10 @@ mod enum_ {
     fn inline_field_with_display_function() {
         #[derive(Tabled)]
         enum Developer {
-            #[header(inline("backend::"))]
+            #[tabled(inline("backend::"))]
             Backend {
-                #[header("name")]
-                #[field(display_with = "display")]
+                #[tabled(rename = "name")]
+                #[tabled(display_with = "display")]
                 specific: &'static str,
             },
             Frontend,
@@ -354,8 +357,8 @@ mod enum_ {
         #[allow(dead_code)]
         #[derive(Tabled)]
         enum Fact {
-            #[field(inline)]
-            Known(#[field(display_with = "Self::format::<4>")] &'static str),
+            #[tabled(inline)]
+            Known(#[tabled(display_with = "Self::format::<4>")] &'static str),
             Unknown,
         }
 
@@ -418,12 +421,12 @@ mod structure {
     }
 
     #[test]
-    fn rename_field() {
+    fn rename_tabled() {
         #[derive(Tabled)]
         struct St {
-            #[header(name = "field 1")]
+            #[tabled(rename = "field 1")]
             f1: u8,
-            #[header("field 2")]
+            #[tabled(rename = "field 2")]
             f2: &'static str,
         }
 
@@ -437,12 +440,35 @@ mod structure {
 
     #[allow(dead_code)]
     #[test]
-    fn hide_field() {
+    fn skip_tabled() {
         #[derive(Tabled)]
         struct St {
-            #[header(hidden = true)]
+            #[tabled(skip)]
             f1: u8,
-            #[header("field 2", hidden)]
+            #[tabled(rename = "field 2", skip)]
+            f2: &'static str,
+            f3: &'static str,
+        }
+
+        let st = St {
+            f1: 0,
+            f2: "v2",
+            f3: "123",
+        };
+
+        assert_eq!(vec!["123".to_owned()], st.fields());
+        assert_eq!(vec!["f3".to_owned()], St::headers());
+        assert_eq!(St::LENGTH, 1);
+    }
+
+    #[allow(dead_code)]
+    #[test]
+    fn skip_true_tabled() {
+        #[derive(Tabled)]
+        struct St {
+            #[tabled(skip = true)]
+            f1: u8,
+            #[tabled(rename = "field 2", skip = true)]
             f2: &'static str,
             f3: &'static str,
         }
@@ -462,10 +488,10 @@ mod structure {
     fn inline() {
         #[derive(Tabled)]
         struct Person {
-            #[header(inline = true)]
+            #[tabled(inline = true)]
             id: u8,
             name: &'static str,
-            #[header(inline)]
+            #[tabled(inline)]
             ed: Education,
         }
 
@@ -510,10 +536,10 @@ mod structure {
     fn inline_with_rename_prefix() {
         #[derive(Tabled)]
         struct Person {
-            #[header("it's an ignored option", inline)] // does nothing
+            #[tabled(rename = "it's an ignored option", inline)] // does nothing
             id: u8,
             name: &'static str,
-            #[header(inline("education::"))]
+            #[tabled(inline("education::"))]
             ed: Education,
         }
 
@@ -565,7 +591,7 @@ mod structure {
         #[derive(Tabled)]
         struct St {
             f1: u8,
-            #[field(display_with = "display_option")]
+            #[tabled(display_with = "display_option")]
             f2: Option<&'static str>,
         }
 
@@ -584,7 +610,7 @@ mod structure {
         #[derive(Tabled)]
         struct St {
             f1: u8,
-            #[field(display_with = "Self::display_option")]
+            #[tabled(display_with = "Self::display_option")]
             f2: Option<&'static str>,
         }
 
@@ -608,12 +634,12 @@ mod structure {
 }
 
 #[test]
-fn hidden_fields_may_not_implement_display() {
+fn skipped_fields_may_not_implement_display() {
     {
         struct Something;
 
         #[derive(Tabled)]
-        struct TupleStruct(#[header(hidden = true)] Something, &'static str);
+        struct TupleStruct(#[tabled(skip)] Something, &'static str);
 
         let st = TupleStruct(Something, "nrdxp");
 
@@ -626,7 +652,7 @@ fn hidden_fields_may_not_implement_display() {
 
         #[derive(Tabled)]
         struct Struct {
-            #[header(hidden = true)]
+            #[tabled(skip)]
             _gem: Something,
             name: &'static str,
         }
@@ -645,7 +671,7 @@ fn hidden_fields_may_not_implement_display() {
 
         #[derive(Tabled)]
         struct Struct1 {
-            #[header(hidden = true)]
+            #[tabled(skip = true)]
             _field1: Something,
             field2: &'static str,
         }
@@ -653,7 +679,7 @@ fn hidden_fields_may_not_implement_display() {
         #[derive(Tabled)]
         struct Struct2 {
             field1: &'static str,
-            #[field(inline)]
+            #[tabled(inline)]
             field2: Struct1,
         }
 
@@ -677,19 +703,19 @@ fn hidden_fields_may_not_implement_display() {
 
         #[derive(Tabled)]
         enum Enum {
-            #[field(inline("A::"))]
+            #[tabled(inline("A::"))]
             A {
                 name: &'static str,
             },
-            #[field(inline("B::"))]
+            #[tabled(inline("B::"))]
             B {
                 issue: usize,
-                #[header(hidden = true)]
+                #[tabled(skip)]
                 _gem: Something,
                 name: &'static str,
             },
-            #[field(inline("C::"))]
-            C(usize, #[header(hidden = true)] Something, &'static str),
+            #[tabled(inline("C::"))]
+            C(usize, #[tabled(skip)] Something, &'static str),
             D,
         }
 
diff --git a/tests/table_test.rs b/tests/table_test.rs
index 75f9712..9b76c0c 100644
--- a/tests/table_test.rs
+++ b/tests/table_test.rs
@@ -444,10 +444,10 @@ fn table_tuple_with_structure_vec() {
 
 #[allow(dead_code)]
 #[test]
-fn table_vector_structures_with_hidden_field() {
+fn table_vector_structures_with_hidden_tabled() {
     #[derive(Tabled)]
     struct St {
-        #[header(hidden = true)]
+        #[tabled(skip)]
         f1: u8,
         f2: &'static str,
     }
@@ -522,7 +522,7 @@ fn table_enum_with_hidden_variant() {
             lang: u8,
         },
         Consonant(char),
-        #[header(hidden)]
+        #[tabled(skip)]
         Digit,
     }
 
@@ -713,7 +713,7 @@ fn tuple_combination() {
     }
 
     #[derive(Tabled)]
-    struct Developer(#[header("name")] &'static str);
+    struct Developer(#[tabled(rename = "name")] &'static str);
 
     let data = vec![
         (Developer("Terri Kshlerin"), Domain::Embeded),
@@ -748,7 +748,7 @@ fn table_trait() {
     }
 
     #[derive(Tabled)]
-    struct Developer(#[header("name")] &'static str);
+    struct Developer(#[tabled(rename = "name")] &'static str);
 
     let data = vec![
         (Developer("Terri Kshlerin"), Domain::Embeded),