introduce dechunk and rechunk as well as change punctuation error in docs

This commit is contained in:
abbie 2022-03-13 19:44:19 +00:00
parent 9c5290813a
commit 8a5a828dbc
No known key found for this signature in database
GPG key ID: 04DDE463F9200F87
2 changed files with 31 additions and 2 deletions

View file

@ -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 <colean@colean.cc>"]
license = "LGPL-3.0-or-later"

View file

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