Ver Fonte

Adds docs for many new methods to Data

Also renames some Vec/Map only methods so they are very clear they only
work with the specific type as prefixed.
Steve Thielemann há 2 meses atrás
pai
commit
f4d05a9b5c
1 ficheiros alterados com 57 adições e 15 exclusões
  1. 57 15
      src/data.rs

+ 57 - 15
src/data.rs

@@ -564,30 +564,44 @@ impl Data {
     pub fn is_nested(&self) -> bool {
         matches!(self, Self::Vec(_) | Self::Map(_))
     }
-    // socks, blue, bed
+    /// Is Data the Data::Vec variant (Vec<Data>)
     pub fn is_vec(&self) -> bool {
         matches!(self, Self::Vec(_))
     }
+    /// Is Data the Data::Map variant (HashMap<String, Data>)
     pub fn is_map(&self) -> bool {
         matches!(self, Self::Map(_))
     }
-    pub fn push(&mut self, v: Data) {
+    /// Push a value into the Data
+    /// 
+    /// Must be a Vec or no-op
+    pub fn vec_push(&mut self, v: Data) {
         if let Self::Vec(vec) = self {
             vec.push(v);
         }
     }
-    pub fn insert(&mut self, k: String, v: Data) {
+    /// Assigns/Reassigns a key the value into the Data
+    /// 
+    /// Must be a Map or no-op
+    pub fn map_insert(&mut self, k: String, v: Data) {
         if let Self::Map(hm) = self {
             hm.insert(k, v);
         }
     }
+    /// Obtains the length of String, Vec or Map
+    /// 
+    /// All other variants of Data will return 0
     pub fn len(&self) -> usize {
         match self {
+            Self::String(s) => s.len(),
             Self::Vec(v) => v.len(),
             Self::Map(m) => m.len(),
             _ => 0,
         }
     }
+    /// Obtains the capacity of Vec or Map
+    /// 
+    /// All other variants of Data return 0
     pub fn capacity(&self) -> usize {
         match self {
             Self::Vec(v) => v.capacity(),
@@ -595,6 +609,9 @@ impl Data {
             _ => 0,
         }
     }
+    /// Reserve {additional} in a Vec or Map
+    /// 
+    /// This no-op's if the Data variants are not Vec or Map
     pub fn reserve(&mut self, additional: usize) {
         match self {
             Self::Vec(v) => v.reserve(additional),
@@ -602,6 +619,7 @@ impl Data {
             _ => (),
         }
     }
+    /// Shrinks a Vec or Map to fit all that is within it
     pub fn shrink_to_fit(&mut self) {
         match self {
             Self::Vec(v) => v.shrink_to_fit(),
@@ -609,6 +627,7 @@ impl Data {
             _ => (),
         }
     }
+    /// Shrinks to a given minimum length for Vec or Map
     pub fn shrink_to(&mut self, min: usize) {
         match self {
             Self::Vec(v) => v.shrink_to(min),
@@ -616,28 +635,36 @@ impl Data {
             _ => (),
         }
     }
-    pub fn truncate(&mut self, len: usize) {
+    /// Truncates Vec to a set length, see [Vec::truncate]
+    pub fn vec_truncate(&mut self, len: usize) {
         if let Self::Vec(v) = self { v.truncate(len) }
     }
-    pub fn remove(&mut self, index: usize) -> Data {
+    /// Removes Vec at given index, see [Vec::remove]
+    pub fn vec_remove(&mut self, index: usize) -> Data {
         match self {
             Self::Vec(v) => v.remove(index),
             _ => Data::None,
         }
     }
-    pub fn pop(&mut self) -> Option<Data> {
+    /// Removes the last item in the Vec, see [Vec::pop]
+    /// 
+    /// This will return None also if the Data isn't a Vec
+    pub fn vec_pop(&mut self) -> Option<Data> {
         match self {
             Self::Vec(v) => v.pop(),
             _ => None,
         }
     }
+    /// Clears all in a String, Vec or Map
     pub fn clear(&mut self) {
         match self {
+            Self::String(s) => s.clear(),
             Self::Vec(v) => v.clear(),
             Self::Map(m) => m.clear(),
             _ => (),
         }
     }
+    /// Returns if String, Vec or Map is empty (has no elements)
     pub fn is_empty(&self) -> bool {
         match self {
             Self::String(s) => s.is_empty(),
@@ -646,34 +673,40 @@ impl Data {
             _ => false,
         }
     }
-    pub fn resize(&mut self, len: usize, value: Data) {
+    /// Resizes the Vec with the given length, assigning new with value
+    pub fn vec_resize(&mut self, len: usize, value: Data) {
         if let Self::Vec(v) = self { v.resize(len, value) }
     }
-    pub fn first(&self) -> Option<&Data> {
+    /// Obtains the first element in Vec
+    pub fn vec_first(&self) -> Option<&Data> {
         match self {
             Self::Vec(v) => v.first(),
             _ => None,
         }
     }
-    pub fn first_mut(&mut self) -> Option<&mut Data> {
+    /// Similar to [Data::vec_first] except it obtains as mutable
+    pub fn vec_first_mut(&mut self) -> Option<&mut Data> {
         match self {
             Self::Vec(v) => v.first_mut(),
             _ => None,
         }
     }
-    pub fn last(&self) -> Option<&Data> {
+    /// Similar to [Data::vec_first] except obtains the last element of Vec
+    pub fn vec_last(&self) -> Option<&Data> {
         match self {
             Self::Vec(v) => v.last(),
             _ => None,
         }
     }
-    pub fn last_mut(&mut self) -> Option<&mut Data> {
+    /// Similar to [Data::vec_last] except obtains as mutable
+    pub fn vec_last_mut(&mut self) -> Option<&mut Data> {
         match self {
             Self::Vec(v) => v.last_mut(),
             _ => None,
         }
     }
-    pub fn get<I>(&self, index: I) -> Option<&I::Output>
+    /// Gets by index or range for a Vec, see [Vec::get]
+    pub fn vec_get<I>(&self, index: I) -> Option<&I::Output>
     where
         I: SliceIndex<[Data]>,
     {
@@ -682,6 +715,7 @@ impl Data {
             _ => None,
         }
     }
+    /// Gets like [Data::vec_get] except as mutable 
     pub fn get_mut<I>(&mut self, index: I) -> Option<&mut I::Output>
     where
         I: SliceIndex<[Data]>,
@@ -691,18 +725,26 @@ impl Data {
             _ => None,
         }
     }
-    pub fn swap(&mut self, a: usize, b: usize) {
+    /// Swaps a with b in a Vec, see [Vec::swap]
+    pub fn vec_swap(&mut self, a: usize, b: usize) {
         if let Self::Vec(v) = self { v.swap(a, b) }
     }
-    pub fn reverse(&mut self) {
+    /// Reverses the order of a Vec, see [Vec::reverse]
+    pub fn vec_reverse(&mut self) {
         if let Self::Vec(v) = self { v.reverse() }
     }
+    /// Attempts to form an iterator for Vec
+    /// 
+    /// This can fail if Data isn't a Vec
     pub fn vec_iter(&self) -> Option<Iter<'_, Data>> {
         match self {
             Self::Vec(v) => Some(v.iter()),
             _ => None,
         }
     }
+    /// Attempts to form a mutable iterator for Vec
+    /// 
+    /// This can fail if Data isn't a Vec
     pub fn vec_iter_mut(&mut self) -> Option<IterMut<'_, Data>> {
         match self {
             Self::Vec(v) => Some(v.iter_mut()),
@@ -722,4 +764,4 @@ impl Data {
             _ => 0
         }
     }
-}
+}