Repository: mehcode/config-rs (rust)

Issue:
Function misnamed, should corrrelate to type
Rust has types for both `str` and `String`. There are tons of functions in *std* and other crates related to the two, `as_str()`, `as_string()`, `from_string()`, `to_string()`, `to_str()`, etc.

This function is cross wired:

https://github.com/mehcode/config-rs/blob/2c0b201055be60ec45e258adcc6c4b6e01bcd627/src/config.rs#L172

It should either be named `get_string()` or return a `str` but not switch them out! Perhaps there should be two functions, one for each...

Pull request description:
Rename get_str() → get_string() to match returned type
Closes #140.

I couldn't find any good reason to make this return `&str` instead, the handling everywhere else is as `String` so just renaming the function to match what it already does seemed most logical thing to do.

This will be a breaking change to the API, but the fix will be easy. All instances of `Config.get_str()` need to be changed to `Config.get_string()`. The returned value being handled in people's code is already `String`.


Patch:
diff --git a/src/config.rs b/src/config.rs
index 94658da..0377438 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -211,8 +211,8 @@ impl Config {
         }
     }
 
-    pub fn get_str(&self, key: &str) -> Result<String> {
-        self.get(key).and_then(Value::into_str)
+    pub fn get_string(&self, key: &str) -> Result<String> {
+        self.get(key).and_then(Value::into_string)
     }
 
     pub fn get_int(&self, key: &str) -> Result<i64> {
diff --git a/src/de.rs b/src/de.rs
index c4fdeb2..0e88642 100644
--- a/src/de.rs
+++ b/src/de.rs
@@ -89,12 +89,12 @@ impl<'de> de::Deserializer<'de> for Value {
 
     #[inline]
     fn deserialize_str<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
-        visitor.visit_string(self.into_str()?)
+        visitor.visit_string(self.into_string()?)
     }
 
     #[inline]
     fn deserialize_string<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
-        visitor.visit_string(self.into_str()?)
+        visitor.visit_string(self.into_string()?)
     }
 
     #[inline]
@@ -421,12 +421,12 @@ impl<'de> de::Deserializer<'de> for Config {
 
     #[inline]
     fn deserialize_str<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
-        visitor.visit_string(self.cache.into_str()?)
+        visitor.visit_string(self.cache.into_string()?)
     }
 
     #[inline]
     fn deserialize_string<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
-        visitor.visit_string(self.cache.into_str()?)
+        visitor.visit_string(self.cache.into_string()?)
     }
 
     #[inline]
diff --git a/src/value.rs b/src/value.rs
index 177acf7..602b40a 100644
--- a/src/value.rs
+++ b/src/value.rs
@@ -286,9 +286,9 @@ impl Value {
         }
     }
 
-    /// Returns `self` into a str, if possible.
+    /// Returns `self` into a string, if possible.
     // FIXME: Should this not be `try_into_*` ?
-    pub fn into_str(self) -> Result<String> {
+    pub fn into_string(self) -> Result<String> {
         match self.kind {
             ValueKind::String(value) => Ok(value),
 


Test patch:
diff --git a/tests/file_hjson.rs b/tests/file_hjson.rs
index 87318eb..b76ccb5 100644
--- a/tests/file_hjson.rs
+++ b/tests/file_hjson.rs
@@ -60,7 +60,7 @@ fn test_file() {
     assert_eq!(s.elements.len(), 10);
     assert_eq!(s.elements[3], "4".to_string());
     assert_eq!(
-        s.place.creator["name"].clone().into_str().unwrap(),
+        s.place.creator["name"].clone().into_string().unwrap(),
         "John Smith".to_string()
     );
 }
diff --git a/tests/file_json.rs b/tests/file_json.rs
index 2c614e1..b8dcbf0 100644
--- a/tests/file_json.rs
+++ b/tests/file_json.rs
@@ -60,7 +60,7 @@ fn test_file() {
     assert_eq!(s.elements.len(), 10);
     assert_eq!(s.elements[3], "4".to_string());
     assert_eq!(
-        s.place.creator["name"].clone().into_str().unwrap(),
+        s.place.creator["name"].clone().into_string().unwrap(),
         "John Smith".to_string()
     );
 }
@@ -98,7 +98,7 @@ fn test_json_vec() {
 
     let v = c.get_array("WASTE").unwrap();
     let mut vi = v.into_iter();
-    assert_eq!(vi.next().unwrap().into_str().unwrap(), "example_dir1");
-    assert_eq!(vi.next().unwrap().into_str().unwrap(), "example_dir2");
+    assert_eq!(vi.next().unwrap().into_string().unwrap(), "example_dir1");
+    assert_eq!(vi.next().unwrap().into_string().unwrap(), "example_dir2");
     assert!(vi.next().is_none());
 }
diff --git a/tests/file_toml.rs b/tests/file_toml.rs
index 834499f..de52548 100644
--- a/tests/file_toml.rs
+++ b/tests/file_toml.rs
@@ -71,7 +71,7 @@ fn test_file() {
     assert_eq!(s.elements.len(), 10);
     assert_eq!(s.elements[3], "4".to_string());
     assert_eq!(
-        s.place.creator["name"].clone().into_str().unwrap(),
+        s.place.creator["name"].clone().into_string().unwrap(),
         "John Smith".to_string()
     );
 }
diff --git a/tests/file_yaml.rs b/tests/file_yaml.rs
index 59174e6..892875e 100644
--- a/tests/file_yaml.rs
+++ b/tests/file_yaml.rs
@@ -60,7 +60,7 @@ fn test_file() {
     assert_eq!(s.elements.len(), 10);
     assert_eq!(s.elements[3], "4".to_string());
     assert_eq!(
-        s.place.creator["name"].clone().into_str().unwrap(),
+        s.place.creator["name"].clone().into_string().unwrap(),
         "John Smith".to_string()
     );
 }
diff --git a/tests/get.rs b/tests/get.rs
index 10ec066..8d6db51 100644
--- a/tests/get.rs
+++ b/tests/get.rs
@@ -111,7 +111,7 @@ fn test_map() {
 
     assert_eq!(m.len(), 8);
     assert_eq!(
-        m["name"].clone().into_str().unwrap(),
+        m["name"].clone().into_string().unwrap(),
         "Torre di Pisa".to_string()
     );
     assert_eq!(m["reviews"].clone().into_int().unwrap(), 3866);
@@ -138,7 +138,7 @@ fn test_map_struct() {
 
     assert_eq!(s.place.len(), 8);
     assert_eq!(
-        s.place["name"].clone().into_str().unwrap(),
+        s.place["name"].clone().into_string().unwrap(),
         "Torre di Pisa".to_string()
     );
     assert_eq!(s.place["reviews"].clone().into_int().unwrap(), 3866);