Skip to main content

sigid_core/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! # SigID Core
4//!
5//! Zero-dependency, no_std core for generating 26-character identifiers
6//! in Crockford Base32 format.
7//!
8//! # Example
9//! ```
10//! use sigid_core::{Generator, SigId26};
11//!
12//! let mut gen = Generator::new(0x123456789abcdef);
13//! let id = gen.generate(1234567890).unwrap();
14//! println!("{}", id);
15//! ```
16
17#![no_std]
18#![warn(missing_docs, missing_debug_implementations)]
19
20#[cfg(feature = "std")]
21extern crate std;
22
23extern crate alloc;
24
25mod checksum;
26mod consts;
27mod decoder;
28mod encoder;
29mod generator;
30mod id;
31
32#[cfg(feature = "serde")]
33mod serde;
34
35#[cfg(feature = "uuid")]
36mod uuid_conv;
37
38pub use checksum::{crc16, iso7064_checksum};
39pub use consts::*;
40pub use decoder::decode_crockford32;
41pub use encoder::encode_crockford32;
42pub use generator::Generator;
43pub use id::SigId26;
44
45/// Result type for no_std compatibility
46pub type Result<T> = core::result::Result<T, Error>;
47
48/// Error types
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum Error {
51    /// ID has invalid length (must be 26 chars)
52    InvalidLength,
53    /// ID contains invalid character
54    InvalidCharacter,
55    /// Checksum verification failed
56    ChecksumMismatch,
57    /// Timestamp overflow (max 2^48)
58    TimestampOverflow,
59    /// Counter overflow (max 16383)
60    CounterOverflow,
61}
62
63impl core::fmt::Display for Error {
64    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
65        match self {
66            Self::InvalidLength => write!(f, "Invalid ID length (must be 26 chars)"),
67            Self::InvalidCharacter => write!(f, "Invalid character in ID"),
68            Self::ChecksumMismatch => write!(f, "Checksum verification failed"),
69            Self::TimestampOverflow => write!(f, "Timestamp overflow (max 2^48)"),
70            Self::CounterOverflow => write!(f, "Counter overflow (max 16383)"),
71        }
72    }
73}