address 0.20.0-rc.1

This library aids in processing network addresses.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
use crate::ParseError;

/// Parses the `address` with the `parse` function, lowercasing the address first when it contains uppercase ASCII.
///
/// Borrowed parsers require lowercase input, so this normalizes owned-type parsing without allocating for
/// already-lowercase input.
pub(crate) fn parse_lowercase<T>(
    address: &str,
    parse: impl FnOnce(&str) -> Result<T, ParseError>,
) -> Result<T, ParseError> {
    if address.bytes().any(|b| b.is_ascii_uppercase()) {
        parse(address.to_ascii_lowercase().as_str())
    } else {
        parse(address)
    }
}