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 + } }