postgres_conn_str/parser/authority/
mod.rs

1pub(crate) mod host;
2#[cfg(test)]
3mod tests;
4pub(crate) mod userinfo;
5
6use super::Res;
7use host::{hostspecs, HostSpec};
8use nom::{
9    bytes::complete::tag,
10    combinator::{fail, map, opt},
11    sequence::{terminated, tuple},
12};
13use userinfo::{userinfo, UserSpec};
14
15#[derive(Debug, Default, PartialEq, Eq)]
16pub(crate) struct Authority {
17    pub userspec: Option<UserSpec>,
18    pub hostspec: Vec<HostSpec>,
19}
20
21/// Parse the `authority` from the connection string. Unlike a standard URI,
22/// this will accept a comma-separated list of host:port pairs.
23pub(crate) fn authority(i: &str) -> Res<&str, Authority> {
24    // We don't want to return an empty Vec of host:port pairs if there aren't any,
25    // so we'll match on the result to replace an empty vec with None.
26    //
27    // TODO: Do this better directly in nom.
28    match map(
29        tuple((opt(terminated(userinfo, tag("@"))), hostspecs)),
30        |(userspec, hostspec)| Authority { userspec, hostspec },
31    )(i)
32    {
33        Ok((rem, auth)) if auth.userspec.is_none() && auth.hostspec.is_empty() => fail(rem),
34        u => u,
35    }
36}