diff options
author | Pietro Albini <pietro@pietroalbini.org> | 2018-11-27 14:44:49 +0100 |
---|---|---|
committer | Pietro Albini <pietro@pietroalbini.org> | 2018-11-27 14:44:49 +0100 |
commit | 56329833966d8d6067398ed4e6e47cad4b221e05 (patch) | |
tree | 252358143f1c32de7d3b90d19bf517a05b2b850a /src | |
parent | d615eeff504451866d2c900d0464cd6efd0e0651 (diff) |
add discord name support
fixes #1
Diffstat (limited to 'src')
-rw-r--r-- | src/schema.rs | 5 | ||||
-rw-r--r-- | src/validate.rs | 18 |
2 files changed, 22 insertions, 1 deletions
diff --git a/src/schema.rs b/src/schema.rs index 641e282..c8579e1 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -9,6 +9,7 @@ pub(crate) struct Person { github: String, irc: Option<String>, email: Option<String>, + discord: Option<String>, } impl Person { @@ -33,6 +34,10 @@ impl Person { pub(crate) fn email(&self) -> Option<&str> { self.email.as_ref().map(|e| e.as_str()) } + + pub(crate) fn discord(&self) -> Option<&str> { + self.discord.as_ref().map(|e| e.as_str()) + } } #[derive(serde_derive::Deserialize, Debug)] diff --git a/src/validate.rs b/src/validate.rs index 0ff43fe..c1680ce 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -1,6 +1,7 @@ use crate::data::Data; -use std::collections::HashSet; use failure::{Error, bail}; +use regex::Regex; +use std::collections::HashSet; pub(crate) fn validate(data: &Data) -> Result<(), Error> { let mut errors = Vec::new(); @@ -11,6 +12,7 @@ pub(crate) fn validate(data: &Data) -> Result<(), Error> { validate_list_email_addresses(data, &mut errors); validate_list_extra_people(data, &mut errors); validate_list_extra_teams(data, &mut errors); + validate_discord_name(data, &mut errors); if !errors.is_empty() { errors.sort(); @@ -117,6 +119,20 @@ fn validate_list_extra_teams(data: &Data, errors: &mut Vec<String>) { }); } +/// Ensure the Discord name is formatted properly +fn validate_discord_name(data: &Data, errors: &mut Vec<String>) { + // https://discordapp.com/developers/docs/resources/user#usernames-and-nicknames + let name_re = Regex::new(r"^[^@#:`]{2,32}#[0-9]{4}$").unwrap(); + wrapper(data.people(), errors, |person, _| { + if let Some(name) = person.discord() { + if !name_re.is_match(name) { + bail!("user `{}` has an invalid discord name: {}", person.github(), name); + } + } + Ok(()) + }) +} + fn wrapper<T, I, F>(iter: I, errors: &mut Vec<String>, mut func: F) where I: Iterator<Item = T>, |