Skip to content

Commit

Permalink
Simplify errors in docstests
Browse files Browse the repository at this point in the history
  • Loading branch information
JSorngard committed Dec 16, 2023
1 parent 4510e6e commit 4e8a5a7
Showing 1 changed file with 16 additions and 48 deletions.
64 changes: 16 additions & 48 deletions common/src/zalgo_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ impl ZalgoString {
///
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// assert_eq!(ZalgoString::new("Zalgo")?, "É̺͇͌͏");
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
/// Can only encode printable ASCII and newlines:
/// ```
Expand All @@ -55,21 +53,17 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Oh boy!")?;
/// assert_eq!(zs.as_str(), "È̯͈͂͏͙́");
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
/// Note that `ZalgoString` implements [`PartialEq`] with common string types,
/// so the comparison in the above example could also be done directly
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// # let zs = ZalgoString::new("Oh boy!")?;
/// assert_eq!(zs, "È̯͈͂͏͙́");
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
#[must_use = "the method returns a reference and does not modify `self`"]
Expand All @@ -86,13 +80,11 @@ impl ZalgoString {
/// Iterate through the encoded [`char`]s:
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("42")?;
/// let mut chars = zs.chars();
/// assert_eq!(chars.next(), Some('E'));
/// assert_eq!(chars.next(), Some('\u{314}'));
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
pub fn chars(&self) -> core::str::Chars<'_> {
Expand All @@ -107,7 +99,6 @@ impl ZalgoString {
/// and may not match with your intuition of what a character is.
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Zalgo")?;
/// let mut ci = zs.char_indices();
/// assert_eq!(ci.next(), Some((0, 'E')));
Expand All @@ -118,8 +109,7 @@ impl ZalgoString {
/// assert_eq!(ci.next_back(), Some((9, '\u{34f}')));
/// // even though the length in bytes is 11
/// assert_eq!(zs.len(), 11);
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
pub fn char_indices(&self) -> core::str::CharIndices<'_> {
Expand All @@ -134,7 +124,6 @@ impl ZalgoString {
///
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Zlgoa")?;
/// let mut decoded_chars = zs.decoded_chars();
/// assert_eq!(decoded_chars.next(), Some('Z'));
Expand All @@ -144,8 +133,7 @@ impl ZalgoString {
/// assert_eq!(decoded_chars.next_back(), Some('o'));
/// assert_eq!(decoded_chars.next(), None);
/// assert_eq!(decoded_chars.next_back(), None);
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
pub fn decoded_chars(&self) -> DecodedChars<'_> {
Expand All @@ -161,11 +149,9 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Zalgo\n He comes!")?;
/// assert_eq!(zs.into_string(), "É̺͇͌͏̨ͯ̀̀̓ͅ͏͍͓́ͅ");
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
#[must_use = "`self` will be dropped if the result is not used"]
Expand All @@ -182,12 +168,10 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let s = "Zalgo";
/// let zs = ZalgoString::new(s)?;
/// assert_eq!(s, zs.into_decoded_string());
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
pub fn into_decoded_string(self) -> String {
Expand All @@ -205,13 +189,11 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Zalgo")?;
/// let bytes = zs.as_bytes();
/// assert_eq!(bytes[0], 69);
/// assert_eq!(&bytes[1..5], &[204, 186, 205, 129]);
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
#[must_use = "the method returns a reference and does not modify `self`"]
Expand All @@ -229,13 +211,11 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Bytes")?;
/// let mut bytes = zs.bytes();
/// assert_eq!(bytes.next(), Some(69));
/// assert_eq!(bytes.nth(5), Some(148));
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
pub fn bytes(&self) -> core::str::Bytes<'_> {
Expand All @@ -250,14 +230,12 @@ impl ZalgoString {
///
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Zalgo")?;
/// let mut decoded_bytes = zs.decoded_bytes();
/// assert_eq!(decoded_bytes.next(), Some(90));
/// assert_eq!(decoded_bytes.next_back(), Some(111));
/// assert_eq!(decoded_bytes.collect::<Vec<u8>>(), vec![97, 108, 103]);
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
pub fn decoded_bytes(&self) -> DecodedBytes<'_> {
Expand All @@ -272,11 +250,9 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Zalgo")?;
/// assert_eq!(zs.into_bytes(), vec![69, 204, 186, 205, 129, 205, 140, 205, 135, 205, 143]);
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
#[must_use = "`self` will be dropped if the result is not used"]
Expand All @@ -293,11 +269,9 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Zalgo")?;
/// assert_eq!(b"Zalgo".to_vec(), zs.into_decoded_bytes());
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
pub fn into_decoded_bytes(self) -> Vec<u8> {
Expand All @@ -320,11 +294,9 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("Z")?;
/// assert_eq!(zs.len(), 3);
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
// Since the length is never empty it makes no sense to have an is_empty function.
// The decoded length can be empty though, so `decoded_is_empty` is provided instead.
Expand Down Expand Up @@ -355,12 +327,10 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let s = "Zalgo, He comes!";
/// let zs = ZalgoString::new(s)?;
/// assert_eq!(s.len(), zs.decoded_len());
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
#[must_use = "the method returns a new value and does not modify `self`"]
Expand All @@ -375,13 +345,11 @@ impl ZalgoString {
/// Basic usage
/// ```
/// # use zalgo_codec_common::{Error, ZalgoString};
/// # fn main() -> Result<(), Error> {
/// let zs = ZalgoString::new("")?;
/// assert!(zs.decoded_is_empty());
/// let zs = ZalgoString::new("Blargh")?;
/// assert!(!zs.decoded_is_empty());
/// # Ok(())
/// # }
/// # Ok::<(), Error>(())
/// ```
#[inline]
#[must_use = "the method returns a new value and does not modify `self`"]
Expand Down

0 comments on commit 4e8a5a7

Please sign in to comment.