Repository: toml-rs/toml (language: rust)

Issue:
Replace value preserving decoration
I have `&mut Value` and want to replace value preserving decoration.

There is `value` function, but it always decorate with leading space and returns `Item`. 
There is `decorated` method, but it is in private `formatted` module and isn't reexported.

Am I missing anything?


Discussion / hints:

Fix patch:
diff --git a/src/decor.rs b/src/decor.rs
index a3145393..d4585dd0 100644
--- a/src/decor.rs
+++ b/src/decor.rs
@@ -14,23 +14,34 @@ pub(crate) struct Repr {
     pub raw_value: InternalString,
 }
 
-// A prefix and suffix,
-// including comments, whitespaces and newlines.
+/// A prefix and suffix,
+/// including comments, whitespaces and newlines.
 #[derive(Eq, PartialEq, Clone, Default, Debug, Hash)]
-pub(crate) struct Decor {
-    pub prefix: InternalString,
-    pub suffix: InternalString,
+pub struct Decor {
+    pub(crate) prefix: InternalString,
+    pub(crate) suffix: InternalString,
 }
 
 pub(crate) type InternalString = String;
 
 impl Decor {
+    /// Creates a new decor from the given prefix and suffix.
     pub fn new<S: Into<InternalString>>(prefix: S, suffix: S) -> Self {
         Self {
             prefix: prefix.into(),
             suffix: suffix.into(),
         }
     }
+
+    /// Get the prefix.
+    pub fn prefix(&self) -> &str {
+        &self.prefix
+    }
+
+    /// Get the suffix.
+    pub fn suffix(&self) -> &str {
+        &self.suffix
+    }
 }
 
 impl Repr {
diff --git a/src/formatted.rs b/src/formatted.rs
index 8c8d3e3f..6016b77b 100644
--- a/src/formatted.rs
+++ b/src/formatted.rs
@@ -57,6 +57,14 @@ pub(crate) fn decorate(value: &mut Value, prefix: &str, suffix: &str) {
     decor.suffix = InternalString::from(suffix);
 }
 
+/// Sets the prefix and the suffix for value.
+/// # Example
+/// ```rust
+/// let mut v = toml_edit::Value::from(42);
+/// assert_eq!(&v.to_string(), "42");
+/// let d = toml_edit::decorated(v, " ", " ");
+/// assert_eq!(&d.to_string(), " 42 ");
+/// ```
 pub fn decorated(mut value: Value, prefix: &str, suffix: &str) -> Value {
     {
         decorate(&mut value, prefix, suffix);
diff --git a/src/lib.rs b/src/lib.rs
index 87b67518..66876ce7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -88,3 +88,4 @@ pub use crate::key::Key;
 pub use crate::parser::TomlError;
 pub use crate::table::{array, table, value, Item, Iter, Table, TableLike};
 pub use crate::value::{Array, InlineTable, Value};
+pub use formatted::decorated;
diff --git a/src/value.rs b/src/value.rs
index f6d4b611..1f439413 100644
--- a/src/value.rs
+++ b/src/value.rs
@@ -413,6 +413,26 @@ impl Value {
     }
 }
 
+impl Value {
+    /// Get the decoration of the value.
+    /// # Example
+    /// ```rust
+    /// let v = toml_edit::Value::from(true);
+    /// assert_eq!(v.decor().suffix(), "");
+    ///```
+    pub fn decor(&self) -> &Decor {
+        match *self {
+            Value::Integer(ref f) => &f.repr.decor,
+            Value::String(ref f) => &f.repr.decor,
+            Value::Float(ref f) => &f.repr.decor,
+            Value::DateTime(ref f) => &f.repr.decor,
+            Value::Boolean(ref f) => &f.repr.decor,
+            Value::Array(ref a) => &a.decor,
+            Value::InlineTable(ref t) => &t.decor,
+        }
+    }
+}
+
 pub(crate) fn sort_key_value_pairs(items: &mut LinkedHashMap<InternalString, TableKeyValue>) {
     let mut keys: Vec<InternalString> = items
         .iter()


Test patch: