diem_types/account_config/constants/
diem.rs

1// Copyright (c) The Diem Core Contributors
2// SPDX-License-Identifier: Apache-2.0
3
4// Copyright 2021 Conflux Foundation. All rights reserved.
5// Conflux is free software and distributed under GNU General Public License.
6// See http://www.gnu.org/licenses/
7
8use anyhow::{bail, Result};
9use move_core_types::identifier::Identifier;
10
11/// In addition to the constraints for valid Move identifiers, currency codes
12/// should consist entirely of uppercase alphanumeric characters (e.g., no
13/// underscores).
14pub fn allowed_currency_code_string(
15    possible_currency_code_string: &str,
16) -> bool {
17    possible_currency_code_string
18        .chars()
19        .all(|chr| matches!(chr, 'A'..='Z' | '0'..='9'))
20        && Identifier::is_valid(possible_currency_code_string)
21}
22
23pub fn from_currency_code_string(
24    currency_code_string: &str,
25) -> Result<Identifier> {
26    if !allowed_currency_code_string(currency_code_string) {
27        bail!("Invalid currency code '{}'", currency_code_string)
28    }
29    Identifier::new(currency_code_string)
30}