From 8a5a828dbcd643c8a1c04a58725882221f15b367 Mon Sep 17 00:00:00 2001 From: Celeste Date: Sun, 13 Mar 2022 19:44:19 +0000 Subject: [PATCH] introduce dechunk and rechunk as well as change punctuation error in docs --- Cargo.toml | 2 +- src/lib.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ba6c941..5b0505e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "clippet" description = "Provides a teeny way to have some nice byte structures that's easy to use." -version = "0.1.5" +version = "0.1.6" edition = "2021" authors = ["Celeste "] license = "LGPL-3.0-or-later" diff --git a/src/lib.rs b/src/lib.rs index 9e06416..fa6795c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -144,7 +144,7 @@ impl Clippet { return clip } - /// Consumes the Clippet, combines all of it's byte sequences and outputs them as a String literal. + /// Consumes the Clippet, combines all of its byte sequences and outputs them as a String literal. /// /// ```no_run /// let data = Clippet::from_string("Hello, World!", 512); @@ -163,4 +163,33 @@ impl Clippet { } return string } + /// Replaces the Clippet with one that has all of its data in one chunk. + /// The label is retained, the data is not modified apart from being dechunked. + /// + /// ```no_run + /// let data = Clippet::from_string("HelloWorld", 5); // [b"Hello", b"World"] + /// let data = data.dechunk(); // [b"HelloWorld"] + /// ``` + pub fn dechunk(self) -> Clippet { + // Retain label. + let label = self.label.to_string(); + let data = self.combine(); + let clip = Clippet { label: label, size: 1, data: vec![BytesMut::from(data.as_str())] }; + return clip + } + /// Combines the Clippet and then remakes it, retaining the label, with the specified max chunk size. + /// No data is modified. + /// + /// ```no_run + /// let longstring = "long string bla bla bla..."; // Pretend this is 2KB large! + /// let data = Clippet::from_string(longstring, 512); // size: 4 + /// let data = data.rechunk(1024) // size: 2 + /// ``` + pub fn rechunk(self, size: usize) -> Clippet { + let label = self.label.to_string(); + let data = self.combine(); + let mut clip = Clippet::from_string(&data, size); + if label != "".to_string() { clip.label = label; } + return clip + } }