pub struct TrackingId(/* private fields */);Expand description
A validated package tracking identifier, e.g. PKG-9ED9285C.
Construct one with TrackingId::generate (default policy),
IdGenerator::generate (custom policy), or TrackingId::parse when
reading an ID back from storage or user input.
With the serde feature an ID serialises as a plain string, and
deserialising runs the same validation as TrackingId::parse — a
TrackingId that arrived over the wire is as trustworthy as one built by
hand.
§Examples
use smart_package_tracker::TrackingId;
let id = TrackingId::generate()?;
assert!(id.as_str().starts_with("PKG-"));
let parsed: TrackingId = "PKG-9ED9285C".parse()?;
assert_eq!(parsed.prefix(), "PKG");
assert_eq!(parsed.body(), "9ED9285C");Implementations§
Source§impl TrackingId
impl TrackingId
Sourcepub fn generate() -> Result<Self>
Available on crate feature os-rng only.
pub fn generate() -> Result<Self>
os-rng only.Generate an ID with the default policy: PKG- plus 32 bits of entropy.
See IdGenerator for why 32 bits is often too narrow, and how to
widen it. Requires the os-rng feature (enabled by default).
§Errors
Returns Error::Entropy if the OS entropy source is unavailable.
Sourcepub fn parse(raw: &str) -> Result<Self>
pub fn parse(raw: &str) -> Result<Self>
Parse and validate an existing ID.
Validation is structural: one separator, a non-empty prefix and body,
only A-Z and 0-9, and a bounded total length. It deliberately does
not check entropy width or check characters, because those are
properties of a generator policy rather than of the format — use
IdGenerator::validate for that.
§Errors
Returns Error::InvalidTrackingId describing the first violation.
Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Consume the ID and return the underlying String.
Trait Implementations§
Source§impl AsRef<str> for TrackingId
impl AsRef<str> for TrackingId
Source§impl Clone for TrackingId
impl Clone for TrackingId
Source§fn clone(&self) -> TrackingId
fn clone(&self) -> TrackingId
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for TrackingId
impl Debug for TrackingId
Source§impl<'de> Deserialize<'de> for TrackingId
Available on crate feature serde only.
impl<'de> Deserialize<'de> for TrackingId
serde only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialise and validate.
Deriving this would accept any string at all, which would let a value
that TrackingId::parse rejects — a lowercase ID, or one carrying a
second separator whose tail is then silently ignored by
body — enter the type through the back door.
Source§impl Display for TrackingId
impl Display for TrackingId
impl Eq for TrackingId
Source§impl FromStr for TrackingId
impl FromStr for TrackingId
Source§impl Hash for TrackingId
impl Hash for TrackingId
Source§impl Ord for TrackingId
impl Ord for TrackingId
Source§fn cmp(&self, other: &TrackingId) -> Ordering
fn cmp(&self, other: &TrackingId) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for TrackingId
impl PartialEq for TrackingId
Source§impl PartialOrd for TrackingId
impl PartialOrd for TrackingId
Source§impl Serialize for TrackingId
Available on crate feature serde only.
impl Serialize for TrackingId
serde only.