Expand description
Utilities to work with usernames and pull information out of it.
§Username Parsing
The parse_username
function is used to parse a username and extract information from
its labels. The function takes a parser, which is used to parse the labels from the username.
The parser is expected to implement the UsernameLabelParser
trait, which has two methods:
parse_label
: This method is called for each label in the username, and is expected to return whether the label was used or ignored.build
: This method is called after all labels have been parsed, and is expected to consume the parser and store any relevant information.
The parser can be a single parser or a tuple of parsers. Tuple parsers all receive all labels,
unless wrapped by a ExclusiveUsernameParsers
, in which case the first parser that consumes
a label will stop the iteration over the parsers.
Parsers are to return UsernameLabelState::Used
in case they consumed the label, and
UsernameLabelState::Ignored
in case they did not. This way the parser-caller (e.g. parse_username
)
can decide whether to fail on ignored labels.
§Example
ProxyFilterUsernameParser
is a real-world example of a parser that uses the username labels.
It support proxy filter definitions directly within the username.
use rama::proxy::{ProxyFilter, ProxyFilterUsernameParser};
use rama::utils::username::{DEFAULT_USERNAME_LABEL_SEPARATOR, parse_username};
use rama::service::context::Extensions;
let mut ext = Extensions::default();
let parser = ProxyFilterUsernameParser::default();
let username = parse_username(
&mut ext, parser,
"john-residential-country-us",
DEFAULT_USERNAME_LABEL_SEPARATOR,
).unwrap();
assert_eq!(username, "john");
let filter = ext.get::<ProxyFilter>().unwrap();
assert_eq!(filter.residential, Some(true));
assert_eq!(filter.country, Some(vec!["us".into()]));
assert!(filter.datacenter.is_none());
assert!(filter.mobile.is_none());
Structs§
- Wrapper type that can be used with a tuple of
UsernameLabelParser
s in order for it to stop iterating over the parsers once there was one that consumed the label. - A
UsernameLabelParser
which does nothing and returnsUsernameLabelState::Used
for all labels. - Opaque string labels parsed collected using the
UsernameOpaqueLabelParser
. - A
UsernameLabelParser
which collects all labels from the username, without any specific parsing logic.
Enums§
- The parse state of a username label.
Constants§
- The default username label separator used by most built-in rama support.
Traits§
- A parser which can parse labels from a username.
Functions§
- Parse a username, extracting the username (first part) and passing everything else to the
UsernameLabelParser
.