refactor: combine all append_* + introduce AppendOptions
This commit is contained in:
parent
3386c26711
commit
469d338d5d
2 changed files with 45 additions and 34 deletions
|
|
@ -84,6 +84,15 @@ pub struct Connection<T: Read + Write> {
|
||||||
pub greeting_read: bool,
|
pub greeting_read: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A set of options for the append command
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct AppendOptions<'a> {
|
||||||
|
/// Optional list of flags
|
||||||
|
pub flags: Option<&'a [Flag<'a>]>,
|
||||||
|
/// Optional internal date
|
||||||
|
pub date: Option<DateTime<FixedOffset>>,
|
||||||
|
}
|
||||||
|
|
||||||
// `Deref` instances are so we can make use of the same underlying primitives in `Client` and
|
// `Deref` instances are so we can make use of the same underlying primitives in `Client` and
|
||||||
// `Session`
|
// `Session`
|
||||||
impl<T: Read + Write> Deref for Client<T> {
|
impl<T: Read + Write> Deref for Client<T> {
|
||||||
|
|
@ -1078,10 +1087,9 @@ impl<T: Read + Write> Session<T> {
|
||||||
/// Specifically, the server will generally notify the client immediately via an untagged
|
/// Specifically, the server will generally notify the client immediately via an untagged
|
||||||
/// `EXISTS` response. If the server does not do so, the client MAY issue a `NOOP` command (or
|
/// `EXISTS` response. If the server does not do so, the client MAY issue a `NOOP` command (or
|
||||||
/// failing that, a `CHECK` command) after one or more `APPEND` commands.
|
/// failing that, a `CHECK` command) after one or more `APPEND` commands.
|
||||||
pub fn append<S: AsRef<str>, B: AsRef<[u8]>>(&mut self, mailbox: S, content: B) -> Result<()> {
|
///
|
||||||
self.append_with_flags(mailbox, content, &[])
|
/// -- TODO merge docs possibly move to AppendOptions
|
||||||
}
|
///
|
||||||
|
|
||||||
/// The [`APPEND` command](https://tools.ietf.org/html/rfc3501#section-6.3.11) can take
|
/// The [`APPEND` command](https://tools.ietf.org/html/rfc3501#section-6.3.11) can take
|
||||||
/// an optional FLAGS parameter to set the flags on the new message.
|
/// an optional FLAGS parameter to set the flags on the new message.
|
||||||
///
|
///
|
||||||
|
|
@ -1092,48 +1100,36 @@ impl<T: Read + Write> Session<T> {
|
||||||
///
|
///
|
||||||
/// The [`\Recent` flag](https://tools.ietf.org/html/rfc3501#section-2.3.2) is not
|
/// The [`\Recent` flag](https://tools.ietf.org/html/rfc3501#section-2.3.2) is not
|
||||||
/// allowed as an argument to `APPEND` and will be filtered out if present in `flags`.
|
/// allowed as an argument to `APPEND` and will be filtered out if present in `flags`.
|
||||||
pub fn append_with_flags<S: AsRef<str>, B: AsRef<[u8]>>(
|
|
||||||
&mut self,
|
|
||||||
mailbox: S,
|
|
||||||
content: B,
|
|
||||||
flags: &[Flag<'_>],
|
|
||||||
) -> Result<()> {
|
|
||||||
self.append_with_flags_and_date(mailbox, content, flags, None)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The [`APPEND` command](https://tools.ietf.org/html/rfc3501#section-6.3.11) can take
|
|
||||||
/// an optional FLAGS parameter to set the flags on the new message.
|
|
||||||
///
|
///
|
||||||
/// > If a flag parenthesized list is specified, the flags SHOULD be set
|
/// -- TODO merge docs possibly move to AppendOptions
|
||||||
/// > in the resulting message; otherwise, the flag list of the
|
|
||||||
/// > resulting message is set to empty by default. In either case, the
|
|
||||||
/// > Recent flag is also set.
|
|
||||||
///
|
|
||||||
/// The [`\Recent` flag](https://tools.ietf.org/html/rfc3501#section-2.3.2) is not
|
|
||||||
/// allowed as an argument to `APPEND` and will be filtered out if present in `flags`.
|
|
||||||
///
|
///
|
||||||
/// Pass a date in order to set the date that the message was originally sent.
|
/// Pass a date in order to set the date that the message was originally sent.
|
||||||
///
|
///
|
||||||
/// > If a date-time is specified, the internal date SHOULD be set in
|
/// > If a date-time is specified, the internal date SHOULD be set in
|
||||||
/// > the resulting message; otherwise, the internal date of the
|
/// > the resulting message; otherwise, the internal date of the
|
||||||
/// > resulting message is set to the current date and time by default.
|
/// > resulting message is set to the current date and time by default.
|
||||||
pub fn append_with_flags_and_date<S: AsRef<str>, B: AsRef<[u8]>>(
|
pub fn append<'a, S: AsRef<str>, B: AsRef<[u8]>>(
|
||||||
&mut self,
|
&mut self,
|
||||||
mailbox: S,
|
mailbox: S,
|
||||||
content: B,
|
content: B,
|
||||||
flags: &[Flag<'_>],
|
options: impl Into<Option<AppendOptions<'a>>>,
|
||||||
date: impl Into<Option<DateTime<FixedOffset>>>,
|
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let content = content.as_ref();
|
let content = content.as_ref();
|
||||||
let flagstr = flags
|
let options_ = options.into().unwrap_or(AppendOptions::default());
|
||||||
|
|
||||||
|
let flagstr = options_
|
||||||
|
.flags
|
||||||
|
.unwrap_or(&[])
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|f| **f != Flag::Recent)
|
.filter(|f| **f != Flag::Recent)
|
||||||
.map(|f| f.to_string())
|
.map(|f| f.to_string())
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(" ");
|
.join(" ");
|
||||||
let datestr = match date.into() {
|
|
||||||
Some(date) => format!(" \"{}\"", date.format("%d-%h-%Y %T %z")),
|
let datestr = if let Some(date) = options_.date {
|
||||||
None => "".to_string(),
|
format!(" \"{}\"", date.format("%d-%h-%Y %T %z"))
|
||||||
|
} else {
|
||||||
|
"".to_string()
|
||||||
};
|
};
|
||||||
|
|
||||||
self.run_command(&format!(
|
self.run_command(&format!(
|
||||||
|
|
|
||||||
|
|
@ -253,7 +253,8 @@ fn append() {
|
||||||
let mbox = "INBOX";
|
let mbox = "INBOX";
|
||||||
c.select(mbox).unwrap();
|
c.select(mbox).unwrap();
|
||||||
//append
|
//append
|
||||||
c.append(mbox, e.message_to_string().unwrap()).unwrap();
|
c.append(mbox, e.message_to_string().unwrap(), None)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// now we should see the e-mail!
|
// now we should see the e-mail!
|
||||||
let inbox = c.uid_search("ALL").unwrap();
|
let inbox = c.uid_search("ALL").unwrap();
|
||||||
|
|
@ -301,7 +302,14 @@ fn append_with_flags() {
|
||||||
c.select(mbox).unwrap();
|
c.select(mbox).unwrap();
|
||||||
//append
|
//append
|
||||||
let flags: &[Flag] = &[Flag::Seen, Flag::Flagged];
|
let flags: &[Flag] = &[Flag::Seen, Flag::Flagged];
|
||||||
c.append_with_flags(mbox, e.message_to_string().unwrap(), flags)
|
c.append(
|
||||||
|
mbox,
|
||||||
|
e.message_to_string().unwrap(),
|
||||||
|
imap::AppendOptions {
|
||||||
|
flags: Some(flags),
|
||||||
|
date: None,
|
||||||
|
},
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// now we should see the e-mail!
|
// now we should see the e-mail!
|
||||||
|
|
@ -358,7 +366,14 @@ fn append_with_flags_and_date() {
|
||||||
let date = FixedOffset::east(8 * 3600)
|
let date = FixedOffset::east(8 * 3600)
|
||||||
.ymd(2020, 12, 13)
|
.ymd(2020, 12, 13)
|
||||||
.and_hms(13, 36, 36);
|
.and_hms(13, 36, 36);
|
||||||
c.append_with_flags_and_date(mbox, e.message_to_string().unwrap(), flags, Some(date))
|
c.append(
|
||||||
|
mbox,
|
||||||
|
e.message_to_string().unwrap(),
|
||||||
|
imap::AppendOptions {
|
||||||
|
flags: Some(flags),
|
||||||
|
date: Some(date),
|
||||||
|
},
|
||||||
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// now we should see the e-mail!
|
// now we should see the e-mail!
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue