Skip to content
This repository was archived by the owner on Oct 18, 2021. It is now read-only.

Commit f55721b

Browse files
authored
Goodbye rustc_serialize, Hello serde_json (#192)
1 parent 86b8eb6 commit f55721b

22 files changed

+189
-197
lines changed

Cargo.toml

+23-16
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
11
[package]
2-
name = "mongodb"
3-
version = "0.2.2"
4-
authors = ["Kevin Yeh <[email protected]>",
5-
"Saghm Rossi <[email protected]>",
6-
"Valeri Karpov <[email protected]>"]
7-
2+
authors = ["Kevin Yeh <[email protected]>", "Saghm Rossi <[email protected]>", "Valeri Karpov <[email protected]>"]
83
description = "A native MongoDB driver developed under MongoDB labs."
9-
repository = "https://github.com/mongodb-labs/mongo-rust-driver-prototype"
104
documentation = "https://docs.rs/mongodb"
11-
readme = "README.md"
125
keywords = ["mongo", "mongodb", "database", "bson", "nosql"]
136
license = "Apache-2.0"
14-
15-
[features]
16-
default = []
17-
ssl = ["openssl"]
7+
name = "mongodb"
8+
readme = "README.md"
9+
repository = "https://github.com/mongodb-labs/mongo-rust-driver-prototype"
10+
version = "0.2.2"
1811

1912
[dependencies]
2013
bitflags = "0.7.0"
21-
bson = "0.4.1"
14+
bson = "0.4.3"
2215
bufstream = "0.1.1"
2316
byteorder = "0.5.3"
2417
chrono = "0.2.25"
25-
openssl = { version = "0.9.3", optional = true }
18+
data-encoding = "1.2.0"
2619
rand = "0.3.14"
2720
rust-crypto = "0.2.31"
28-
rustc-serialize = "0.3.19"
2921
scan_fmt = "0.1.0"
3022
semver = "0.5.0"
3123
separator = "0.3.1"
32-
textnonce = { version = "0.4.1", default-features = false }
3324
time = "0.1.35"
3425

26+
[dependencies.openssl]
27+
optional = true
28+
version = "0.9.3"
29+
30+
[dependencies.textnonce]
31+
default-features = false
32+
version = "0.4.1"
33+
3534
[dev-dependencies]
3635
nalgebra = "0.10.1"
36+
37+
[dev-dependencies.serde_json]
38+
features = ["preserve_order"]
39+
version = "0.9.8"
40+
41+
[features]
42+
default = []
43+
ssl = ["openssl"]

src/auth.rs

+11-27
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@ use crypto::mac::Mac;
99
use crypto::md5::Md5;
1010
use crypto::pbkdf2;
1111
use crypto::sha1::Sha1;
12+
use data_encoding::base64;
1213
use db::{Database, ThreadedDatabase};
1314
use error::Error::{DefaultError, MaliciousServerError, ResponseError};
1415
use error::MaliciousServerErrorType;
1516
use error::Result;
16-
use rustc_serialize::base64::{self, FromBase64, ToBase64};
1717
use textnonce::TextNonce;
1818

19-
const B64_CONFIG: base64::Config = base64::Config {
20-
char_set: base64::CharacterSet::Standard,
21-
newline: base64::Newline::LF,
22-
pad: true,
23-
line_length: None,
24-
};
25-
2619
/// Handles SCRAM-SHA-1 authentication logic.
2720
pub struct Authenticator {
2821
db: Database,
@@ -108,31 +101,22 @@ impl Authenticator {
108101
String,
109102
u32);
110103

111-
let rnonce_b64 = match rnonce_opt {
112-
Some(val) => val,
113-
None => return Err(ResponseError(String::from("Invalid rnonce returned"))),
114-
};
104+
let rnonce_b64 = rnonce_opt
105+
.ok_or_else(|| ResponseError(String::from("Invalid rnonce returned")))?;
115106

116107
// Validate rnonce to make sure server isn't malicious
117108
if !rnonce_b64.starts_with(&initial_data.nonce[..]) {
118109
return Err(MaliciousServerError(MaliciousServerErrorType::InvalidRnonce));
119110
}
120111

121-
let salt_b64 = match salt_opt {
122-
Some(val) => val,
123-
None => return Err(ResponseError(String::from("Invalid salt returned"))),
124-
};
125-
126-
let salt = match salt_b64.from_base64() {
127-
Ok(val) => val,
128-
Err(_) => return Err(ResponseError(String::from("Invalid base64 salt returned"))),
129-
};
112+
let salt_b64 = salt_opt
113+
.ok_or_else(|| ResponseError(String::from("Invalid salt returned")))?;
130114

115+
let salt = base64::decode(salt_b64.as_bytes())
116+
.or_else(|e| Err(ResponseError(format!("Invalid base64 salt returned: {}", e))))?;
131117

132-
let i = match i_opt {
133-
Some(val) => val,
134-
None => return Err(ResponseError(String::from("Invalid iteration count returned"))),
135-
};
118+
let i = i_opt
119+
.ok_or_else(|| ResponseError(String::from("Invalid iteration count returned")))?;
136120

137121
// Hash password
138122
let mut md5 = Md5::new();
@@ -181,7 +165,7 @@ impl Authenticator {
181165
}
182166

183167
// Encode proof and produce the message to send to the server
184-
let b64_proof = proof.to_base64(B64_CONFIG);
168+
let b64_proof = base64::encode(&proof);
185169
let final_message = format!("{},p={}", without_proof, b64_proof);
186170
let binary = Binary(Generic, final_message.into_bytes());
187171

@@ -238,7 +222,7 @@ impl Authenticator {
238222
};
239223

240224
// Check that the signature is valid
241-
if verifier.ne(&server_signature.to_base64(B64_CONFIG)[..]) {
225+
if verifier.ne(&base64::encode(&server_signature)[..]) {
242226
return Err(
243227
MaliciousServerError(MaliciousServerErrorType::InvalidServerSignature));
244228
}

src/error.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! MongoDB Errors and Error Codes.
22
use bson::{self, oid};
33
use coll::error::{WriteException, BulkWriteException};
4-
use rustc_serialize::hex;
4+
use data_encoding;
55
use std::{error, fmt, io, result, sync};
66

77
/// A type for results generated by MongoDB related functions, where the Err type is
@@ -43,7 +43,7 @@ pub enum Error {
4343
/// An ObjectId could not be generated.
4444
OIDError(oid::Error),
4545
/// A hexadecimal string could not be converted to bytes.
46-
FromHexError(hex::FromHexError),
46+
FromHexError(data_encoding::decode::Error),
4747
/// A single-write operation failed.
4848
WriteError(WriteException),
4949
/// A bulk-write operation failed due to one or more lower-level write-related errors.
@@ -121,8 +121,8 @@ impl From<oid::Error> for Error {
121121
}
122122
}
123123

124-
impl From<hex::FromHexError> for Error {
125-
fn from(err: hex::FromHexError) -> Error {
124+
impl From<data_encoding::decode::Error> for Error {
125+
fn from(err: data_encoding::decode::Error) -> Error {
126126
Error::FromHexError(err)
127127
}
128128
}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ extern crate bufstream;
9696
extern crate byteorder;
9797
extern crate chrono;
9898
extern crate crypto;
99+
extern crate data_encoding;
99100
#[cfg(feature = "ssl")]
100101
extern crate openssl;
101102
extern crate rand;
102-
extern crate rustc_serialize;
103103
#[macro_use]
104104
extern crate scan_fmt;
105105
extern crate semver;

tests/client/crud_spec/framework.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ macro_rules! run_update_test {
243243
#[macro_export]
244244
macro_rules! run_suite {
245245
( $file:expr, $coll:expr ) => {{
246-
let json = Json::from_file($file).unwrap();
246+
let json = Value::from_file($file).unwrap();
247247
let suite = json.get_suite().unwrap();
248248
let client = Client::connect("localhost", 27017).unwrap();
249249
let db = client.db("test");

tests/client/crud_spec/read.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use json::eq::{self, NumEq};
55
use mongodb::{Client, ThreadedClient};
66
use mongodb::coll::options::{InsertManyOptions, ReplaceOptions, UpdateOptions};
77
use mongodb::db::ThreadedDatabase;
8-
use rustc_serialize::json::Json;
8+
use serde_json::Value;
99

1010
#[test]
1111
fn aggregate() {

tests/client/crud_spec/write.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use json::eq::{self, NumEq};
55
use mongodb::{Client, ThreadedClient};
66
use mongodb::coll::options::{InsertManyOptions, ReplaceOptions, UpdateOptions};
77
use mongodb::db::ThreadedDatabase;
8-
use rustc_serialize::json::Json;
8+
use serde_json::Value;
99

1010
#[test]
1111
fn delete_many() {

tests/json/arguments.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use bson::{Bson, Document};
2-
use json::options::FromJson;
2+
use json::options::FromValue;
33
use mongodb::coll::options::{AggregateOptions, CountOptions, FindOneAndDeleteOptions,
44
FindOneAndUpdateOptions, FindOptions};
5-
use rustc_serialize::json::Object;
5+
use serde_json::Map;
66

77
pub enum Arguments {
88
Aggregate {
@@ -53,7 +53,7 @@ pub enum Arguments {
5353
}
5454

5555
impl Arguments {
56-
pub fn aggregate_from_json(object: &Object) -> Result<Arguments, String> {
56+
pub fn aggregate_from_json(object: &Map<String, Value>) -> Result<Arguments, String> {
5757
let options = AggregateOptions::from_json(object);
5858

5959
let f = |x| Some(Bson::from_json(x));
@@ -84,7 +84,7 @@ impl Arguments {
8484
})
8585
}
8686

87-
pub fn count_from_json(object: &Object) -> Arguments {
87+
pub fn count_from_json(object: &Map<String, Value>) -> Arguments {
8888
let options = CountOptions::from_json(object);
8989

9090
let f = |x| Some(Bson::from_json(x));
@@ -99,7 +99,7 @@ impl Arguments {
9999
}
100100
}
101101

102-
pub fn delete_from_json(object: &Object, many: bool) -> Result<Arguments, String> {
102+
pub fn delete_from_json(object: &Map<String, Value>, many: bool) -> Result<Arguments, String> {
103103
let f = |x| Some(Bson::from_json(x));
104104
let document = val_or_err!(object.get("filter").and_then(f),
105105
Some(Bson::Document(doc)) => doc,
@@ -111,7 +111,7 @@ impl Arguments {
111111
})
112112
}
113113

114-
pub fn distinct_from_json(object: &Object) -> Result<Arguments, String> {
114+
pub fn distinct_from_json(object: &Map<String, Value>) -> Result<Arguments, String> {
115115
let f = |x| Some(Bson::from_json(x));
116116
let field_name = val_or_err!(object.get("fieldName").and_then(f),
117117
Some(Bson::String(ref s)) => s.to_owned(),
@@ -129,7 +129,7 @@ impl Arguments {
129129
})
130130
}
131131

132-
pub fn find_from_json(object: &Object) -> Arguments {
132+
pub fn find_from_json(object: &Map<String, Value>) -> Arguments {
133133
let options = FindOptions::from_json(object);
134134

135135
let f = |x| Some(Bson::from_json(x));
@@ -144,7 +144,7 @@ impl Arguments {
144144
}
145145
}
146146

147-
pub fn find_one_and_delete_from_json(object: &Object) -> Result<Arguments, String> {
147+
pub fn find_one_and_delete_from_json(object: &Map<String, Value>) -> Result<Arguments, String> {
148148
let options = FindOneAndDeleteOptions::from_json(object);
149149

150150
let f = |x| Some(Bson::from_json(x));
@@ -158,7 +158,7 @@ impl Arguments {
158158
})
159159
}
160160

161-
pub fn find_one_and_replace_from_json(object: &Object) -> Result<Arguments, String> {
161+
pub fn find_one_and_replace_from_json(object: &Map<String, Value>) -> Result<Arguments, String> {
162162
let options = FindOneAndUpdateOptions::from_json(object);
163163

164164
let f = |x| Some(Bson::from_json(x));
@@ -178,7 +178,7 @@ impl Arguments {
178178
})
179179
}
180180

181-
pub fn find_one_and_update_from_json(object: &Object) -> Result<Arguments, String> {
181+
pub fn find_one_and_update_from_json(object: &Map<String, Value>) -> Result<Arguments, String> {
182182
let options = FindOneAndUpdateOptions::from_json(object);
183183

184184
let f = |x| Some(Bson::from_json(x));
@@ -198,7 +198,7 @@ impl Arguments {
198198
})
199199
}
200200

201-
pub fn insert_many_from_json(object: &Object) -> Result<Arguments, String> {
201+
pub fn insert_many_from_json(object: &Map<String, Value>) -> Result<Arguments, String> {
202202
let f = |x| Some(Bson::from_json(x));
203203

204204
let bsons = val_or_err!(object.get("documents").and_then(f),
@@ -217,7 +217,7 @@ impl Arguments {
217217
Ok(Arguments::InsertMany { documents: docs })
218218
}
219219

220-
pub fn insert_one_from_json(object: &Object) -> Result<Arguments, String> {
220+
pub fn insert_one_from_json(object: &Map<String, Value>) -> Result<Arguments, String> {
221221
let f = |x| Some(Bson::from_json(x));
222222
let document = val_or_err!(object.get("document").and_then(f),
223223
Some(Bson::Document(doc)) => doc,
@@ -226,7 +226,7 @@ impl Arguments {
226226
Ok(Arguments::InsertOne { document: document })
227227
}
228228

229-
pub fn replace_one_from_json(object: &Object) -> Result<Arguments, String> {
229+
pub fn replace_one_from_json(object: &Map<String, Value>) -> Result<Arguments, String> {
230230
let f = |x| Some(Bson::from_json(x));
231231
let filter = val_or_err!(object.get("filter").and_then(f),
232232
Some(Bson::Document(doc)) => doc,
@@ -248,7 +248,7 @@ impl Arguments {
248248
})
249249
}
250250

251-
pub fn update_from_json(object: &Object, many: bool) -> Result<Arguments, String> {
251+
pub fn update_from_json(object: &Map<String, Value>, many: bool) -> Result<Arguments, String> {
252252
let f = |x| Some(Bson::from_json(x));
253253
let filter = val_or_err!(object.get("filter").and_then(f),
254254
Some(Bson::Document(doc)) => doc,

0 commit comments

Comments
 (0)