diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2024-07-04 19:36:01 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2024-07-04 19:36:01 +0200 |
| commit | 966a56cb87744651f7716bdae2e590d34ecb03b0 (patch) | |
| tree | 409acbfc18e4fcc3bbe5e7f41e45f6652059f880 /mullvad-cli/src/cmds | |
| parent | 3638342c117109f00b29bd0f43c889f8dd152b9d (diff) | |
| parent | 4aed3375b0e9ed85cc23709e9220b673859ae0b9 (diff) | |
| download | mullvadvpn-966a56cb87744651f7716bdae2e590d34ecb03b0.tar.xz mullvadvpn-966a56cb87744651f7716bdae2e590d34ecb03b0.zip | |
Merge branch 'trim-precedingtrailing-whitespace-in-custom-list-name-and-des-930'
Diffstat (limited to 'mullvad-cli/src/cmds')
| -rw-r--r-- | mullvad-cli/src/cmds/custom_list.rs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/mullvad-cli/src/cmds/custom_list.rs b/mullvad-cli/src/cmds/custom_list.rs index 47e48c0c5f..edc6b4b0e2 100644 --- a/mullvad-cli/src/cmds/custom_list.rs +++ b/mullvad-cli/src/cmds/custom_list.rs @@ -6,11 +6,15 @@ use mullvad_types::{ constraints::Constraint, relay_constraints::GeographicLocationConstraint, relay_list::RelayList, }; +/// Custom list length, expressed as a number of UTF8 codepoints (i.e. chars). +pub const CUSTOM_LIST_MAX_LEN: usize = 30; + #[derive(Subcommand, Debug)] pub enum CustomList { /// Create a new custom list New { /// A name for the new custom list + #[clap(value_parser = parse_custom_list_name)] name: String, }, @@ -55,7 +59,9 @@ pub enum EditCommand { Rename { /// Current name of the custom list name: String, + /// A new name for the custom list + #[clap(value_parser = parse_custom_list_name)] new_name: String, }, } @@ -259,3 +265,16 @@ pub async fn find_list_by_name( .find(|list| list.name == name) .ok_or(anyhow!("List not found")) } + +/// Trim the string and validate the length against [CUSTOM_LIST_MAX_LEN]. +// NOTE: should only be used when *creating* custom lists, as we don't want to make it impossible +// to reference any custom lists created before the max length and whitespace restrictions were put +// in place. +fn parse_custom_list_name(s: &str) -> Result<String> { + let s = s.trim(); + let length = s.chars().count(); + if length > CUSTOM_LIST_MAX_LEN { + bail!("Provided name is too long, {length}/{CUSTOM_LIST_MAX_LEN} characters."); + } + Ok(s.to_string()) +} |
