Skip to content

Commit 80511f2

Browse files
add cli for server/client (#4)
This PR adds a CLI to main.rs that allows you to run the redis server and eventually the redis client.
1 parent 9852de9 commit 80511f2

File tree

12 files changed

+232
-24
lines changed

12 files changed

+232
-24
lines changed

Cargo.lock

Lines changed: 151 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
[package]
2-
name = "mini-redis"
3-
version = "0.1.0"
42
authors = ["Carl Lerche <[email protected]>"]
53
edition = "2018"
6-
7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
4+
name = "mini-redis"
5+
version = "0.1.0"
86

97
[dependencies]
8+
atoi = "0.3.2"
109
bytes = "0.5"
10+
clap = { git = "https://github.com/clap-rs/clap/" }
1111
tokio = { git = "https://github.com/tokio-rs/tokio", features = ["full"] }
12-
13-
atoi = "0.3.2"

examples/hello_world.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
1+
#[tokio::main]
2+
async fn main() {
3+
unimplemented!();
4+
}

examples/pub.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
1+
#[tokio::main]
2+
async fn main() {
3+
unimplemented!();
4+
}

src/bin/client.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use bytes::Bytes;
2+
use clap::Clap;
3+
use mini_redis::{client, DEFAULT_PORT};
4+
use std::{io, str};
5+
6+
#[tokio::main]
7+
async fn main() -> io::Result<()> {
8+
let cli = Cli::parse();
9+
let port = cli.port.unwrap_or(DEFAULT_PORT.to_string());
10+
let mut client = client::connect(&format!("127.0.0.1:{}", port)).await?;
11+
match cli.command {
12+
Client::Get { key } => {
13+
let result = client.get(&key).await?;
14+
if let Some(result) = result {
15+
println!("\"{}\"", str::from_utf8(&result).unwrap());
16+
} else {
17+
println!("(nil)");
18+
}
19+
Ok(())
20+
}
21+
Client::Set { key, value } => client.set(&key, Bytes::from(value)).await,
22+
}
23+
}
24+
25+
#[derive(Clap, Debug)]
26+
#[clap(name = "mini-redis-client", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "Opens a connection to a Redis server")]
27+
struct Cli {
28+
#[clap(subcommand)]
29+
command: Client,
30+
#[clap(name = "port", long = "--port")]
31+
port: Option<String>,
32+
}
33+
34+
#[derive(Clap, Debug)]
35+
enum Client {
36+
#[clap(about = "Gets a value associated with a key")]
37+
Get { key: String },
38+
#[clap(about = "Associates a value with a key")]
39+
Set { key: String, value: String },
40+
}

src/bin/server.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use clap::Clap;
2+
use mini_redis::{server, DEFAULT_PORT};
3+
use std::io;
4+
5+
#[tokio::main]
6+
pub async fn main() -> io::Result<()> {
7+
let cli = Cli::parse();
8+
let port = cli.port.unwrap_or(DEFAULT_PORT.to_string());
9+
server::run(&port).await
10+
}
11+
12+
#[derive(Clap, Debug)]
13+
#[clap(name = "mini-redis-server", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "A Redis server")]
14+
struct Cli {
15+
#[clap(name = "port", long = "--port")]
16+
port: Option<String>,
17+
}

src/client.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![cfg(nope)]
21
use crate::Connection;
32

43
use bytes::Bytes;

src/cmd/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl Get {
1414
}
1515

1616
pub(crate) async fn apply(self, kv: &Kv, dst: &mut Connection) -> io::Result<()> {
17-
let response = if let Some(value) = kv.get(&self.key[..]) {
17+
let response = if let Some(value) = kv.get(&self.key) {
1818
Frame::Bulk(value)
1919
} else {
2020
Frame::Null

src/cmd/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ impl Command {
2727
pub(crate) fn from_frame(frame: Frame) -> Result<Command, ParseError> {
2828
let mut parse = Parse::new(frame)?;
2929

30-
let command_name = parse.next_string()?;
30+
let command_name = parse.next_string()?.to_lowercase();
3131

3232
let command = match &command_name[..] {
33-
"GET" => Command::Get(Get::parse(&mut parse)?),
34-
"PUBLISH" => Command::Publish(Publish::parse(&mut parse)?),
35-
"SET" => Command::Set(Set::parse(&mut parse)?),
36-
"SUBSCRIBE" => Command::Subscribe(Subscribe::parse(&mut parse)?),
37-
"UNSUBSCRIBE" => Command::Unsubscribe(Unsubscribe::parse(&mut parse)?),
33+
"get" => Command::Get(Get::parse(&mut parse)?),
34+
"publish" => Command::Publish(Publish::parse(&mut parse)?),
35+
"set" => Command::Set(Set::parse(&mut parse)?),
36+
"subscribe" => Command::Subscribe(Subscribe::parse(&mut parse)?),
37+
"unsubscribe" => Command::Unsubscribe(Unsubscribe::parse(&mut parse)?),
3838
_ => return Err(ParseError::UnknownCommand(command_name)),
3939
};
4040

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
pub const DEFAULT_PORT: &str = "6379";
2+
13
pub mod client;
24

35
mod cmd;

0 commit comments

Comments
 (0)