Skip to content

Commit

Permalink
fix: Format to repl.rs structure, remove readme
Browse files Browse the repository at this point in the history
  • Loading branch information
eddique committed Jan 24, 2024
1 parent 1b28cf8 commit 3216a64
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 95 deletions.
41 changes: 0 additions & 41 deletions examples/repl-derive.md

This file was deleted.

91 changes: 40 additions & 51 deletions examples/repl-derive.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@
use std::io::Write;

use clap::{Args, Parser, Subcommand};
use clap::{Parser, Subcommand};

#[derive(Debug, Parser)]
#[command(multicall = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
fn main() -> Result<(), String> {
loop {
let line = readline()?;
let line = line.trim();
if line.is_empty() {
continue;
}

#[derive(Debug, Subcommand)]
enum Commands {
Echo(EchoArgs),
Ping,
Exit,
}
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())?;
}
}
}

#[derive(Args, Debug)]
pub struct EchoArgs {
#[arg(
short = 't',
long = "text",
visible_alias = "text",
help = "The text to be echoed",
help_heading = "Echo"
)]
text: 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())?;
let cli = Cli::try_parse_from(args)
.map_err(|e| e.to_string())?;
match cli.command {
Commands::Echo(args) => {
println!("{}", args.text);
}
Commands::Ping => {
println!("Pong");
write!(std::io::stdout(), "Pong").map_err(|e| e.to_string())?;
std::io::stdout().flush().map_err(|e| e.to_string())?;
}
Commands::Exit => {
println!("Exiting ...");
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())?;
Expand All @@ -55,25 +66,3 @@ fn readline() -> Result<String, String> {
.map_err(|e| e.to_string())?;
Ok(buffer)
}

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(())
}
3 changes: 0 additions & 3 deletions src/_cookbook/repl_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@
//!
//! ```rust
#![doc = include_str!("../../examples/repl-derive.rs")]
//! ```
//!
#![doc = include_str!("../../examples/repl-derive.md")]

0 comments on commit 3216a64

Please sign in to comment.