Svix - A pure Rust and fully tested KSUID implementation

Related tags

Utilities rust ksuid svix
Overview

Svix - Webhooks as a service

Svix-KSUID (Rust)

A pure Rust and fully tested KSUID implementation

GitHub tag Crates.io Build Status Security audit docs.rs License Join our slack

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

What is a 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.

Usage

Add the dependency:

[dependencies]
svix-ksuid = "^0.5.0"
use svix_ksuid::*;

let ksuid = Ksuid::new(None, None);
println!("{}", ksuid.to_string());
// 1srOrx2ZWZBpBUvZwXKQmoEYga2

Higher timestamp accuracy mode

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:

use svix_ksuid::*;

let ksuid = KsuidMs::new(None, None);

And they both implement the same KsuidLike trait.

Examples

Converting Ksuids

use svix_ksuid::*;

let ksuid = Ksuid::new(None, None);

// Base62
println!("{}", ksuid.to_string()); // also: ksuid.to_base62()
// 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]

Create Ksuids

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::from_base62(base62).unwrap();
let ksuid = Ksuid::from_str(base62).unwrap(); // Also implement FromStr

// From bytes
let bytes = [12u8; 20];
let ksuid = Ksuid::from_bytes(bytes.clone());
assert_eq!(&bytes, ksuid.bytes());

Compare and order Ksuids

use svix_ksuid::*;

let ksuid1 = Ksuid::from_seconds(Some(1_555_555_555), None);
let ksuid2 = Ksuid::from_seconds(Some(1_777_777_777), None);

assert!(ksuid1 < ksuid2);
assert!(ksuid1 <= ksuid2);
assert!(ksuid1 == ksuid1);
assert!(ksuid2 > ksuid1);
assert!(ksuid2 >= ksuid1);

License

ksuid source code is available under an MIT License.

All rights reserved to the Svix webhooks service.

You might also like...
The rust implementation of IPLD schemas and some associated functionalities.

Linked Data IPLD schemas and what to do with them. Beacon Directory of user content and metadata. Since this object should not change it can be used a

untyped-arena provides an Arena allocator implementation that is safe and untyped with minimal complexity

untyped-arena untyped-arena provides an Arena allocator implementation that is safe and untyped with minimal complexity Usage let arena = Arena::new()

Implementation of "Complete and Easy Bidirectional Typechecking for Higher-Rank Polymorphism"

Implementation of "Complete and Easy Bidirectional Typechecking for Higher-Rank Polymorphism" See arXiv:1306.6032 This implementation focusses on read

An implementation of the SMP protocol as used in zephyr, mcuboot, mcumgr, and more.

SMP An implementation of the SMP protocol in pure Rust. This repository contains: ./mcumgr-smp: A SMP library implementation to be used in your own pr

Rust implementation of the legacy Master Server Query Protocol

msq-rs Rust library implementation of the legacy Master Server Query Protocol. Documentation crates.io Repository Release Notes Usage Add this to your

🦀 Rust-based implementation of a Snowflake Generator which communicates using gRPC

Clawflake Clawflake is a Rust application which implements Twitter Snowflakes and communicates using gRPC. Snowflake ID numbers are 63 bits integers s

Re-implementation of Panda Doodle in Rust targetting WASM, a mobile game originally written in C++

Description This is the source code of my game Panda Doodle, which can be played at https://pandadoodle.lucamoller.com/ (it's best playable on touch s

2D Predictive-Corrective Smoothed Particle Hydrodynamics (SPH) implementation in Rust with WASM + WebGL

pcisph-wasm 2D Predictive-Corrective Smoothed Particle Hydrodynamics (SPH) implementation in Rust with WASM + WebGL Reimplementation of my previous Ru

A Rust implementation of fractional indexing.
A Rust implementation of fractional indexing.

fractional_index This crate implements fractional indexing, a term coined by Figma in their blog post Realtime Editing of Ordered Sequences. Specifica

Comments
Owner
Svix
Webhooks as a service
Svix
Pure rust implementation of jq

XQ JQ reimplemented purely in Rust. Caution This program is under development. You probably want to use the original implementation of jq, or pure Go

null 181 Jan 4, 2023
A pure Rust PLONK implementation using arkworks as a backend.

PLONK This is a pure Rust implementation of the PLONK zk proving system Usage use ark_plonk::prelude::*; use ark_ec::bls12::Bls12; use rand_core::OsRn

rust-zkp 201 Dec 31, 2022
Pure rust implementation of python's random module with compatible generator behaviour.

pyrand Pure rust implementation of (parts of) python's random module with compatible PRNG behaviour: seeding with equivalent values will yield identic

Stefan V. 4 Feb 10, 2024
Rustymind is a driver and parser for NeuroSky MindWave EEG headset written in pure Rust.

Rustymind is a driver and parser for NeuroSky MindWave EEG headset written in pure Rust. You can use it to connect, interact, and plot real time data from the headset.

Junjun Dong 34 Sep 13, 2022
A program written in pure Rust to query music info from mpd and display it in a notification.

musinfo A program written in pure Rust to query music info from mpd and display it in a notification. Note: Cover art is expected to be placed at /tmp

Cpt.Howdy 10 Aug 16, 2022
Wait Service is a pure rust program to test and wait on the availability of a service.

Wait Service Wait Service is a pure rust program to test and wait on the availability of a service.

Magic Len (Ron Li) 3 Jan 18, 2022
mollusc is a collection of pure-Rust libraries for parsing, interpreting, and analyzing LLVM.

mollusc is a collection of pure-Rust libraries for parsing, interpreting, and analyzing LLVM.

William Woodruff 50 Dec 2, 2022
Program a Raspberry Pi Pico with pure Rust

pi-pico-rs Program a Raspberry Pi Pico with pure Rust. Get Started Install the latest version of Rust and the thumbv6m-none-eabi target. This is the p

Gerald Nash 5 Jul 29, 2022
Pure Rust library for Apache ZooKeeper built on tokio

zookeeper-async Async Zookeeper client written 100% in Rust, based on tokio. This library is intended to be equivalent with the official (low-level) Z

Kamil Rojewski 16 Dec 16, 2022
A bare metal STM32F103C8T6/STM32F103 MCU program written in pure Rust

A bare metal (register level) STM32F103C8T6/STM32F103 MCU program written in pure Rust without any IDE, SDK, HAL or library, and no assembly code, the only tool required is the Rust compiler.

Hema Shushu 105 Dec 18, 2022