async_imap/types/
name.rs

1pub use imap_proto::types::NameAttribute;
2use imap_proto::{MailboxDatum, Response};
3use self_cell::self_cell;
4
5use crate::types::ResponseData;
6
7self_cell!(
8    /// A name that matches a `LIST` or `LSUB` command.
9    pub struct Name {
10        owner: Box<ResponseData>,
11
12        #[covariant]
13        dependent: InnerName,
14    }
15
16    impl { Debug }
17);
18
19#[derive(PartialEq, Eq, Debug)]
20pub struct InnerName<'a> {
21    attributes: Vec<NameAttribute<'a>>,
22    delimiter: Option<&'a str>,
23    name: &'a str,
24}
25
26impl Name {
27    pub(crate) fn from_mailbox_data(resp: ResponseData) -> Self {
28        Name::new(Box::new(resp), |response| match response.parsed() {
29            Response::MailboxData(MailboxDatum::List {
30                name_attributes,
31                delimiter,
32                name,
33            }) => InnerName {
34                attributes: name_attributes.to_owned(),
35                delimiter: delimiter.as_deref(),
36                name,
37            },
38            _ => panic!("cannot construct from non mailbox data"),
39        })
40    }
41
42    /// Attributes of this name.
43    pub fn attributes(&self) -> &[NameAttribute<'_>] {
44        &self.borrow_dependent().attributes[..]
45    }
46
47    /// The hierarchy delimiter is a character used to delimit levels of hierarchy in a mailbox
48    /// name.  A client can use it to create child mailboxes, and to search higher or lower levels
49    /// of naming hierarchy.  All children of a top-level hierarchy node use the same
50    /// separator character.  `None` means that no hierarchy exists; the name is a "flat" name.
51    pub fn delimiter(&self) -> Option<&str> {
52        self.borrow_dependent().delimiter
53    }
54
55    /// The name represents an unambiguous left-to-right hierarchy, and are valid for use as a
56    /// reference in `LIST` and `LSUB` commands. Unless [`NameAttribute::NoSelect`] is indicated,
57    /// the name is also valid as an argument for commands, such as `SELECT`, that accept mailbox
58    /// names.
59    pub fn name(&self) -> &str {
60        self.borrow_dependent().name
61    }
62}