Skip to main content

asimov_id/
id.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{Handle, IdError, KeyError, PublicKey};
4use core::str::FromStr;
5
6pub const ID_PREFIX: &str = "Ⓐ";
7
8#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
9pub enum Id {
10    Handle(Handle),
11    PublicKey(PublicKey),
12}
13
14impl FromStr for Id {
15    type Err = IdError;
16
17    fn from_str(input: &str) -> Result<Self, Self::Err> {
18        Ok(PublicKey::from_str(input)
19            .map(|key| Id::PublicKey(key))
20            .or_else(|_| Handle::from_str(input).map(|handle| Id::Handle(handle)))?)
21    }
22}
23
24impl core::fmt::Display for Id {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            Id::Handle(handle) => handle.fmt(f),
28            Id::PublicKey(key) => key.fmt(f),
29        }
30    }
31}