granular-id

Crates.io Docs.rs License: MIT

This crate provides a data type GranularId<T> that can represent ID numbers with arbitrary precision.

Features

Example usage

```rust // Create a new GranularId from a vec of u8 (id: 1.2.3) let id: GranularId = vec![1, 2, 3].into();

// Get the parent ID (id: 1.2) let parent = id.parent(); assert_eq!(parent, vec![1, 2].into());

// Iterate over the following siblings of 1.2.3 let mut nextsiblings = id.nextsiblings(); // First one is 1.2.4 asserteq!(nextsiblings.next().unwrap(), vec![1, 2, 4].into()); // Then, 1.2.5, etc asserteq!(nextsiblings.next().unwrap(), vec![1, 2, 5].into()); asserteq!(nextsiblings.next().unwrap(), vec![1, 2, 6].into());

// Get an iterator over childrens of 1.2.3 let mut children = id.children(); // First one is 1.2.3.0 asserteq!(children.next().unwrap(), vec![1, 2, 3, 0].into()); // Then, 1.2.3.1, etc asserteq!(children.next().unwrap(), vec![1, 2, 3, 1].into()); assert_eq!(children.next().unwrap(), vec![1, 2, 3, 2].into());

// Each parent is always smaller than all of its children assert!(parent < id); ```

Installation

Add this to your Cargo.toml:

toml [dependencies] granular-id = "0.2.0"

License

This project is licensed under the MIT license. See LICENSE for more details.