substrate_data/schema/refs/
ref_id.rs1use std::borrow::Cow;
4use std::fmt;
5
6use crate::model::value::RefKey;
7use crate::schema::{Expected, Schema};
8
9pub trait RefId: Schema + Clone + PartialEq + fmt::Debug {
13 const EXPECTED: Expected;
15 fn parse(spelled: &str) -> Option<Self>;
17 fn spell(&self) -> Cow<'_, str>;
19}
20
21impl RefId for String {
22 const EXPECTED: Expected = Expected::Text;
23 fn parse(spelled: &str) -> Option<Self> {
24 RefKey::is_valid(spelled).then(|| spelled.to_owned())
25 }
26 fn spell(&self) -> Cow<'_, str> {
27 Cow::Borrowed(self)
28 }
29}
30
31macro_rules! number_ids {
32 ($($t:ty => $what:ident),*) => {$(
33 impl RefId for $t {
34 const EXPECTED: Expected = Expected::$what;
35 fn parse(spelled: &str) -> Option<Self> {
36 let digits = spelled.strip_prefix('-').unwrap_or(spelled);
38 if digits.is_empty() || !digits.bytes().all(|b| b.is_ascii_digit()) {
39 return None;
40 }
41 spelled.parse().ok()
42 }
43 fn spell(&self) -> Cow<'_, str> {
44 Cow::Owned(self.to_string())
45 }
46 }
47 )*};
48}
49number_ids!(u32 => Unsigned, u64 => Unsigned, i64 => Integer);