-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
api_user.rs
154 lines (132 loc) · 3.79 KB
/
api_user.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]
use std::io;
use log::info;
use wampire::{
client::{Client, Connection},
ArgList, Value, URI,
};
enum Command {
Add,
Echo,
Help,
Quit,
NoOp,
Invalid(String),
}
fn print_prompt() {
println!("Enter a command (or type \"help\")");
}
fn get_input_from_user() -> String {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
input
}
fn process_input(input: &str) -> (Command, Vec<String>) {
let mut input_iter = input.splitn(2, ' ');
let command = match input_iter.next() {
Some(command) => command.trim().to_lowercase(),
None => return (Command::NoOp, Vec::new()),
};
let command = match command.as_str() {
"add" => Command::Add,
"echo" => Command::Echo,
"help" => Command::Help,
"quit" => Command::Quit,
"" => Command::NoOp,
x => Command::Invalid(x.to_string()),
};
let args = match input_iter.next() {
Some(args_string) => args_string
.split(',')
.map(|arg| arg.trim().to_string())
.collect(),
None => Vec::new(),
};
(command, args)
}
#[allow(clippy::comparison_chain)]
async fn add(client: &mut Client, args: &[String]) {
if args.len() > 2 {
println!("Too many arguments to add. Ignoring");
} else if args.len() < 2 {
println!("Please pass two numbers for adding");
return;
}
let a = match str::parse::<i64>(&args[0]) {
Ok(value) => value,
Err(_) => {
println!("Please enter an integer (got {})", args[0]);
return;
}
};
let b = match str::parse::<i64>(&args[1]) {
Ok(value) => value,
Err(_) => {
println!("Please enter an integer (got {})", args[0]);
return;
}
};
match client
.call(
URI::new("ca.test.add"),
Some(vec![Value::Integer(a), Value::Integer(b)]),
None,
)
.await
{
Ok((args, _)) => {
println!("Result: {}", args.get_int(0).unwrap().unwrap());
}
Err(err) => {
println!("Error: {:?}", err);
}
}
}
async fn echo(client: &mut Client, args: Vec<String>) {
let args = args.into_iter().map(Value::String).collect();
let result = client
.call(URI::new("ca.test.echo"), Some(args), None)
.await;
println!("Result: {:?}", result);
}
fn help() {
println!("This client expects the 'endpoint' and 'router' examples to also be running",);
println!("The following commands are supported:");
println!(" add <a>, <b>");
println!(" Adds the two numbers given by <a> and <b>",);
println!(" echo <args>*");
println!(" Echoes any arguments passed back");
println!(" quit");
println!(" Sends a goodbye message and quits the program");
}
async fn event_loop(mut client: Client) {
loop {
print_prompt();
let input = get_input_from_user();
let (command, args) = process_input(&input);
match command {
Command::Add => {
add(&mut client, &args).await;
}
Command::Echo => {
echo(&mut client, args).await;
}
Command::Help => help(),
Command::Quit => break,
Command::NoOp => {}
Command::Invalid(bad_command) => print!("Invalid command: {}", bad_command),
}
}
client.shutdown().await.unwrap();
}
#[tokio::main]
async fn main() {
env_logger::init();
let connection = Connection::new("ws://127.0.0.1:8080/ws", "demo");
info!("Connecting");
let client = connection.connect().unwrap();
info!("Connected");
event_loop(client).await;
}