use std::{
error::Error,
fmt,
};
pub struct Platform {
pub key: &'static str,
pub user_id_tag: &'static str,
pub handle_tag: &'static str,
pub rules: crate::Rules,
pub id_rules: crate::IdRules,
}
pub const PLATFORM_GOOGLE_KEY: &str = "google";
pub const PLATFORM_X_KEY: &str = "x";
pub const PLATFORM_GITHUB_KEY: &str = "github";
pub const PLATFORM_GOOGLE: Platform = Platform {
key: PLATFORM_GOOGLE_KEY,
user_id_tag: "libid.google.user-id",
handle_tag: "libid.google.handle",
rules: crate::Rules {
max_length: 62,
},
id_rules: crate::IdRules {
max_length: 31,
decimal: false,
},
};
pub const PLATFORM_X: Platform = Platform {
key: PLATFORM_X_KEY,
user_id_tag: "libid.x.user-id",
handle_tag: "libid.x.handle",
rules: crate::Rules {
max_length: 39,
},
id_rules: crate::IdRules {
max_length: 20,
decimal: true,
},
};
pub const PLATFORM_GITHUB: Platform = Platform {
key: PLATFORM_GITHUB_KEY,
user_id_tag: "libid.github.user-id",
handle_tag: "libid.github.handle",
rules: crate::Rules {
max_length: 39,
},
id_rules: crate::IdRules {
max_length: 20,
decimal: true,
},
};
pub static PLATFORMS: [Platform; 3] = [PLATFORM_GOOGLE, PLATFORM_X, PLATFORM_GITHUB];
pub fn platform(key: &str) -> Option<&'static Platform> {
PLATFORMS.iter().find(|p| p.key == key)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HandleError {
Empty,
TooLong,
BadCharacter,
}
impl fmt::Display for HandleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Empty => "the handle is empty",
Self::TooLong => "the handle is too long for this platform",
Self::BadCharacter => "the handle has a byte outside printable ASCII, or a quote or backslash",
})
}
}
impl Error for HandleError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IdError {
Empty,
TooLong,
BadCharacter,
}
impl fmt::Display for IdError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Empty => "the id is empty",
Self::TooLong => "the id is too long for this platform",
Self::BadCharacter => "the id has a byte this platform does not allow",
})
}
}
impl Error for IdError {}