diff --git a/README.md b/README.md index 3e83376..e438772 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Sofa - CouchDB for Rust -[![Crates.io](https://img.shields.io/crates/v/sofa.svg)](https://crates.io/crates/sofa) +[![Crates.io](https://img.shields.io/crates/v/sofa.svg)](https://crates.io/crates/sofa) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FYellowInnovation%2Fsofa.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2FYellowInnovation%2Fsofa?ref=badge_shield) + [![docs.rs](https://docs.rs/sofa/badge.svg)](https://docs.rs/sofa) ![sofa-logo](https://raw.githubusercontent.com/YellowInnovation/sofa/master/docs/logo-sofa.png "Logo Sofa") @@ -54,10 +55,13 @@ Licensed under either of these: * MIT license ([LICENSE-MIT](LICENSE-MIT) or [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)) + +[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2FYellowInnovation%2Fsofa.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2FYellowInnovation%2Fsofa?ref=badge_large) + ## Yellow Innovation Yellow Innovation is the innovation laboratory of the French postal service: La Poste. We create innovative user experiences and journeys through services with a focus on IoT lately. -[Yellow Innovation's website and works](http://yellowinnovation.fr/en/) +[Yellow Innovation's website and works](http://yellowinnovation.fr/en/) \ No newline at end of file diff --git a/src/client.rs b/src/client.rs index 7c21680..6be7feb 100644 --- a/src/client.rs +++ b/src/client.rs @@ -22,7 +22,7 @@ pub struct Client { } impl Client { - pub fn new(uri: String) -> Result { + pub fn new>(uri: S) -> Result { let client = reqwest::Client::builder() .gzip(true) .timeout(Duration::new(4, 0)) @@ -30,7 +30,7 @@ impl Client { Ok(Client { _client: client, - uri: uri, + uri: uri.into(), _gzip: true, _timeout: 4, dbs: Vec::new(), @@ -51,13 +51,13 @@ impl Client { self } - pub fn set_uri(&mut self, uri: String) -> &Self { - self.uri = uri; + pub fn set_uri>(&mut self, uri: S) -> &Self { + self.uri = uri.into(); self } - pub fn set_prefix(&mut self, prefix: String) -> &Self { - self.db_prefix = prefix; + pub fn set_prefix>(&mut self, prefix: S) -> &Self { + self.db_prefix = prefix.into(); self } @@ -68,8 +68,8 @@ impl Client { Ok(self) } - pub fn timeout(&mut self, to: u8) -> Result<&Self, Error> { - self._timeout = to; + pub fn timeout>(&mut self, to: U) -> Result<&Self, Error> { + self._timeout = to.into(); self._client = self.create_client()?; Ok(self) @@ -82,12 +82,12 @@ impl Client { Ok(data) } - fn build_dbname(&self, dbname: &'static str) -> String { - self.db_prefix.clone() + dbname + fn build_dbname>(&self, dbname: S) -> String { + format!("{}{}", self.db_prefix, dbname.as_ref()) } - pub fn db(&self, dbname: &'static str) -> Result { - let name = self.build_dbname(dbname); + pub fn db>(&self, dbname: S) -> Result { + let name = self.build_dbname(&dbname); let db = Database::new(name.clone(), self.clone()); @@ -99,12 +99,12 @@ impl Client { match head_response.status() { StatusCode::Ok => Ok(db), - _ => self.make_db(dbname), + _ => self.make_db(&dbname), } } - pub fn make_db(&self, dbname: &'static str) -> Result { - let name = self.build_dbname(dbname); + pub fn make_db>(&self, dbname: S) -> Result { + let name = self.build_dbname(&dbname); let db = Database::new(name.clone(), self.clone()); @@ -125,7 +125,7 @@ impl Client { } } - pub fn destroy_db(&self, dbname: &'static str) -> Result { + pub fn destroy_db>(&self, dbname: S) -> Result { let path = self.create_path(self.build_dbname(dbname), None)?; let response = self._client.delete(&path) .header(reqwest::header::ContentType::json()) @@ -146,11 +146,11 @@ impl Client { Ok(status) } - fn create_path(&self, - path: String, + fn create_path>(&self, + path: S, args: Option> ) -> Result { - let mut uri = Url::parse(&self.uri)?.join(&path)?; + let mut uri = Url::parse(&self.uri)?.join(path.as_ref())?; if let Some(ref map) = args { let mut qp = uri.query_pairs_mut(); @@ -162,9 +162,9 @@ impl Client { Ok(uri.into_string()) } - pub fn req(&self, + pub fn req>(&self, method: Method, - path: String, + path: S, opts: Option> ) -> Result { let uri = self.create_path(path, opts)?; @@ -175,27 +175,27 @@ impl Client { Ok(req) } - pub fn get(&self, path: String, args: Option>) -> Result { + pub fn get>(&self, path: S, args: Option>) -> Result { Ok(self.req(Method::Get, path, args)?) } - pub fn post(&self, path: String, body: String) -> Result { + pub fn post>(&self, path: S, body: String) -> Result { let mut req = self.req(Method::Post, path, None)?; req.body(body); Ok(req) } - pub fn put(&self, path: String, body: String) -> Result { + pub fn put>(&self, path: S, body: String) -> Result { let mut req = self.req(Method::Put, path, None)?; req.body(body); Ok(req) } - pub fn head(&self, path: String, args: Option>) -> Result { + pub fn head>(&self, path: S, args: Option>) -> Result { Ok(self.req(Method::Head, path, args)?) } - pub fn delete(&self, path: String, args: Option>) -> Result { + pub fn delete>(&self, path: S, args: Option>) -> Result { Ok(self.req(Method::Delete, path, args)?) } } diff --git a/src/database.rs b/src/database.rs index bb482b0..cea97f6 100644 --- a/src/database.rs +++ b/src/database.rs @@ -20,10 +20,10 @@ pub struct Database { } impl Database { - pub fn new(name: String, client: Client) -> Database { + pub fn new>(name: S, client: Client) -> Database { Database { _client: client, - name: name, + name: name.into(), } } @@ -42,10 +42,10 @@ impl Database { result } - fn create_compact_path(&self, design_name: &'static str) -> String { + fn create_compact_path>(&self, design_name: S) -> String { let mut result: String = self.name.clone(); result.push_str("/_compact/"); - result.push_str(design_name); + result.push_str(design_name.as_ref()); result } @@ -82,7 +82,7 @@ impl Database { } /// Starts the compaction of a given index - pub fn compact_index(&self, index: &'static str) -> bool { + pub fn compact_index>(&self, index: S) -> bool { let request = self._client.post(self.create_compact_path(index), "".into()); request @@ -281,12 +281,12 @@ impl Database { /// Inserts an index in a naive way, if it already exists, will throw an /// `Err` - pub fn insert_index(&self, name: String, spec: IndexFields) -> Result { + pub fn insert_index>(&self, name: S, spec: IndexFields) -> Result { let response = self._client .post( self.create_document_path("_index".into()), js!(json!({ - "name": name, + "name": name.into(), "index": spec })), )? @@ -314,19 +314,20 @@ impl Database { /// Method to ensure an index is created on the database with the following /// spec. Returns `true` when we created a new one, or `false` when the /// index was already existing. - pub fn ensure_index(&self, name: String, spec: IndexFields) -> Result { + pub fn ensure_index>(&self, name: S, spec: IndexFields) -> Result { + let n = name.into(); let db_indexes = self.read_indexes()?; // We look for our index for i in db_indexes.indexes.into_iter() { - if i.name == name { + if &i.name == &n { // Found? Ok let's return return Ok(false); } } // Let's create it then - let _ = self.insert_index(name, spec)?; + let _ = self.insert_index(n, spec)?; // Created and alright Ok(true) diff --git a/src/document.rs b/src/document.rs index ca33f78..d4f93d3 100644 --- a/src/document.rs +++ b/src/document.rs @@ -68,8 +68,8 @@ impl Document { /// Recursively populates field (must be an array of IDs from another /// database) with provided database documents - pub fn populate(&mut self, field: &String, db: Database) -> &Self { - let ref val = self[field].clone(); + pub fn populate>(&mut self, field: S, db: Database) -> &Self { + let ref val = self[field.as_ref()].clone(); if *val == Value::Null { return self; } @@ -84,7 +84,7 @@ impl Document { match data { Ok(data) => { - self[field] = data.into_iter() + self[field.as_ref()] = data.into_iter() .filter_map(|d: Value| { let did = match d["_id"].as_str() { Some(did) => did, diff --git a/src/lib.rs b/src/lib.rs index ad38104..0160b10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -156,14 +156,14 @@ mod sofa_tests { #[test] fn a_should_check_couchdbs_status() { - let client = Client::new("http://localhost:5984".into()).unwrap(); + let client = Client::new("http://localhost:5984").unwrap(); let status = client.check_status(); assert!(status.is_ok()); } #[test] fn b_should_create_sofa_test_db() { - let client = Client::new("http://localhost:5984".into()).unwrap(); + let client = Client::new("http://localhost:5984").unwrap(); let dbw = client.db("b_should_create_sofa_test_db"); assert!(dbw.is_ok()); @@ -172,7 +172,7 @@ mod sofa_tests { #[test] fn c_should_create_a_document() { - let client = Client::new("http://localhost:5984".into()).unwrap(); + let client = Client::new("http://localhost:5984").unwrap(); let dbw = client.db("c_should_create_a_document"); assert!(dbw.is_ok()); let db = dbw.unwrap(); @@ -191,7 +191,7 @@ mod sofa_tests { #[test] fn d_should_destroy_the_db() { - let client = Client::new("http://localhost:5984".into()).unwrap(); + let client = Client::new("http://localhost:5984").unwrap(); let _ = client.db("d_should_destroy_the_db"); assert!(client.destroy_db("d_should_destroy_the_db").unwrap()); @@ -202,7 +202,7 @@ mod sofa_tests { use *; fn setup(dbname: &'static str) -> (Client, Database, Document) { - let client = Client::new("http://localhost:5984".into()).unwrap(); + let client = Client::new("http://localhost:5984").unwrap(); let dbw = client.db(dbname); assert!(dbw.is_ok()); let db = dbw.unwrap(); @@ -257,7 +257,7 @@ mod sofa_tests { let spec = types::IndexFields::new(vec![types::SortSpec::Simple(s!("thing"))]); - let res = db.insert_index("thing-index".into(), spec); + let res = db.insert_index("thing-index", spec); assert!(res.is_ok()); @@ -289,7 +289,7 @@ mod sofa_tests { let spec = types::IndexFields::new(vec![types::SortSpec::Simple(s!("thing"))]); - let res = db.ensure_index("thing-index".into(), spec); + let res = db.ensure_index("thing-index", spec); assert!(res.is_ok()); teardown(client, "f_should_ensure_index_in_db");