Svix - Webhooks as a service
A pure Rust and fully tested KSUID implementation
This library is fully compatible with Segment's KSUID implementation: https://github.com/segmentio/ksuid
For the Python version, please check out https://github.com/svix/python-ksuid
A ksuid is a K sorted UID. In other words, a KSUID also stores a date component, so that ksuids can be approximately sorted based on the time they were created.
Read more here.
Add the dependency:
toml
[dependencies]
svix-ksuid = "^0.6.0"
```rust use svix_ksuid::*;
let ksuid = Ksuid::new(None, None); println!("{}", ksuid.to_string()); // 1srOrx2ZWZBpBUvZwXKQmoEYga2 ```
Ksuids have a 1 second accuracy which is not sufficient for all use-cases. That's why this library exposes a higher accuracy mode which supports accuracy of up to 4ms.
It's fully compatible with normal ksuids, in fact, it outputs valid ksuids. The difference is that it sacrifices one byte of the random payload in favor of this accuracy.
The code too is fully compatible:
```rust use svix_ksuid::*;
let ksuid = KsuidMs::new(None, None); ```
And they both implement the same KsuidLike
trait.
serde
- adds the ability to serialize and deserialize Ksuid
and KsuidMs
using serde.Make sure to enable like this:
toml
[dependencies]
svix-ksuid = { version = "^0.6.0", features = ["serde"] }
```rust use svix_ksuid::*;
let ksuid = Ksuid::new(None, None);
// Base62 println!("{}", ksuid.tostring()); // also: ksuid.tobase62() // 1srOrx2ZWZBpBUvZwXKQmoEYga2
// Bytes (&[u8]) println!("{:?}", ksuid.bytes()); // [13, 53, 196, 51, 225, 147, 62, 55, 242, 117, 112, 135, 99, 173, 199, 116, 90, 245, 231, 242]
// Timestamp (time::OffsetDateTime) println!("{:?}", ksuid.timestamp()); // 2021-05-21T20:04:03Z
// Timestamp (seconds) println!("{}", ksuid.timestamp_seconds()); 1621627443
// Payload (&[u8]) println!("{:?}", ksuid.bytes()); // [225, 147, 62, 55, 242, 117, 112, 135, 99, 173, 199, 116, 90, 245, 231, 242] ```
```rust use svix_ksuid::*;
// Timestamp is now, payload is randomly generated let ksuid = Ksuid::new(None, None);
// Explicitly set either let bytes = [12u8; Ksuid::PAYLOAD_BYTES]; let ksuid = Ksuid::new(Some(Utc::now()), Some(&bytes)); let ksuid = Ksuid::new(None, Some(&bytes)); let ksuid = Ksuid::new(Some(Utc::now()), None);
// From base62 let base62 = "1srOrx2ZWZBpBUvZwXKQmoEYga2"; let ksuid = Ksuid::frombase62(base62).unwrap(); let ksuid = Ksuid::fromstr(base62).unwrap(); // Also implement FromStr
// From bytes let bytes = [12u8; 20]; let ksuid = Ksuid::frombytes(bytes.clone()); asserteq!(&bytes, ksuid.bytes()); ```
```rust use svix_ksuid::*;
let ksuid1 = Ksuid::fromseconds(Some(1555555555), None); let ksuid2 = Ksuid::fromseconds(Some(1777777777), None);
assert!(ksuid1 < ksuid2); assert!(ksuid1 <= ksuid2); assert!(ksuid1 == ksuid1); assert!(ksuid2 > ksuid1); assert!(ksuid2 >= ksuid1); ```
ksuid source code is available under an MIT License.
All rights reserved to the Svix webhooks service.