diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-26 06:27:16 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-26 06:41:01 +0800 |
| commit | d1a74ce57e5be72436376a829e9c7e1e7c1c561b (patch) | |
| tree | 6ac923671fc09f1c47b613869d793ed3e04b91db /mingling | |
| parent | e735671acb3a81e1b7e334e56b9ef3963ba0c2fc (diff) | |
refactor(general_renderer): rename to structural_renderer
Diffstat (limited to 'mingling')
| -rw-r--r-- | mingling/Cargo.toml | 20 | ||||
| -rw-r--r-- | mingling/src/example_docs.rs | 202 | ||||
| -rw-r--r-- | mingling/src/features.rs | 66 | ||||
| -rw-r--r-- | mingling/src/lib.rs | 12 | ||||
| -rw-r--r-- | mingling/src/setups.rs | 8 | ||||
| -rw-r--r-- | mingling/src/setups/structural_renderer.rs (renamed from mingling/src/setups/general_renderer.rs) | 26 |
6 files changed, 167 insertions, 167 deletions
diff --git a/mingling/Cargo.toml b/mingling/Cargo.toml index 427bed6..0cb211d 100644 --- a/mingling/Cargo.toml +++ b/mingling/Cargo.toml @@ -15,7 +15,7 @@ serde.workspace = true tokio.workspace = true mingling = { path = ".", features = [ "comp", - "general_renderer", + "structural_renderer", "parser", "repl", ] } @@ -23,7 +23,7 @@ mingling = { path = ".", features = [ [package.metadata.docs.rs] features = [ "builds", - "general_renderer", + "structural_renderer", "repl", "comp", "parser", @@ -45,21 +45,21 @@ repl = ["mingling_core/repl", "mingling_macros/repl"] comp = ["mingling_core/comp", "mingling_macros/comp"] parser = ["dep:size"] -general_renderer = [ - "mingling_core/general_renderer", +structural_renderer = [ + "mingling_core/structural_renderer", "dep:serde", - "mingling_macros/general_renderer", + "mingling_macros/structural_renderer", "json_serde_fmt", ] -general_renderer_empty = [ - "mingling_core/general_renderer", +structural_renderer_empty = [ + "mingling_core/structural_renderer", "dep:serde", - "mingling_macros/general_renderer", + "mingling_macros/structural_renderer", ] -general_renderer_full = [ - "general_renderer", +structural_renderer_full = [ + "structural_renderer", "yaml_serde_fmt", "toml_serde_fmt", "ron_serde_fmt", diff --git a/mingling/src/example_docs.rs b/mingling/src/example_docs.rs index 266a449..c492a11 100644 --- a/mingling/src/example_docs.rs +++ b/mingling/src/example_docs.rs @@ -1170,101 +1170,6 @@ pub mod example_error_handling {} /// gen_program!(); /// ``` pub mod example_exitcode {} -/// Example General Renderer -/// -/// > This example demonstrates how to use the `general_renderer` feature to render data into structures such as json / yaml -/// -/// Run -/// ```bash -/// cargo run --manifest-path examples/example-general-renderer/Cargo.toml --quiet -- render Bob 22 -/// cargo run --manifest-path examples/example-general-renderer/Cargo.toml --quiet -- render Bob 22 --json -/// cargo run --manifest-path examples/example-general-renderer/Cargo.toml --quiet -- render Bob 22 --yaml -/// ``` -/// -/// Output: -/// ```plain -/// Bob is 22 years old -/// {"member_name":"Bob","member_age":22} -/// member_name: Bob -/// member_age: 22 -/// ``` -/// -/// Source code (./Cargo.toml) -/// ```toml -/// [package] -/// name = "example-general-renderer" -/// version = "0.1.0" -/// edition = "2024" -/// -/// [dependencies] -/// serde = { version = "1.0.228", features = ["derive"] } -/// -/// [dependencies.mingling] -/// path = "../../mingling" -/// features = [ -/// "general_renderer", -/// "parser", -/// ] -/// -/// [workspace] -/// ``` -/// -/// Source code (./src/main.rs) -/// ```ignore -/// use mingling::prelude::*; -/// use mingling::{parser::Picker, setup::GeneralRendererSetup, StructuralData, Groupped}; -/// use serde::Serialize; -/// -/// dispatcher!("render", CMDRender => EntryRender); -/// -/// fn main() { -/// let mut program = ThisProgram::new(); -/// // Add `GeneralRendererSetup` to receive user input `--json` `--yaml` parameters -/// program.with_setup(GeneralRendererSetup); -/// program.with_dispatcher(CMDRender); -/// let _ = program.exec(); -/// } -/// -/// // --------- IMPORTANT --------- -/// // For beautiful output structure, do not use `pack!` to wrap the types that need to be output. -/// // Instead, manually implement -/// // __________________________________ Mark as structured data so it can be rendered -/// // / ____________________ Implement serde::Serialize -/// // | / _________ Implement mingling::Groupped -/// // | | / to ensure Mingling can recognize the type -/// // vvvvvvvvvvvv vvvvvvvvv vvvvvvvv -/// #[derive(StructuralData, Serialize, Groupped)] -/// struct Info { -/// #[serde(rename = "member_name")] -/// name: String, -/// #[serde(rename = "member_age")] -/// age: i32, -/// } -/// // This will output: {"member_name":"name","member_age":32} structure -/// -/// // If using pack!(Info = (String, i32)); -/// // Output: {"inner":["name", 32]} -/// -/// // --------- IMPORTANT --------- -/// -/// #[chain] -/// fn parse_render(prev: EntryRender) -> Next { -/// let (name, age) = Picker::new(prev.inner) -/// .pick::<String>(()) -/// .pick::<i32>(()) -/// .unpack(); -/// Info { name, age }.to_render() -/// } -/// -/// /// Implement default renderer for when general_renderer is not specified -/// #[renderer] -/// fn render_info(prev: Info) { -/// r_println!("{} is {} years old", prev.name, prev.age); -/// } -/// -/// gen_program!(); -/// ``` -pub mod example_general_renderer {} /// Example Help /// /// > This example demonstrates how to use the `#[help]` macro to generate help information, @@ -1682,7 +1587,7 @@ pub mod example_outside_type {} /// /// > This example demonstrates how to use the `pack_err!` macro to define error types /// > with automatic `name` field (set to snake_case at compile time) and optional `info` field. -/// > Also demonstrates `--json` serialization when `general_renderer` is enabled. +/// > Also demonstrates `--json` serialization when `structural_renderer` is enabled. /// /// Run: /// ```bash @@ -1719,7 +1624,7 @@ pub mod example_outside_type {} /// [dependencies.mingling] /// path = "../../mingling" /// features = [ -/// "general_renderer", +/// "structural_renderer", /// "extra_macros", /// ] /// @@ -1729,7 +1634,7 @@ pub mod example_outside_type {} /// Source code (./src/main.rs) /// ```ignore /// use mingling::prelude::*; -/// use mingling::setup::GeneralRendererSetup; +/// use mingling::setup::StructuralRendererSetup; /// use std::path::PathBuf; /// /// dispatcher!("find", CMDFind => EntryFind); @@ -1747,7 +1652,7 @@ pub mod example_outside_type {} /// // The typed form additionally generates `pub fn new(info)`. /// // name = "error_not_dir" /// // -/// // When `general_renderer` is enabled, the struct also gets +/// // When `structural_renderer` is enabled, the struct also gets /// // `#[derive(serde::Serialize)]` for --json / --yaml output. /// // --------- IMPORTANT --------- /// @@ -1834,8 +1739,8 @@ pub mod example_outside_type {} /// /// fn main() { /// let mut program = ThisProgram::new(); -/// // Add GeneralRendererSetup to support --json / --yaml flags -/// program.with_setup(GeneralRendererSetup); +/// // Add StructuralRendererSetup to support --json / --yaml flags +/// program.with_setup(StructuralRendererSetup); /// program.with_dispatcher(CMDFind); /// program.with_dispatcher(CMDFindStructural); /// let _ = program.exec(); @@ -2256,6 +2161,101 @@ pub mod example_resources {} /// gen_program!(); /// ``` pub mod example_setup {} +/// Example structural renderer +/// +/// > This example demonstrates how to use the `structural_renderer` feature to render data into structures such as json / yaml +/// +/// Run +/// ```bash +/// cargo run --manifest-path examples/example-structural-renderer/Cargo.toml --quiet -- render Bob 22 +/// cargo run --manifest-path examples/example-structural-renderer/Cargo.toml --quiet -- render Bob 22 --json +/// cargo run --manifest-path examples/example-structural-renderer/Cargo.toml --quiet -- render Bob 22 --yaml +/// ``` +/// +/// Output: +/// ```plain +/// Bob is 22 years old +/// {"member_name":"Bob","member_age":22} +/// member_name: Bob +/// member_age: 22 +/// ``` +/// +/// Source code (./Cargo.toml) +/// ```toml +/// [package] +/// name = "example-structural-renderer" +/// version = "0.1.0" +/// edition = "2024" +/// +/// [dependencies] +/// serde = { version = "1.0.228", features = ["derive"] } +/// +/// [dependencies.mingling] +/// path = "../../mingling" +/// features = [ +/// "structural_renderer", +/// "parser", +/// ] +/// +/// [workspace] +/// ``` +/// +/// Source code (./src/main.rs) +/// ```ignore +/// use mingling::prelude::*; +/// use mingling::{parser::Picker, setup::StructuralRendererSetup, StructuralData, Groupped}; +/// use serde::Serialize; +/// +/// dispatcher!("render", CMDRender => EntryRender); +/// +/// fn main() { +/// let mut program = ThisProgram::new(); +/// // Add `StructuralRendererSetup` to receive user input `--json` `--yaml` parameters +/// program.with_setup(StructuralRendererSetup); +/// program.with_dispatcher(CMDRender); +/// let _ = program.exec(); +/// } +/// +/// // --------- IMPORTANT --------- +/// // For beautiful output structure, do not use `pack!` to wrap the types that need to be output. +/// // Instead, manually implement +/// // __________________________________ Mark as structured data so it can be rendered +/// // / ____________________ Implement serde::Serialize +/// // | / _________ Implement mingling::Groupped +/// // | | / to ensure Mingling can recognize the type +/// // vvvvvvvvvvvv vvvvvvvvv vvvvvvvv +/// #[derive(StructuralData, Serialize, Groupped)] +/// struct Info { +/// #[serde(rename = "member_name")] +/// name: String, +/// #[serde(rename = "member_age")] +/// age: i32, +/// } +/// // This will output: {"member_name":"name","member_age":32} structure +/// +/// // If using pack!(Info = (String, i32)); +/// // Output: {"inner":["name", 32]} +/// +/// // --------- IMPORTANT --------- +/// +/// #[chain] +/// fn parse_render(prev: EntryRender) -> Next { +/// let (name, age) = Picker::new(prev.inner) +/// .pick::<String>(()) +/// .pick::<i32>(()) +/// .unpack(); +/// Info { name, age }.to_render() +/// } +/// +/// /// Implement default renderer for when structural_renderer is not specified +/// #[renderer] +/// fn render_info(prev: Info) { +/// r_println!("{} is {} years old", prev.name, prev.age); +/// } +/// +/// gen_program!(); +/// ``` +pub mod example_structural_renderer {} /// Example Unit Test /// /// > This example shows how to write unit tests for Chain and Renderer in Mingling diff --git a/mingling/src/features.rs b/mingling/src/features.rs index ec9c7ad..8f147fb 100644 --- a/mingling/src/features.rs +++ b/mingling/src/features.rs @@ -97,39 +97,6 @@ pub const MINGLING_EXTRA_MACROS: bool = false; #[cfg(feature = "extra_macros")] #[allow(unused)] pub const MINGLING_EXTRA_MACROS: bool = true; -/// Whether the `general_renderer` feature is enabled -/// Current: `disabled` -#[cfg(not(feature = "general_renderer"))] -#[allow(unused)] -pub const MINGLING_GENERAL_RENDERER: bool = false; - -/// Whether the `general_renderer` feature is enabled -/// Current: `enabled` -#[cfg(feature = "general_renderer")] -#[allow(unused)] -pub const MINGLING_GENERAL_RENDERER: bool = true; -/// Whether the `general_renderer_empty` feature is enabled -/// Current: `disabled` -#[cfg(not(feature = "general_renderer_empty"))] -#[allow(unused)] -pub const MINGLING_GENERAL_RENDERER_EMPTY: bool = false; - -/// Whether the `general_renderer_empty` feature is enabled -/// Current: `enabled` -#[cfg(feature = "general_renderer_empty")] -#[allow(unused)] -pub const MINGLING_GENERAL_RENDERER_EMPTY: bool = true; -/// Whether the `general_renderer_full` feature is enabled -/// Current: `disabled` -#[cfg(not(feature = "general_renderer_full"))] -#[allow(unused)] -pub const MINGLING_GENERAL_RENDERER_FULL: bool = false; - -/// Whether the `general_renderer_full` feature is enabled -/// Current: `enabled` -#[cfg(feature = "general_renderer_full")] -#[allow(unused)] -pub const MINGLING_GENERAL_RENDERER_FULL: bool = true; /// Whether the `json_serde_fmt` feature is enabled /// Current: `disabled` #[cfg(not(feature = "json_serde_fmt"))] @@ -185,6 +152,39 @@ pub const MINGLING_RON_SERDE_FMT: bool = false; #[cfg(feature = "ron_serde_fmt")] #[allow(unused)] pub const MINGLING_RON_SERDE_FMT: bool = true; +/// Whether the `structural_renderer` feature is enabled +/// Current: `disabled` +#[cfg(not(feature = "structural_renderer"))] +#[allow(unused)] +pub const MINGLING_STRUCTURAL_RENDERER: bool = false; + +/// Whether the `structural_renderer` feature is enabled +/// Current: `enabled` +#[cfg(feature = "structural_renderer")] +#[allow(unused)] +pub const MINGLING_STRUCTURAL_RENDERER: bool = true; +/// Whether the `structural_renderer_empty` feature is enabled +/// Current: `disabled` +#[cfg(not(feature = "structural_renderer_empty"))] +#[allow(unused)] +pub const MINGLING_STRUCTURAL_RENDERER_EMPTY: bool = false; + +/// Whether the `structural_renderer_empty` feature is enabled +/// Current: `enabled` +#[cfg(feature = "structural_renderer_empty")] +#[allow(unused)] +pub const MINGLING_STRUCTURAL_RENDERER_EMPTY: bool = true; +/// Whether the `structural_renderer_full` feature is enabled +/// Current: `disabled` +#[cfg(not(feature = "structural_renderer_full"))] +#[allow(unused)] +pub const MINGLING_STRUCTURAL_RENDERER_FULL: bool = false; + +/// Whether the `structural_renderer_full` feature is enabled +/// Current: `enabled` +#[cfg(feature = "structural_renderer_full")] +#[allow(unused)] +pub const MINGLING_STRUCTURAL_RENDERER_FULL: bool = true; /// Whether the `toml_serde_fmt` feature is enabled /// Current: `disabled` #[cfg(not(feature = "toml_serde_fmt"))] diff --git a/mingling/src/lib.rs b/mingling/src/lib.rs index 4c49f15..b021479 100644 --- a/mingling/src/lib.rs +++ b/mingling/src/lib.rs @@ -97,7 +97,7 @@ pub mod macros { #[cfg(feature = "extra_macros")] pub use mingling_macros::group; /// Like `group!` but also marks the type for structured output - #[cfg(all(feature = "general_renderer", feature = "extra_macros"))] + #[cfg(all(feature = "structural_renderer", feature = "extra_macros"))] pub use mingling_macros::group_structural; /// Used to generate a struct implementing the `HelpRequest` trait via a method pub use mingling_macros::help; @@ -106,13 +106,13 @@ pub mod macros { /// Used to create a wrapper type for use with `Chain` and `Renderer` pub use mingling_macros::pack; /// Like `pack!` but also marks the type for structured output - #[cfg(feature = "general_renderer")] + #[cfg(feature = "structural_renderer")] pub use mingling_macros::pack_structural; /// Used to create an error struct with automatic `name` field #[cfg(feature = "extra_macros")] pub use mingling_macros::pack_err; /// Like `pack_err!` but also marks the type for structured output - #[cfg(all(feature = "general_renderer", feature = "extra_macros"))] + #[cfg(all(feature = "structural_renderer", feature = "extra_macros"))] pub use mingling_macros::pack_err_structural; #[cfg(feature = "comp")] #[doc(hidden)] @@ -158,7 +158,7 @@ pub use mingling_macros::EnumTag; pub use mingling_macros::Groupped; /// derive macro `StructuralData` — marks a type as supporting structured output -#[cfg(feature = "general_renderer")] +#[cfg(feature = "structural_renderer")] pub use mingling_macros::StructuralData; /// Example projects for `Mingling`, for learning how to use `Mingling` @@ -214,13 +214,13 @@ pub mod prelude { /// Re-export of the `pack` macro for creating wrapper types. pub use crate::macros::pack; /// Like `pack!` but also marks the type for structured output - #[cfg(feature = "general_renderer")] + #[cfg(feature = "structural_renderer")] pub use mingling_macros::pack_structural; /// Re-export of the `pack_err` macro for creating error types. #[cfg(feature = "extra_macros")] pub use crate::macros::pack_err; /// Like `pack_err!` but also marks the type for structured output - #[cfg(all(feature = "general_renderer", feature = "extra_macros"))] + #[cfg(all(feature = "structural_renderer", feature = "extra_macros"))] pub use mingling_macros::pack_err_structural; /// Re-export of the `r_print` macro for printing within a renderer context. pub use crate::macros::r_print; diff --git a/mingling/src/setups.rs b/mingling/src/setups.rs index b4fad58..5546268 100644 --- a/mingling/src/setups.rs +++ b/mingling/src/setups.rs @@ -4,11 +4,11 @@ pub use basic::*; mod exit_code; pub use exit_code::*; -#[cfg(feature = "general_renderer")] -mod general_renderer; +#[cfg(feature = "structural_renderer")] +mod structural_renderer; -#[cfg(feature = "general_renderer")] -pub use general_renderer::*; +#[cfg(feature = "structural_renderer")] +pub use structural_renderer::*; #[cfg(feature = "repl")] mod repl_basic; diff --git a/mingling/src/setups/general_renderer.rs b/mingling/src/setups/structural_renderer.rs index 88f5bfa..af3ed91 100644 --- a/mingling/src/setups/general_renderer.rs +++ b/mingling/src/setups/structural_renderer.rs @@ -1,22 +1,22 @@ use mingling_core::{Program, ProgramCollect, setup::ProgramSetup}; -/// Sets up the general renderer for the program: +/// Sets up the structural renderer for the program: /// /// - Adds a `--renderer` global argument to specify the renderer type -pub struct GeneralRendererSimpleSetup; +pub struct StructuralRendererSimpleSetup; -impl<C> ProgramSetup<C> for GeneralRendererSimpleSetup +impl<C> ProgramSetup<C> for StructuralRendererSimpleSetup where C: ProgramCollect<Enum = C>, { fn setup(self, program: &mut Program<C>) { program.global_argument("--renderer", |p, renderer| { - p.general_renderer_name = renderer.into(); + p.structural_renderer_name = renderer.into(); }); } } -/// Sets up the general renderer for the program: +/// Sets up the structural renderer for the program: /// /// - Adds global flags to specify the renderer type: /// * `--json` for JSON output @@ -25,9 +25,9 @@ where /// * `--toml` for TOML output /// * `--ron` for RON output /// * `--ron-pretty` for pretty-printed RON output -pub struct GeneralRendererSetup; +pub struct StructuralRendererSetup; -impl<C> ProgramSetup<C> for GeneralRendererSetup +impl<C> ProgramSetup<C> for StructuralRendererSetup where C: ProgramCollect<Enum = C>, { @@ -35,27 +35,27 @@ where fn setup(self, program: &mut Program<C>) { #[cfg(feature = "json_serde_fmt")] program.global_flag("--json", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::Json; + p.structural_renderer_name = crate::StructuralRendererSetting::Json; }); #[cfg(feature = "json_serde_fmt")] program.global_flag("--json-pretty", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::JsonPretty; + p.structural_renderer_name = crate::StructuralRendererSetting::JsonPretty; }); #[cfg(feature = "yaml_serde_fmt")] program.global_flag("--yaml", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::Yaml; + p.structural_renderer_name = crate::StructuralRendererSetting::Yaml; }); #[cfg(feature = "toml_serde_fmt")] program.global_flag("--toml", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::Toml; + p.structural_renderer_name = crate::StructuralRendererSetting::Toml; }); #[cfg(feature = "ron_serde_fmt")] program.global_flag("--ron", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::Ron; + p.structural_renderer_name = crate::StructuralRendererSetting::Ron; }); #[cfg(feature = "ron_serde_fmt")] program.global_flag("--ron-pretty", |p| { - p.general_renderer_name = crate::GeneralRendererSetting::RonPretty; + p.structural_renderer_name = crate::StructuralRendererSetting::RonPretty; }); } } |
