libid_identity/
platforms.rs1use std::{
9 error::Error,
10 fmt,
11};
12
13pub struct Platform {
15 pub key: &'static str,
17 pub user_id_tag: &'static str,
19 pub handle_tag: &'static str,
21 pub rules: crate::Rules,
23 pub id_rules: crate::IdRules,
25}
26
27pub const PLATFORM_GOOGLE_KEY: &str = "google";
29pub const PLATFORM_X_KEY: &str = "x";
31pub const PLATFORM_GITHUB_KEY: &str = "github";
33pub const PLATFORM_TELEGRAM_KEY: &str = "telegram";
35
36pub const PLATFORM_GOOGLE: Platform = Platform {
38 key: PLATFORM_GOOGLE_KEY,
39 user_id_tag: "libid.google.user-id",
40 handle_tag: "libid.google.handle",
41 rules: crate::Rules {
42 max_length: 62,
43 },
44 id_rules: crate::IdRules {
45 max_length: 31,
46 decimal: false,
47 },
48};
49pub const PLATFORM_X: Platform = Platform {
51 key: PLATFORM_X_KEY,
52 user_id_tag: "libid.x.user-id",
53 handle_tag: "libid.x.handle",
54 rules: crate::Rules {
55 max_length: 39,
56 },
57 id_rules: crate::IdRules {
58 max_length: 20,
59 decimal: true,
60 },
61};
62pub const PLATFORM_GITHUB: Platform = Platform {
64 key: PLATFORM_GITHUB_KEY,
65 user_id_tag: "libid.github.user-id",
66 handle_tag: "libid.github.handle",
67 rules: crate::Rules {
68 max_length: 39,
69 },
70 id_rules: crate::IdRules {
71 max_length: 20,
72 decimal: true,
73 },
74};
75pub const PLATFORM_TELEGRAM: Platform = Platform {
77 key: PLATFORM_TELEGRAM_KEY,
78 user_id_tag: "libid.telegram.user-id",
79 handle_tag: "libid.telegram.handle",
80 rules: crate::Rules {
81 max_length: 32,
82 },
83 id_rules: crate::IdRules {
84 max_length: 20,
85 decimal: true,
86 },
87};
88
89pub static PLATFORMS: [Platform; 4] = [PLATFORM_GOOGLE, PLATFORM_X, PLATFORM_GITHUB, PLATFORM_TELEGRAM];
91
92pub fn platform(key: &str) -> Option<&'static Platform> {
94 PLATFORMS.iter().find(|p| p.key == key)
95}
96
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99pub enum HandleError {
100 Empty,
102 TooLong,
104 BadCharacter,
106}
107
108impl fmt::Display for HandleError {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 f.write_str(match self {
111 Self::Empty => "the handle is empty",
112 Self::TooLong => "the handle is too long for this platform",
113 Self::BadCharacter => "the handle has a byte outside printable ASCII, or a quote or backslash",
114 })
115 }
116}
117
118impl Error for HandleError {}
119
120#[derive(Debug, Clone, Copy, PartialEq, Eq)]
122pub enum IdError {
123 Empty,
125 TooLong,
127 BadCharacter,
129}
130
131impl fmt::Display for IdError {
132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133 f.write_str(match self {
134 Self::Empty => "the id is empty",
135 Self::TooLong => "the id is too long for this platform",
136 Self::BadCharacter => "the id has a byte this platform does not allow",
137 })
138 }
139}
140
141impl Error for IdError {}