1 unstable release
new 0.1.0 | Apr 8, 2025 |
---|
#560 in Database interfaces
44KB
323 lines
A strict, deterministic, and time-traveling Snowflake ID generator for Rust — supports timewalk generation from SystemTime, DateTime, epoch milliseconds, or RFC3339 strings.
🧩 Installation
Add the following to your Cargo.toml
file:
[dependencies]
tiltflake = {version = "*", features = ["serde"]}
📖 Usage
Generate a Snowflake ID with a UNIX epoch
use tiltflake::flake::Tiltflake;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let generator = Tiltflake::builder().build(); // Default machine_id is 1 and epoch is Unix
let id = generator.generate_from_rfc3339("2025-04-08T12:00:00Z", 0)?;
println!("Snowflake ID: {}", id);
let (dt, machine_id, seq) = generator.parse(id);
println!(
"Parsed: {}, machine_id={}, sequence={}",
dt, machine_id, seq
);
Ok(())
}
Generate a Snowflake ID with a custom epoch
use chrono::{TimeZone, Utc};
use tiltflake::{epoch::EpochType, flake::Tiltflake};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let epoch = EpochType::Custom(
Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).single().unwrap(),
);
let generator = Tiltflake::builder()
.with_epoch(epoch)
.with_machine_id(1)
.build();
let id = generator.generate_from_rfc3339("2025-04-08T12:00:00Z", 0)?;
println!("Snowflake ID: {}", id);
let (dt, machine_id, seq) = generator.parse(id);
println!(
"Parsed: {}, machine_id={}, sequence={}",
dt, machine_id, seq
);
Ok(())
}
Generate a Snowflake ID with a custom Discord epoch
use tiltflake::{epoch::EpochType, flake::Tiltflake};
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
let generator = Tiltflake::builder()
.with_machine_id(1)
.with_epoch(EpochType::Discord)
.build();
let id = generator.generate_from_rfc3339("2025-04-08T12:00:00Z", 0)?;
println!("Snowflake ID: {}", id);
let (dt, machine_id, seq) = generator.parse(id);
println!(
"Parsed: {}, machine_id={}, sequence={}",
dt, machine_id, seq
);
Ok(())
}
See all examples in the examples
folder.
🤝 Contributing
Contributions are what make the open source community an amazing place to learn, be inspired, and create. Any contributions you make are greatly appreciated.
- Fork the repository
- Clone your fork
git clone https://github.com/t1ltxz-gxd/tiltflake.git
- Create your feature branch
git checkout -b feat-smth-amazing
- Stage changes
git add .
- Commit your changes
git commit -m 'feat: add some amazing feature'
- Use Conventional Commits for commit messages.
- Use
fix
,feat
,docs
,style
,refactor
,perf
,test
,chore
prefixes. - Use the present tense ("add feature" not "added feature").
- Use the imperative mood ("move cursor to..." not "moves cursor to...").
- Limit the first line to 72 characters or less.
- Reference issues and pull requests liberally after the first line.
- Push to the branch
git push origin feat-smth-amazing
- Submit a pull request
❤️ Credits
Released with ❤️ by Tilt.
Dependencies
~1.2–2MB
~35K SLoC