ssb_multiformats/
lib.rs

1//! Implementations of the [ssb multiformats](https://spec.scuttlebutt.nz/feed/datatypes.html).
2// #![warn(missing_docs)]
3
4extern crate base64;
5extern crate serde;
6
7#[cfg(test)]
8#[macro_use]
9extern crate matches;
10
11pub mod multibox;
12pub mod multifeed;
13pub mod multihash;
14pub mod multikey;
15
16///////////////////////////////////////////////////////////////////////////////
17// A bunch of helper functions used throughout the crate for parsing legacy encodings.
18////////////////////////////////////////////////////////////////////////////////
19
20// Split the input slice at the first occurence of the given byte, the byte itself is not
21// part of any of the returned slices. Return `None` if the byte is not found in the input.
22pub(crate) fn split_at_byte(input: &[u8], byte: u8) -> Option<(&[u8], &[u8])> {
23    for i in 0..input.len() {
24        if unsafe { *input.get_unchecked(i) } == byte {
25            let (start, end) = input.split_at(i);
26            return Some((start, &end[1..]));
27        }
28    }
29
30    None
31}
32
33// If the slice begins with the given prefix, return everything after that prefix.
34pub(crate) fn skip_prefix<'a>(input: &'a [u8], prefix: &[u8]) -> Option<&'a [u8]> {
35    if input.starts_with(prefix) {
36        Some(&input[prefix.len()..])
37    } else {
38        None
39    }
40}