-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add xtask tool to generate event UIDs
According to the iCalendar (IETF) RFC 5545, the UID for each event must be "globally unique". In this context, that means that the identifier must not collide with any other identifier for any other calendar event across all calendars, ever. The way we were suggesting that people generate these, by calling out to `date`, unfortunately falls a bit short of being able to assure this. Let's instead add a small tool runnable with `cargo xtask` to generate sufficiently-long randomly-generated identifiers to make it easy for people to do the right thing here for new events.
- Loading branch information
1 parent
7cec3fa
commit 9d54d3d
Showing
6 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[alias] | ||
xtask = "run --manifest-path ./xtask/Cargo.toml --" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "calendar-xtask" | ||
version = "0.0.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
publish = false | ||
|
||
[dependencies] | ||
getrandom = { version = "0.2.15", default-features = false } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
use_small_heuristics = "Max" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use std::process::ExitCode; | ||
|
||
/// Generate a UID for an RFC 5545 iCalendar event. | ||
/// | ||
/// The iCalender (IETF) RFC 5545 requires, in section 3.8.4.7, that | ||
/// the `UID` be a: | ||
/// | ||
/// > ...persistent, globally unique identifier for the calendar | ||
/// > component. | ||
/// | ||
/// In this context, "globally unique" means that the identifier must | ||
/// not collide with any other identifier for any other calendar event | ||
/// across all calendars, ever. | ||
/// | ||
/// To achieve this, we'll generate a hexidecimal encoding of 160 | ||
/// randomly-generated bits. | ||
fn print_uid() { | ||
const UID_LEN: usize = 20; | ||
let mut xs = [0u8; UID_LEN]; | ||
getrandom::getrandom(&mut xs).unwrap(); | ||
xs.iter().for_each(|x| print!("{x:02x}")); | ||
println!(); | ||
} | ||
|
||
fn print_usage() { | ||
const USAGE: &str = "\ | ||
Usage: cargo xtask [OPTIONS] COMMAND | ||
Commands: | ||
generate-uid, uid Generate an event UID | ||
Options: | ||
-h Print help | ||
"; | ||
println!("{USAGE}"); | ||
} | ||
|
||
fn main() -> ExitCode { | ||
let mut args = std::env::args(); | ||
let _arg0 = args.next().unwrap(); | ||
match args.next().as_deref() { | ||
Some("-h") => print_usage(), | ||
Some("generate-uid" | "uid") => print_uid(), | ||
_ => { | ||
print_usage(); | ||
return ExitCode::FAILURE; | ||
} | ||
} | ||
ExitCode::SUCCESS | ||
} |