-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5328 from eddique/master
docs(example): Added repl derive example
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use std::io::Write; | ||
|
||
use clap::{Parser, Subcommand}; | ||
|
||
fn main() -> Result<(), String> { | ||
loop { | ||
let line = readline()?; | ||
let line = line.trim(); | ||
if line.is_empty() { | ||
continue; | ||
} | ||
|
||
match respond(line) { | ||
Ok(quit) => { | ||
if quit { | ||
break; | ||
} | ||
} | ||
Err(err) => { | ||
write!(std::io::stdout(), "{err}").map_err(|e| e.to_string())?; | ||
std::io::stdout().flush().map_err(|e| e.to_string())?; | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn respond(line: &str) -> Result<bool, String> { | ||
let args = shlex::split(line).ok_or("error: Invalid quoting")?; | ||
let cli = Cli::try_parse_from(args).map_err(|e| e.to_string())?; | ||
match cli.command { | ||
Commands::Ping => { | ||
write!(std::io::stdout(), "Pong").map_err(|e| e.to_string())?; | ||
std::io::stdout().flush().map_err(|e| e.to_string())?; | ||
} | ||
Commands::Exit => { | ||
write!(std::io::stdout(), "Exiting ...").map_err(|e| e.to_string())?; | ||
std::io::stdout().flush().map_err(|e| e.to_string())?; | ||
return Ok(true); | ||
} | ||
} | ||
Ok(false) | ||
} | ||
|
||
#[derive(Debug, Parser)] | ||
#[command(multicall = true)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum Commands { | ||
Ping, | ||
Exit, | ||
} | ||
|
||
fn readline() -> Result<String, String> { | ||
write!(std::io::stdout(), "$ ").map_err(|e| e.to_string())?; | ||
std::io::stdout().flush().map_err(|e| e.to_string())?; | ||
let mut buffer = String::new(); | ||
std::io::stdin() | ||
.read_line(&mut buffer) | ||
.map_err(|e| e.to_string())?; | ||
Ok(buffer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//! # Example: REPL (Derive API) | ||
//! | ||
//! ```rust | ||
#![doc = include_str!("../../examples/repl-derive.rs")] |