Skip to content

Commit b11172d

Browse files
committed
Add misc methods and bump version
1 parent bb735db commit b11172d

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All changes in this project will be noted in this file.
44

5+
### 0.8.3
6+
7+
Added the following implementations:
8+
- `FromResponse` for `Row`
9+
- `FromValue` for `Value` (this was erroneously missing)
10+
- Added the `Value::parse` and `Value::parse_cloned` member methods
11+
- Added `Row::into_first` and `Row::into_first_as` member methods
12+
513
### 0.8.2
614

715
Support deriving queries and responses.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ license = "Apache-2.0"
99
name = "skytable"
1010
readme = "README.md"
1111
repository = "https://github.com/skytable/client-rust"
12-
version = "0.8.2"
12+
version = "0.8.3"
1313

1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515
[dependencies]
1616
# internal deps
1717
sky-derive = "0.2.0"
1818
# external deps
19-
tokio = { version = "1.34.0", features = ["full"] }
19+
tokio = { version = "1.35.1", features = ["full"] }
2020
native-tls = "0.2.11"
2121
tokio-native-tls = "0.3.1"
2222
rand = "0.8.5"
2323
r2d2 = "0.8.10"
24-
async-trait = "0.1.74"
24+
async-trait = "0.1.77"
2525
bb8 = "0.8.1"
26-
itoa = "1.0.9"
26+
itoa = "1.0.10"

src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
2525
use {crate::protocol::ProtocolError, core::fmt};
2626

27+
/// A [`Result`] type alias for the client driver
2728
pub type ClientResult<T> = Result<T, Error>;
2829

2930
#[derive(Debug)]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ pub mod pool;
118118
pub mod query;
119119
pub mod response;
120120
pub mod syncio;
121-
// re-exports
122121
/// The `Query` derive macro enables you to directly pass complex types as parameters into queries
123122
pub use sky_derive::Query;
124123
/// The `Response` derive macro enables you to directly pass complex types as parameters into queries
125124
pub use sky_derive::Response;
125+
// re-exports
126126
pub use {
127127
aio::{ConnectionAsync, ConnectionTlsAsync},
128128
config::Config,

src/pool.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@
4545
//! as a string.
4646
//!
4747
48-
use {
49-
crate::{error::Error, Config, Connection, ConnectionAsync, ConnectionTls, ConnectionTlsAsync},
50-
r2d2::ManageConnection,
51-
};
48+
use crate::{error::Error, Config, Connection, ConnectionAsync, ConnectionTls, ConnectionTlsAsync};
5249

5350
const QUERY_SYSCTL_STATUS: &str = "sysctl report status";
5451

@@ -97,7 +94,7 @@ impl ConnectionMgrTcp {
9794
}
9895
}
9996

100-
impl ManageConnection for ConnectionMgrTcp {
97+
impl r2d2::ManageConnection for ConnectionMgrTcp {
10198
type Connection = Connection;
10299
type Error = Error;
103100
fn connect(&self) -> Result<Self::Connection, Self::Error> {
@@ -143,7 +140,7 @@ impl ConnectionMgrTls {
143140
}
144141
}
145142

146-
impl ManageConnection for ConnectionMgrTls {
143+
impl r2d2::ManageConnection for ConnectionMgrTls {
147144
type Connection = ConnectionTls;
148145
type Error = Error;
149146
fn connect(&self) -> Result<Self::Connection, Self::Error> {

src/response.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ pub enum Value {
6969
List(Vec<Self>),
7070
}
7171

72+
impl FromValue for Value {
73+
fn from_value(v: Value) -> ClientResult<Self> {
74+
Ok(v)
75+
}
76+
}
77+
78+
impl Value {
79+
/// Attempt to parse this value into a different type
80+
pub fn parse<T: FromValue>(self) -> ClientResult<T> {
81+
T::from_value(self)
82+
}
83+
/// Attempt to parse this value into a different type, by cloning the value first
84+
pub fn parse_cloned<T: FromValue>(&self) -> ClientResult<T> {
85+
T::from_value(self.clone())
86+
}
87+
}
88+
7289
#[derive(Debug, PartialEq, Clone)]
7390
/// A row returned by the server
7491
pub struct Row {
@@ -87,6 +104,18 @@ impl Row {
87104
pub fn into_values(self) -> Vec<Value> {
88105
self.values
89106
}
107+
/// Returns the first [`Value`] in the [`Row`] if present
108+
pub fn into_first(mut self) -> ClientResult<Value> {
109+
if self.values.is_empty() {
110+
Err(Error::ParseError(ParseError::ResponseMismatch))
111+
} else {
112+
Ok(self.values.remove(0))
113+
}
114+
}
115+
/// Returns the first [`Value`] in the [`Row`] if present, as the given type
116+
pub fn into_first_as<T: FromValue>(self) -> ClientResult<T> {
117+
self.into_first().and_then(FromValue::from_value)
118+
}
90119
}
91120

92121
#[derive(Debug, PartialEq, Clone)]
@@ -141,6 +170,7 @@ pub enum Response {
141170
/// assert_eq!(myuser.username, "bob");
142171
/// ```
143172
pub trait FromResponse: Sized {
173+
/// Decode the target type from the [`Response`]
144174
fn from_response(resp: Response) -> ClientResult<Self>;
145175
}
146176

@@ -268,3 +298,12 @@ from_response_row!(
268298
(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y) as 25,
269299
(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z) as 26,
270300
);
301+
302+
impl FromResponse for Row {
303+
fn from_response(resp: Response) -> ClientResult<Self> {
304+
match resp {
305+
Response::Row(r) => Ok(r),
306+
_ => Err(Error::ParseError(ParseError::ResponseMismatch)),
307+
}
308+
}
309+
}

0 commit comments

Comments
 (0)