summaryrefslogtreecommitdiffstats
path: root/vendor/tabled/examples/derive
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-07 05:48:48 +0000
commitef24de24a82fe681581cc130f342363c47c0969a (patch)
tree0d494f7e1a38b95c92426f58fe6eaa877303a86c /vendor/tabled/examples/derive
parentReleasing progress-linux version 1.74.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-ef24de24a82fe681581cc130f342363c47c0969a.tar.xz
rustc-ef24de24a82fe681581cc130f342363c47c0969a.zip
Merging upstream version 1.75.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/tabled/examples/derive')
-rw-r--r--vendor/tabled/examples/derive/display_with.rs65
-rw-r--r--vendor/tabled/examples/derive/inline.rs55
-rw-r--r--vendor/tabled/examples/derive/inline_enum.rs50
-rw-r--r--vendor/tabled/examples/derive/order.rs48
-rw-r--r--vendor/tabled/examples/derive/rename.rs45
-rw-r--r--vendor/tabled/examples/derive/rename_all.rs56
-rw-r--r--vendor/tabled/examples/derive/skip.rs49
7 files changed, 368 insertions, 0 deletions
diff --git a/vendor/tabled/examples/derive/display_with.rs b/vendor/tabled/examples/derive/display_with.rs
new file mode 100644
index 000000000..9dfc87a47
--- /dev/null
+++ b/vendor/tabled/examples/derive/display_with.rs
@@ -0,0 +1,65 @@
+//! This example demonstrates using the [attribute macro](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
+//! [`display_with`] to seamlessly augment field representations in a [`Table`] display.
+//!
+//! * [`display_with`] functions act as transformers during [`Table`] instantiation.
+//!
+//! * Note how [`display_with`] works with [std] and custom functions alike.
+//!
+//! * [`display_with`] attributes can be constructed in two ways (shown below).
+//!
+//! * Attribute arguments can be directly overridden with static values, effectively ignoring the
+//! augmented fields natural value entirely. Even an entire object can be passed as context with `self`.
+
+use std::borrow::Cow;
+
+use tabled::{Table, Tabled};
+
+#[derive(Tabled)]
+#[tabled(rename_all = "camelCase")]
+struct Country {
+ name: &'static str,
+ capital_city: &'static str,
+ #[tabled(display_with("display_perimeter", self))]
+ surface_area_km2: f32,
+ #[tabled(display_with = "str::to_lowercase")]
+ national_currency: &'static str,
+ national_currency_short: &'static str,
+}
+
+fn display_perimeter(country: &Country) -> Cow<'_, str> {
+ if country.surface_area_km2 > 1_000_000.0 {
+ "Very Big Land".into()
+ } else {
+ "Big Land".into()
+ }
+}
+
+impl Country {
+ fn new(
+ name: &'static str,
+ national_currency: &'static str,
+ national_currency_short: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ ) -> Self {
+ Self {
+ name,
+ national_currency,
+ national_currency_short,
+ capital_city,
+ surface_area_km2,
+ }
+ }
+}
+
+fn main() {
+ let data = [
+ Country::new("Afghanistan", "Afghani", "AFN", "Kabul", 652867.0),
+ Country::new("Angola", "Kwanza", "AOA", "Luanda", 1246700.0),
+ Country::new("Canada", "Canadian Dollar", "CAD", "Ottawa", 9984670.0),
+ ];
+
+ let table = Table::new(data);
+
+ println!("{table}");
+}
diff --git a/vendor/tabled/examples/derive/inline.rs b/vendor/tabled/examples/derive/inline.rs
new file mode 100644
index 000000000..2ce07816f
--- /dev/null
+++ b/vendor/tabled/examples/derive/inline.rs
@@ -0,0 +1,55 @@
+//! This example demonstrates using the [attribute macro](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
+//! [`inline`] to expand struct fields to individual columns in a [`Table`] display.
+//!
+//! * Note that without inlining a struct or enum field, those objects
+//! must implement the [`Display`] trait as they will be represented in
+//! a single column with the value of their [`ToString`] output.
+
+use tabled::{Table, Tabled};
+
+#[derive(Tabled)]
+struct Country {
+ name: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ #[tabled(inline)]
+ currency: Currency,
+}
+
+#[derive(Tabled)]
+struct Currency {
+ str: &'static str,
+ short: &'static str,
+}
+
+impl Country {
+ fn new(
+ name: &'static str,
+ national_currency: &'static str,
+ national_currency_short: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ ) -> Self {
+ Self {
+ name,
+ capital_city,
+ surface_area_km2,
+ currency: Currency {
+ str: national_currency,
+ short: national_currency_short,
+ },
+ }
+ }
+}
+
+fn main() {
+ let data = [
+ Country::new("Afghanistan", "Afghani", "AFN", "Kabul", 652867.0),
+ Country::new("Angola", "Kwanza", "AOA", "Luanda", 1246700.0),
+ Country::new("Canada", "Canadian Dollar", "CAD", "Ottawa", 9984670.0),
+ ];
+
+ let table = Table::new(data);
+
+ println!("{table}");
+}
diff --git a/vendor/tabled/examples/derive/inline_enum.rs b/vendor/tabled/examples/derive/inline_enum.rs
new file mode 100644
index 000000000..707d5bb2c
--- /dev/null
+++ b/vendor/tabled/examples/derive/inline_enum.rs
@@ -0,0 +1,50 @@
+//! This example demonstrates using the [attribute macro](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
+//! [`inline`] to expand enum fields to individual columns in a [`Table`] display.
+//!
+//! * Note how the optional [`inline`] argument is used to apply prefixes
+//! to decomposed column headers. This is helpful for organizing tables
+//! with repetative fields that would normally result in confusing headers.
+//!
+//! * Note that without inlining a struct or enum field, those objects
+//! must implement the [`Display`] trait as they will be represented in
+//! a single column with the value of their [`ToString`] output.
+
+use tabled::{Table, Tabled};
+
+#[derive(Tabled)]
+enum Contact {
+ #[tabled(inline("telegram::"))]
+ Telegram {
+ username: &'static str,
+ #[tabled(inline("telegram::"))]
+ number: Number,
+ },
+ #[tabled(inline)]
+ Local(#[tabled(inline("local::"))] Number),
+}
+
+#[derive(Tabled)]
+struct Number {
+ number: &'static str,
+ code: usize,
+}
+
+impl Number {
+ fn new(number: &'static str, code: usize) -> Self {
+ Self { number, code }
+ }
+}
+
+fn main() {
+ let data = [
+ Contact::Local(Number::new("654321", 123)),
+ Contact::Telegram {
+ username: "no2Presley",
+ number: Number::new("123456", 123),
+ },
+ ];
+
+ let table = Table::new(data);
+
+ println!("{table}");
+}
diff --git a/vendor/tabled/examples/derive/order.rs b/vendor/tabled/examples/derive/order.rs
new file mode 100644
index 000000000..44e036fad
--- /dev/null
+++ b/vendor/tabled/examples/derive/order.rs
@@ -0,0 +1,48 @@
+//! This example demonstrates using the [attribute macro](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
+//! [`order`] to relocate fields to specified indexes in a [`Table`] display.
+//!
+//! * By default, [`Table`] columns are shown in the same ordered they are
+//! defined in the deriving struct/enum definition.
+
+use tabled::{Table, Tabled};
+
+#[derive(Tabled)]
+struct Country {
+ name: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ #[tabled(order = 1)]
+ national_currency: &'static str,
+ #[tabled(order = 2)]
+ national_currency_short: &'static str,
+}
+
+impl Country {
+ fn new(
+ name: &'static str,
+ national_currency: &'static str,
+ national_currency_short: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ ) -> Self {
+ Self {
+ name,
+ national_currency,
+ national_currency_short,
+ capital_city,
+ surface_area_km2,
+ }
+ }
+}
+
+fn main() {
+ let data = [
+ Country::new("Afghanistan", "Afghani", "AFN", "Kabul", 652867.0),
+ Country::new("Angola", "Kwanza", "AOA", "Luanda", 1246700.0),
+ Country::new("Canada", "Canadian Dollar", "CAD", "Ottawa", 9984670.0),
+ ];
+
+ let table = Table::new(data);
+
+ println!("{table}");
+}
diff --git a/vendor/tabled/examples/derive/rename.rs b/vendor/tabled/examples/derive/rename.rs
new file mode 100644
index 000000000..709411948
--- /dev/null
+++ b/vendor/tabled/examples/derive/rename.rs
@@ -0,0 +1,45 @@
+//! This example demonstrates using the [attribute macro](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
+//! [`rename`] to alias specific fields in a [`Table`] display.
+
+use tabled::{Table, Tabled};
+
+#[derive(Tabled)]
+struct Country {
+ name: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ #[tabled(rename = "Currency")]
+ national_currency: &'static str,
+ #[tabled(rename = "Currency-ISO")]
+ national_currency_short: &'static str,
+}
+
+impl Country {
+ fn new(
+ name: &'static str,
+ national_currency: &'static str,
+ national_currency_short: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ ) -> Self {
+ Self {
+ name,
+ national_currency,
+ national_currency_short,
+ capital_city,
+ surface_area_km2,
+ }
+ }
+}
+
+fn main() {
+ let data = [
+ Country::new("Afghanistan", "Afghani", "AFN", "Kabul", 652867.0),
+ Country::new("Angola", "Kwanza", "AOA", "Luanda", 1246700.0),
+ Country::new("Canada", "Canadian Dollar", "CAD", "Ottawa", 9984670.0),
+ ];
+
+ let table = Table::new(data);
+
+ println!("{table}");
+}
diff --git a/vendor/tabled/examples/derive/rename_all.rs b/vendor/tabled/examples/derive/rename_all.rs
new file mode 100644
index 000000000..cd408e5ba
--- /dev/null
+++ b/vendor/tabled/examples/derive/rename_all.rs
@@ -0,0 +1,56 @@
+//! This example demonstrates using the [attribute macro](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
+//! [`rename_all`] to apply table-wide header formatting in a [`Table`] display.
+//!
+//! * Supported formatting rules include:
+//! * 'camelCase'
+//! * 'kabab-case'
+//! * 'PascalCase'
+//! * 'SCREAMING_SNAKE_CASE'
+//! * 'snake_case'
+//! * 'lowercase'
+//! * 'UPPERCASE'
+//! * 'verbatim'
+
+use tabled::{Table, Tabled};
+
+#[derive(Tabled)]
+#[tabled(rename_all = "camelCase")]
+struct Country {
+ name: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ #[tabled(rename_all = "kebab-case")]
+ national_currency: &'static str,
+ #[tabled(rename_all = "kebab-case")]
+ national_currency_short: &'static str,
+}
+
+impl Country {
+ fn new(
+ name: &'static str,
+ national_currency: &'static str,
+ national_currency_short: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ ) -> Self {
+ Self {
+ name,
+ national_currency,
+ national_currency_short,
+ capital_city,
+ surface_area_km2,
+ }
+ }
+}
+
+fn main() {
+ let data = [
+ Country::new("Afghanistan", "Afghani", "AFN", "Kabul", 652867.0),
+ Country::new("Angola", "Kwanza", "AOA", "Luanda", 1246700.0),
+ Country::new("Canada", "Canadian Dollar", "CAD", "Ottawa", 9984670.0),
+ ];
+
+ let table = Table::new(data);
+
+ println!("{table}");
+}
diff --git a/vendor/tabled/examples/derive/skip.rs b/vendor/tabled/examples/derive/skip.rs
new file mode 100644
index 000000000..86f7a9ba2
--- /dev/null
+++ b/vendor/tabled/examples/derive/skip.rs
@@ -0,0 +1,49 @@
+//! This example demonstrates using the [attribute macro](https://doc.rust-lang.org/reference/procedural-macros.html#attribute-macros)
+//! [`skip`] to omit specific fields from becoming columns in a [`Table`] display.
+//!
+//! * Note how [`skip`] annoys [clippy](https://doc.rust-lang.org/clippy/) with `dead_code`
+//! warnings. This can be addressed with compiler overrides like `#[allow(dead_code)]`.
+
+use tabled::{Table, Tabled};
+
+#[allow(dead_code)]
+#[derive(Tabled)]
+struct Country {
+ name: &'static str,
+ capital_city: &'static str,
+ #[tabled(skip)]
+ surface_area_km2: f32,
+ national_currency: &'static str,
+ #[tabled(skip)]
+ national_currency_short: &'static str,
+}
+
+impl Country {
+ fn new(
+ name: &'static str,
+ national_currency: &'static str,
+ national_currency_short: &'static str,
+ capital_city: &'static str,
+ surface_area_km2: f32,
+ ) -> Self {
+ Self {
+ name,
+ national_currency,
+ national_currency_short,
+ capital_city,
+ surface_area_km2,
+ }
+ }
+}
+
+fn main() {
+ let data = [
+ Country::new("Afghanistan", "Afghani", "AFN", "Kabul", 652867.0),
+ Country::new("Angola", "Kwanza", "AOA", "Luanda", 1246700.0),
+ Country::new("Canada", "Canadian Dollar", "CAD", "Ottawa", 9984670.0),
+ ];
+
+ let table = Table::new(data);
+
+ println!("{table}");
+}