Impl ToString for Builder, accept AsRef<[Node]> in `render()`
continuous-integration/drone/pr Build is failing Details

This adds a to_string method to the `Builder` allowing for the easy conversion of a Vec<Node> into a String, for any usecases where a library might not be directly writing to an io::Write, or may want to do String-y things with your document first.  Without this, users would have to write to a Vec<u8> and convert to a String, which is kinda unintuitive, takes a lot of steps, and doesn't produce very readable code.  This simplifies it to one method call.

* Implementation of the std::str::ToString method for Builder
* Accepting any AsRef<[Node]> in render (including accepting the old Vec<Node>, so not breaking)
* Addition of estimate_len() to Node, used to pre-allocate the correct size of the String buffer

* `estimate_len` has some quick doctests and examples.  I know most of the rest of the project uses test methods, but I hope this is alright given that the tests may add some more clarity to the purpose and function of the method.
* `to_string` has a single line of unsafe code.  As the associated comment explains, this is provably safe, and exists just to avoid having to choose between having a bunch of duplicate code or inefficiently performing a UTF-8 check on a whole bunch bytes that we already know are safe.  That said, I totally get it if you're just generally against unsafe code and will change it to be an alternative if you so wish
* ToString is implemented instead of Display.  This is to discourage users from directly using this in a println!() or write!() macro, which would not be a thing you would normally expect to do with this.  It also gives us the advantage of being able to pre-allocate a buffer size, meaning less expensive String resizing.
* I couldn't think of a clever way to get `render()` to work with both `io::Write`s or `fmt::Write`s without duplicating the code, but I'm dumb and might be missing something, so if there's a way to do that instead of doing my funky unsafe hack that's cool and I can do that instead.
This commit is contained in:
Emii Tatsuo 2020-11-29 23:17:15 -05:00
parent c743056263
commit 34dca8d92d
No known key found for this signature in database
GPG Key ID: 68FAB2E2E6DFC98B
1 changed files with 63 additions and 3 deletions

View File

@ -54,11 +54,30 @@ impl Builder {
}
}
impl ToString for Builder {
/// Render a document to a string
///
/// This produces a text/gemini compliant text document, represented as a string
fn to_string(&self) -> String {
let len: usize = self.nodes.iter().map(Node::estimate_len).sum(); // sum up node lengths
let mut bytes = Vec::with_capacity(len + self.nodes.len()); // add in inter-node newlines
render(&self.nodes, &mut bytes).unwrap(); // Writing to a string shouldn't produce errors
unsafe {
// This is safe because bytes is composed of Strings. We could have this as
// pure safe code by replicating the `render()` method and switching it to use
// a fmt::Write (or even `String::push()`)instead of a io::Write, but this has
// the same effect, with much DRYer code.
String::from_utf8_unchecked(bytes)
}
}
}
/// Render a set of nodes as a document to a writer.
pub fn render(nodes: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
pub fn render(nodes: impl AsRef<[Node]>, out: &mut impl Write) -> io::Result<()> {
use Node::*;
for node in nodes {
for node in nodes.as_ref() {
match node {
Text(body) => {
let special_prefixes = ["=>", "```", "#", "*", ">"];
@ -72,7 +91,7 @@ pub fn render(nodes: Vec<Node>, out: &mut impl Write) -> io::Result<()> {
None => write!(out, "=> {}\n", to)?,
},
Preformatted(body) => write!(out, "```\n{}\n```\n", body)?,
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(level as usize), body)?,
Heading { level, body } => write!(out, "{} {}\n", "#".repeat(*level as usize), body)?,
ListItem(body) => write!(out, "* {}\n", body)?,
Quote(body) => write!(out, "> {}\n", body)?,
};
@ -166,6 +185,47 @@ impl Node {
pub fn blank() -> Node {
Node::Text("".to_string())
}
/// Cheaply estimate the length of this node
///
/// This measures length in bytes, *not characters*. So if the user includes
/// non-ascii characters, a single one of these characters may add several bytes to
/// the length, despite only displaying as one character.
///
/// This does include any newlines, but not any trailing newlines. For example, a
/// preformatted text block containing a single line reading "trans rights! 🏳️‍⚧️"
/// would have a length of 30: 3 backticks, a newline, the text (including 16 bytes
/// for the trans flag), another newline, and another 3 backticks.
///
/// ```
/// # use gemtext::Node;
/// let simple_text = Node::Text(String::from("Henlo worl"));
/// let linky_link = Node::Link { to: "gemini://cetacean.club/maj/".to_string(), name: Some("Maj".to_string()) };
/// let human_rights = Node::Preformatted("trans rights! 🏳️‍⚧️".to_string());;
///
/// assert_eq!(
/// simple_text.estimate_len(),
/// "Henlo worl".as_bytes().len()
/// );
/// assert_eq!(
/// linky_link.estimate_len(),
/// "=> gemini://cetacean.club/maj/ Maj".as_bytes().len()
/// );
/// assert_eq!(
/// human_rights.estimate_len(),
/// "```\ntrans rights! 🏳️‍⚧️\n```".as_bytes().len()
/// );
/// ```
pub fn estimate_len(&self) -> usize {
match self {
Self::Text(text) => text.len(),
Self::Link { to, name } => 3 + to.as_bytes().len() +
name.as_ref().map(|n| n.as_bytes().len() + 1).unwrap_or(0),
Self::Preformatted(text) => text.as_bytes().len() + 8,
Self::Heading { level, body } => *level as usize + 1 + body.as_bytes().len(),
Self::ListItem(item) | Self::Quote(item)=> 2 + item.as_bytes().len(),
}
}
}
pub fn parse(doc: &str) -> Vec<Node> {