make more APIs public (#55)

This commit is contained in:
Carl Lerche
2020-06-12 15:15:51 -07:00
committed by GitHub
parent f1042d6b7c
commit 03f8281431
7 changed files with 18 additions and 17 deletions

View File

@@ -16,14 +16,14 @@ pub struct Get {
impl Get { impl Get {
/// Create a new `Get` command which fetches `key`. /// Create a new `Get` command which fetches `key`.
pub(crate) fn new(key: impl ToString) -> Get { pub fn new(key: impl ToString) -> Get {
Get { Get {
key: key.to_string(), key: key.to_string(),
} }
} }
/// Get the key /// Get the key
pub(crate) fn key(&self) -> &str { pub fn key(&self) -> &str {
&self.key &self.key
} }

View File

@@ -19,7 +19,7 @@ use crate::{Connection, Db, Frame, Parse, ParseError, Shutdown};
/// ///
/// Methods called on `Command` are delegated to the command implementation. /// Methods called on `Command` are delegated to the command implementation.
#[derive(Debug)] #[derive(Debug)]
pub(crate) enum Command { pub enum Command {
Get(Get), Get(Get),
Publish(Publish), Publish(Publish),
Set(Set), Set(Set),
@@ -37,7 +37,7 @@ impl Command {
/// # Returns /// # Returns
/// ///
/// On success, the command value is returned, otherwise, `Err` is returned. /// On success, the command value is returned, otherwise, `Err` is returned.
pub(crate) fn from_frame(frame: Frame) -> crate::Result<Command> { pub fn from_frame(frame: Frame) -> crate::Result<Command> {
// The frame value is decorated with `Parse`. `Parse` provides a // The frame value is decorated with `Parse`. `Parse` provides a
// "cursor" like API which makes parsing the command easier. // "cursor" like API which makes parsing the command easier.
// //

View File

@@ -34,7 +34,7 @@ impl Set {
/// ///
/// If `expire` is `Some`, the value should expire after the specified /// If `expire` is `Some`, the value should expire after the specified
/// duration. /// duration.
pub(crate) fn new(key: impl ToString, value: Bytes, expire: Option<Duration>) -> Set { pub fn new(key: impl ToString, value: Bytes, expire: Option<Duration>) -> Set {
Set { Set {
key: key.to_string(), key: key.to_string(),
value, value,
@@ -43,13 +43,13 @@ impl Set {
} }
/// Get the key /// Get the key
pub(crate) fn key(&self) -> &str { pub fn key(&self) -> &str {
&self.key &self.key
} }
/// Get the value /// Get the value
pub(crate) fn value(&self) -> Bytes { pub fn value(&self) -> &Bytes {
self.value.clone() &self.value
} }
/// Get the expires /// Get the expires

View File

@@ -18,7 +18,7 @@ use tokio::net::TcpStream;
/// When sending frames, the frame is first encoded into the write buffer. /// When sending frames, the frame is first encoded into the write buffer.
/// The contents of the write buffer are then written to the socket. /// The contents of the write buffer are then written to the socket.
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Connection { pub struct Connection {
// The `TcpStream`. It is decorated with a `BufWriter`, which provides write // The `TcpStream`. It is decorated with a `BufWriter`, which provides write
// level buffering. The `BufWriter` implementation provided by Tokio is // level buffering. The `BufWriter` implementation provided by Tokio is
// sufficient for our needs. // sufficient for our needs.
@@ -34,7 +34,7 @@ pub(crate) struct Connection {
impl Connection { impl Connection {
/// Create a new `Connection`, backed by `socket`. Read and write buffers /// Create a new `Connection`, backed by `socket`. Read and write buffers
/// are initialized. /// are initialized.
pub(crate) fn new(socket: TcpStream) -> Connection { pub fn new(socket: TcpStream) -> Connection {
Connection { Connection {
stream: BufWriter::new(socket), stream: BufWriter::new(socket),
// Default to a 4KB read buffer. For the use case of mini redis, // Default to a 4KB read buffer. For the use case of mini redis,
@@ -56,7 +56,7 @@ impl Connection {
/// On success, the received frame is returned. If the `TcpStream` /// On success, the received frame is returned. If the `TcpStream`
/// is closed in a way that doesn't break a frame in half, it returns /// is closed in a way that doesn't break a frame in half, it returns
/// `None`. Otherwise, an error is returned. /// `None`. Otherwise, an error is returned.
pub(crate) async fn read_frame(&mut self) -> crate::Result<Option<Frame>> { pub async fn read_frame(&mut self) -> crate::Result<Option<Frame>> {
use frame::Error::Incomplete; use frame::Error::Incomplete;
loop { loop {
@@ -146,7 +146,7 @@ impl Connection {
/// syscalls. However, it is fine to call these functions on a *buffered* /// syscalls. However, it is fine to call these functions on a *buffered*
/// write stream. The data will be written to the buffer. Once the buffer is /// write stream. The data will be written to the buffer. Once the buffer is
/// full, it is flushed to the underlying socket. /// full, it is flushed to the underlying socket.
pub(crate) async fn write_frame(&mut self, frame: &Frame) -> io::Result<()> { pub async fn write_frame(&mut self, frame: &Frame) -> io::Result<()> {
// Arrays are encoded by encoding each entry. All other frame types are // Arrays are encoded by encoding each entry. All other frame types are
// considered literals. For now, mini-redis is not able to encode // considered literals. For now, mini-redis is not able to encode
// recursive frame structures. See below for more details. // recursive frame structures. See below for more details.

View File

@@ -5,8 +5,9 @@ use std::io::Cursor;
use std::num::TryFromIntError; use std::num::TryFromIntError;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
/// A frame in the Redis protocol.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(crate) enum Frame { pub enum Frame {
Simple(String), Simple(String),
Error(String), Error(String),
Integer(u64), Integer(u64),

View File

@@ -28,13 +28,13 @@
pub mod client; pub mod client;
pub mod cmd; pub mod cmd;
use cmd::Command; pub use cmd::Command;
mod connection; mod connection;
use connection::Connection; pub use connection::Connection;
mod frame; mod frame;
use frame::Frame; pub use frame::Frame;
mod db; mod db;
use db::Db; use db::Db;

View File

@@ -33,7 +33,7 @@ async fn run(
} }
Command::Set(set) => { Command::Set(set) => {
let key = set.key(); let key = set.key();
let value = set.value(); let value = set.value().clone();
let expires = set.expire(); let expires = set.expire();
let result = match expires { let result = match expires {
None => client.set(&key, value).await, None => client.set(&key, value).await,