aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/line_ending.rs
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-10-14 13:37:22 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-10-14 13:37:22 +0200
commit88d1c902f7a1677516a2eabadf792e870e233bc2 (patch)
tree3eeff48bd006c6047d2524aa92190487a4b151fd /src/util/line_ending.rs
parent7616c1d843d812b1a66757867f4ad561d349e2e3 (diff)
downloadmarkdown-rs-88d1c902f7a1677516a2eabadf792e870e233bc2.tar.gz
markdown-rs-88d1c902f7a1677516a2eabadf792e870e233bc2.tar.bz2
markdown-rs-88d1c902f7a1677516a2eabadf792e870e233bc2.zip
Remove `must_use` if tarpaulin doesn’t understand them
Also moves some code to separate files, in the hopes that tarpaulin understands them.
Diffstat (limited to '')
-rw-r--r--src/util/line_ending.rs129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/util/line_ending.rs b/src/util/line_ending.rs
new file mode 100644
index 0000000..a018fe8
--- /dev/null
+++ b/src/util/line_ending.rs
@@ -0,0 +1,129 @@
+extern crate alloc;
+
+use alloc::{str::FromStr, string::String};
+
+/// Type of line endings in markdown.
+///
+/// Particularly when working with Windows, you might want to use
+/// `LineEnding::CarriageReturnLineFeed`.
+///
+/// ## Examples
+///
+/// ```
+/// use markdown::LineEnding;
+/// # fn main() {
+///
+/// // Use a CR + LF combination:
+/// let crlf = LineEnding::CarriageReturnLineFeed;
+/// # }
+/// ```
+#[derive(Clone, Debug, Default, Eq, PartialEq)]
+pub enum LineEnding {
+ /// Both a carriage return (`\r`) and a line feed (`\n`).
+ ///
+ /// ## Example
+ ///
+ /// ```markdown
+ /// a␍␊
+ /// b
+ /// ```
+ CarriageReturnLineFeed,
+ /// Sole carriage return (`\r`).
+ ///
+ /// ## Example
+ ///
+ /// ```markdown
+ /// a␍
+ /// b
+ /// ```
+ CarriageReturn,
+ /// Sole line feed (`\n`).
+ ///
+ /// ## Example
+ ///
+ /// ```markdown
+ /// a␊
+ /// b
+ /// ```
+ #[default]
+ LineFeed,
+}
+
+// xxxxxxxxxxxxxxx
+impl LineEnding {
+ /// Turn the line ending into a [str].
+ #[must_use]
+ pub fn as_str(&self) -> &str {
+ match self {
+ LineEnding::CarriageReturnLineFeed => "\r\n",
+ LineEnding::CarriageReturn => "\r",
+ LineEnding::LineFeed => "\n",
+ }
+ }
+}
+
+impl FromStr for LineEnding {
+ type Err = String;
+
+ /// Turn a string into a line ending.
+ ///
+ /// ## Panics
+ ///
+ /// Panics if `code` is not `\r\n`, `\r`, or `\n`.
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ match s {
+ "\r\n" => Ok(LineEnding::CarriageReturnLineFeed),
+ "\r" => Ok(LineEnding::CarriageReturn),
+ "\n" => Ok(LineEnding::LineFeed),
+ _ => Err("Expected CR, LF, or CRLF".into()),
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ extern crate std;
+ use super::*;
+
+ #[test]
+ fn test_line_ending() {
+ assert_eq!(
+ "\r".parse(),
+ Ok(LineEnding::CarriageReturn),
+ "should support turning a string into a carriage return"
+ );
+ assert_eq!(
+ LineEnding::CarriageReturn.as_str(),
+ "\r",
+ "should support turning a carriage return into a string"
+ );
+
+ assert_eq!(
+ "\n".parse(),
+ Ok(LineEnding::LineFeed),
+ "should support turning a string into a line feed"
+ );
+ assert_eq!(
+ LineEnding::LineFeed.as_str(),
+ "\n",
+ "should support turning a line feed into a string"
+ );
+
+ assert_eq!(
+ "\r\n".parse(),
+ Ok(LineEnding::CarriageReturnLineFeed),
+ "should support turning a string into a carriage return + line feed"
+ );
+ assert_eq!(
+ LineEnding::CarriageReturnLineFeed.as_str(),
+ "\r\n",
+ "should support turning a carriage return + line feed into a string"
+ );
+
+ assert_eq!(
+ "aaa".parse::<LineEnding>(),
+ Err("Expected CR, LF, or CRLF".into()),
+ "should error when parsing a non-eol"
+ );
+ }
+}