cubob/instant/mod.rs
1//! When your struct has field which is some kind of list (Vec, slice, array, etc.) or map (HashMap, BTreeMap, etc.) you
2//! can experience additional complications while trying to output them if they have no standard Display implementation.
3//! Since this whole crate is aiming on simplification of Display implementations routine, the need in some related solution
4//! becomes obvious. And here it is - this module contain trait and generics which can simplify displaying of lists and maps.
5//!
6//! Usage example:
7//! ```
8//! use core::fmt::{Display, Formatter, Result as FmtResult};
9//! use std::{vec::Vec, collections::HashMap};
10//! use cubob::{display_struct, Alternate, StructShow, InstantList, InstantStruct};
11//!
12//! struct Object {
13//! title: String,
14//! description: Option<String>,
15//! properties: HashMap<String, String>,
16//! }
17//!
18//! struct Space {
19//! tags: Vec<String>,
20//! members: HashMap<usize, Object>,
21//! }
22//!
23//! impl Display for Object {
24//! fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
25//! StructShow::inherit(f)
26//! .field(&"title", &self.title)
27//! .field_opt(&"description", &self.description)
28//! // self.properties field can be displayed as struct without any self-made helpers
29//! .field(&"properties", &InstantStruct::inherit(&self.properties))
30//! .finish()
31//! }
32//! }
33//!
34//! impl Display for Space {
35//! fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
36//! StructShow::inherit(f)
37//! // self.tags field can be displayed as list without any self-made helpers
38//! .field_override(&"tags", &InstantList::inherit(&self.tags), Alternate::OneLine)
39//! // self.members field can be displayed as struct since Object provides Display implementation
40//! .field(&"members", &InstantStruct::inherit(&self.members))
41//! .finish()
42//! }
43//! }
44//!
45//! let space = Space {
46//! tags: vec!["full".into(), "hilbert".into()],
47//! members: {
48//! let mut map = HashMap::new();
49//! map.insert(1, Object {
50//! title: "Ball".into(),
51//! description: Some("The ball Masha had been playing with".into()),
52//! properties: {
53//! let mut map = HashMap::new();
54//! map.insert("colour".into(), "green".into());
55//! map.insert("owner".into(), "masha".into());
56//! map
57//! }
58//! });
59//! map.insert(2, Object {
60//! title: "Cube".into(),
61//! description: None,
62//! properties: {
63//! let mut map = HashMap::new();
64//! map.insert("colour".into(), "red".into());
65//! map
66//! }
67//! });
68//! map
69//! }
70//! };
71//! println!("One-line: {}", space);
72//! println!("Prettified: {:#}", space);
73//! ```
74
75#[cfg(any(feature = "list", feature = "struct"))]
76#[cfg_attr(docsrs, doc(cfg(any(feature = "list", feature = "struct"))))]
77mod iterable;
78#[cfg(feature = "list")]
79#[cfg_attr(docsrs, doc(cfg(feature = "list")))]
80mod list;
81#[cfg(feature = "struct")]
82#[cfg_attr(docsrs, doc(cfg(feature = "struct")))]
83mod r#struct;
84
85#[cfg(any(feature = "list", feature = "struct"))]
86pub use iterable::*;
87#[cfg(feature = "list")]
88pub use list::*;
89#[cfg(feature = "struct")]
90pub use r#struct::*;