Skip to content

Commit

Permalink
Add documentation and fix README example
Browse files Browse the repository at this point in the history
Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
  • Loading branch information
wiktor-k committed Oct 30, 2024
1 parent e82d742 commit 621af80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ pub struct DecodeError;

#### Example

```rs
```rust
use zbase32::{encode, decode};

fn main() {
assert_eq!(encode(b"foo"), "c3zs6".to_string());
assert_eq!(Ok(b"foo"), decode("c3zs6".to_string()));
assert_eq!(decode(&encode(b"foo")).unwrap(), b"foo")
}
assert_eq!(encode("foo"), "c3zs6");
assert_eq!(Ok(b"foo".into()), decode("c3zs6"));
assert_eq!(decode(&encode("foo")).unwrap(), b"foo")
```

### CLI
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]

#[cfg(feature = "python")]
mod python;

Expand All @@ -17,6 +20,7 @@ const INVERSE_ALPHABET: [i8; 123] = [
23,
];

/// Error decoding input bytes.
#[derive(Debug, PartialEq)]
pub struct DecodeError;

Expand All @@ -28,6 +32,15 @@ impl fmt::Display for DecodeError {
}
}

/// Z-base32 encodes `input`.
///
/// # Examples
///
/// ```
/// use zbase32::encode;
///
/// assert_eq!(encode("asdasd"), "cf3seamuco");
/// ```
pub fn encode(input: impl AsRef<[u8]>) -> String {
let input = input.as_ref();
let mut result = Vec::new();
Expand Down Expand Up @@ -58,6 +71,15 @@ pub fn encode(input: impl AsRef<[u8]>) -> String {
unsafe { String::from_utf8_unchecked(result) }
}

/// Decodes z-base-32 encoded data into a vector of bytes.
///
/// # Examples
///
/// ```
/// use zbase32::decode;
///
/// assert_eq!(decode("cf3seamu"), Ok(b"asdas".to_vec()))
/// ```
pub fn decode(input: &str) -> Result<Vec<u8>, DecodeError> {
let mut result = Vec::new();
for chunk in input.as_bytes().chunks(8) {
Expand Down

0 comments on commit 621af80

Please sign in to comment.