aboutsummaryrefslogtreecommitdiff
path: root/examples/example-enum-tag
diff options
context:
space:
mode:
authorWeicao-CatilGrass <1992414357@qq.com>2026-05-23 23:41:04 +0800
committerWeicao-CatilGrass <1992414357@qq.com>2026-05-23 23:49:34 +0800
commit0a2ef958c0dca21d19e4ffc38ba5a7c4078e182a (patch)
treec82fc4242ed393b132ba514eb434d722e7d9c387 /examples/example-enum-tag
parentccab1940c019dfbfb7dfcbbe4cb927258933755f (diff)
Rework examples and add entry macro for testing
Diffstat (limited to 'examples/example-enum-tag')
-rw-r--r--examples/example-enum-tag/Cargo.lock93
-rw-r--r--examples/example-enum-tag/Cargo.toml12
-rw-r--r--examples/example-enum-tag/src/main.rs102
3 files changed, 207 insertions, 0 deletions
diff --git a/examples/example-enum-tag/Cargo.lock b/examples/example-enum-tag/Cargo.lock
new file mode 100644
index 0000000..9839796
--- /dev/null
+++ b/examples/example-enum-tag/Cargo.lock
@@ -0,0 +1,93 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "example-enum-tag"
+version = "0.1.0"
+dependencies = [
+ "mingling",
+]
+
+[[package]]
+name = "just_fmt"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5454cda0d57db59778608d7a47bff5b16c6705598265869fb052b657f66cf05e"
+
+[[package]]
+name = "just_template"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db3edb658c34b10b69c4b3b58f7ba989cd09c82c0621dee1eef51843c2327225"
+dependencies = [
+ "just_fmt",
+]
+
+[[package]]
+name = "mingling"
+version = "0.1.9"
+dependencies = [
+ "mingling_core",
+ "mingling_macros",
+ "size",
+]
+
+[[package]]
+name = "mingling_core"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+ "just_template",
+]
+
+[[package]]
+name = "mingling_macros"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.106"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "size"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b6709c7b6754dca1311b3c73e79fcce40dd414c782c66d88e8823030093b02b"
+
+[[package]]
+name = "syn"
+version = "2.0.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
diff --git a/examples/example-enum-tag/Cargo.toml b/examples/example-enum-tag/Cargo.toml
new file mode 100644
index 0000000..7a8e5d6
--- /dev/null
+++ b/examples/example-enum-tag/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "example-enum-tag"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies.mingling]
+path = "../../mingling"
+
+features = [
+ "comp",
+ "parser"
+]
diff --git a/examples/example-enum-tag/src/main.rs b/examples/example-enum-tag/src/main.rs
new file mode 100644
index 0000000..05419f7
--- /dev/null
+++ b/examples/example-enum-tag/src/main.rs
@@ -0,0 +1,102 @@
+//! Example Enum Tag
+//!
+//! > This example demonstrates how to use the `EnumTag` derive macro to tag enum variants with metadata,
+//! > which can be used for autocompletion and parsing
+//!
+//! Run:
+//! ```bash
+//! cargo run --manifest-path examples/example-enum-tag/Cargo.toml --quiet -- lang-select OCaml
+//! cargo run --manifest-path examples/example-enum-tag/Cargo.toml --quiet -- lang-select
+//! ```
+//!
+//! Output:
+//! ```plaintext
+//! Selected: OCaml (A representative functional programming language with strong type inference)
+//! Selected: Rust (A systems programming language focused on performance, safety, and concurrency)
+//! ```
+
+use mingling::{
+ EnumTag, Groupped, ShellContext, Suggest, macros::suggest_enum, parser::PickableEnum,
+ prelude::*,
+};
+
+// Define the enum and derive the EnumTag trait
+// ________ adds metadata to the enum, enabling it to:
+// / 1. Be used by the `suggest_enum!(Enum)` macro under the `comp` feature for autocompletion
+// vvvvvvv 2. Implement the `PickableEnum` trait
+#[derive(Debug, Default, EnumTag, Groupped)]
+pub enum ProgrammingLanguages {
+ #[enum_desc("An efficient and flexible compiled language widely used for system programming")]
+ C,
+
+ #[enum_rename("C++")]
+ #[enum_desc("A high-performance language extending C with object-oriented features")]
+ CPlusPlus,
+
+ #[enum_rename("C#")]
+ #[enum_desc("Microsoft's object-oriented programming language running on the .NET platform")]
+ Csharp,
+
+ #[enum_desc(
+ "A cross-platform object-oriented language widely used for enterprise application development"
+ )]
+ Java,
+
+ #[enum_desc(
+ "A dynamic scripting language for web development, supporting prototype chain inheritance"
+ )]
+ JavaScript,
+
+ #[enum_desc("A modern statically typed language running on the JVM, concise and safe")]
+ Kotlin,
+
+ #[enum_desc("A representative functional programming language with strong type inference")]
+ OCaml,
+
+ #[enum_desc("A general-purpose programming language with clean syntax, known for readability")]
+ Python,
+
+ #[enum_desc("An object-oriented scripting language, famous for its concise and elegant syntax")]
+ Ruby,
+
+ #[default]
+ #[enum_desc("A systems programming language focused on performance, safety, and concurrency")]
+ Rust,
+}
+
+// --------- IMPORTANT ---------
+// Implement the PickableEnum trait for ProgrammingLanguages,
+// so that `Picker` can parse this enum
+impl PickableEnum for ProgrammingLanguages {}
+// --------- IMPORTANT ---------
+
+dispatcher!("lang-select", CMDLanguageSelection => EntryLanguageSelection);
+
+#[chain]
+fn handle_language_selection(args: EntryLanguageSelection) -> Next {
+ // You can use Picker to directly parse ProgrammingLanguages
+ let lang: ProgrammingLanguages = args.pick(()).unpack();
+ lang
+}
+
+#[renderer]
+fn render_programming_language(lang: ProgrammingLanguages) {
+ // You can use `enum_info()` to get the name and description of the current enum
+ let (name, desc) = lang.enum_info();
+ r_println!("Selected: {} ({})", name, desc)
+}
+
+#[completion(EntryLanguageSelection)]
+fn complete_language_selection(_: &ShellContext) -> Suggest {
+ // Use `suggest_enum!` directly to generate enum suggestions
+ suggest_enum!(ProgrammingLanguages)
+}
+
+gen_program!();
+
+fn main() {
+ let mut program = ThisProgram::new();
+ program.with_dispatcher(CompletionDispatcher);
+ program.with_dispatcher(CMDLanguageSelection);
+ program.exec_and_exit();
+}