Skip to content

Commit fe081ca

Browse files
committed
add own quota types and implement from trait
so that the ones from imap_proto can be converted easily
1 parent 492ee4a commit fe081ca

File tree

4 files changed

+98
-12
lines changed

4 files changed

+98
-12
lines changed

src/client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use async_std::io::{self, Read, Write};
1010
use async_std::net::{TcpStream, ToSocketAddrs};
1111
use async_std::prelude::*;
1212
use extensions::quota::parse_get_quota_root;
13-
use imap_proto::{Quota, QuotaRoot, RequestId, Response};
13+
use imap_proto::{RequestId, Response};
1414

1515
use super::authenticator::Authenticator;
1616
use super::error::{Error, ParseError, Result, ValidateError};
@@ -1247,7 +1247,7 @@ impl<T: Read + Write + Unpin + fmt::Debug + Send> Session<T> {
12471247
}
12481248

12491249
/// The [`GETQUOTA` command](https://tools.ietf.org/html/rfc2087#section-4.2)
1250-
pub async fn get_quota(&mut self, quota_root: &str) -> Result<Quota<'_>> {
1250+
pub async fn get_quota(&mut self, quota_root: &str) -> Result<Quota> {
12511251
let id = self
12521252
.run_command(format!("GETQUOTA {}", quote!(quota_root)))
12531253
.await?;
@@ -1264,7 +1264,7 @@ impl<T: Read + Write + Unpin + fmt::Debug + Send> Session<T> {
12641264
pub async fn get_quota_root(
12651265
&mut self,
12661266
mailbox_name: &str,
1267-
) -> Result<(Vec<QuotaRoot<'_>>, Vec<Quota<'_>>)> {
1267+
) -> Result<(Vec<QuotaRoot>, Vec<Quota>)> {
12681268
let id = self
12691269
.run_command(format!("GETQUOTAROOT {}", quote!(mailbox_name)))
12701270
.await?;

src/extensions/quota.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use async_std::channel;
44
use async_std::io;
55
use async_std::prelude::*;
66
use async_std::stream::Stream;
7-
use imap_proto::{self, Quota, QuotaRoot, RequestId, Response};
7+
use imap_proto::{self, RequestId, Response};
88

99
use crate::types::*;
1010
use crate::{
@@ -13,14 +13,14 @@ use crate::{
1313
};
1414
use crate::{
1515
error::{Error, ParseError},
16-
types::ResponseData,
16+
types::{Quota, QuotaRoot, ResponseData},
1717
};
1818

1919
pub(crate) async fn parse_get_quota<T: Stream<Item = io::Result<ResponseData>> + Unpin>(
2020
stream: &mut T,
2121
unsolicited: channel::Sender<UnsolicitedResponse>,
2222
command_tag: RequestId,
23-
) -> Result<Quota<'_>> {
23+
) -> Result<Quota> {
2424
let mut quota = None;
2525
while let Some(resp) = stream
2626
.take_while(|res| filter_sync(res, &command_tag))
@@ -29,7 +29,7 @@ pub(crate) async fn parse_get_quota<T: Stream<Item = io::Result<ResponseData>> +
2929
{
3030
let resp = resp?;
3131
match resp.parsed() {
32-
Response::Quota(q) => quota = Some(q.clone().into_owned()),
32+
Response::Quota(q) => quota = Some(q.clone().into()),
3333
_ => {
3434
handle_unilateral(resp, unsolicited.clone()).await;
3535
}
@@ -48,9 +48,9 @@ pub(crate) async fn parse_get_quota_root<T: Stream<Item = io::Result<ResponseDat
4848
stream: &mut T,
4949
unsolicited: channel::Sender<UnsolicitedResponse>,
5050
command_tag: RequestId,
51-
) -> Result<(Vec<QuotaRoot<'_>>, Vec<Quota<'_>>)> {
52-
let mut roots: Vec<QuotaRoot<'_>> = Vec::new();
53-
let mut quotas: Vec<Quota<'_>> = Vec::new();
51+
) -> Result<(Vec<QuotaRoot>, Vec<Quota>)> {
52+
let mut roots: Vec<QuotaRoot> = Vec::new();
53+
let mut quotas: Vec<Quota> = Vec::new();
5454

5555
while let Some(resp) = stream
5656
.take_while(|res| filter_sync(res, &command_tag))
@@ -60,10 +60,10 @@ pub(crate) async fn parse_get_quota_root<T: Stream<Item = io::Result<ResponseDat
6060
let resp = resp?;
6161
match resp.parsed() {
6262
Response::QuotaRoot(qr) => {
63-
roots.push(qr.clone().into_owned());
63+
roots.push(qr.clone().into());
6464
}
6565
Response::Quota(q) => {
66-
quotas.push(q.clone().into_owned());
66+
quotas.push(q.clone().into());
6767
}
6868
_ => {
6969
handle_unilateral(resp, unsolicited.clone()).await;

src/types/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ pub(crate) use self::response_data::ResponseData;
213213
mod request;
214214
pub(crate) use self::request::Request;
215215

216+
mod quota;
217+
pub(crate) use self::quota::*;
218+
216219
/// Responses that the server sends that are not related to the current command.
217220
/// [RFC 3501](https://tools.ietf.org/html/rfc3501#section-7) states that clients need to be able
218221
/// to accept any response at any time. These are the ones we've encountered in the wild.

src/types/quota.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use imap_proto::types::Quota as QuotaRef;
2+
use imap_proto::types::QuotaResource as QuotaResourceRef;
3+
use imap_proto::types::QuotaResourceName as QuotaResourceNameRef;
4+
use imap_proto::types::QuotaRoot as QuotaRootRef;
5+
6+
/// https://tools.ietf.org/html/rfc2087#section-3
7+
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
8+
pub enum QuotaResourceName {
9+
/// Sum of messages' RFC822.SIZE, in units of 1024 octets
10+
Storage,
11+
/// Number of messages
12+
Message,
13+
Atom(String),
14+
}
15+
16+
impl<'a> From<QuotaResourceNameRef<'a>> for QuotaResourceName {
17+
fn from(name: QuotaResourceNameRef<'_>) -> Self {
18+
match name {
19+
QuotaResourceNameRef::Message => QuotaResourceName::Message,
20+
QuotaResourceNameRef::Storage => QuotaResourceName::Storage,
21+
QuotaResourceNameRef::Atom(v) => QuotaResourceName::Atom(v.to_string()),
22+
}
23+
}
24+
}
25+
26+
/// 5.1. QUOTA Response (https://tools.ietf.org/html/rfc2087#section-5.1)
27+
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
28+
pub struct QuotaResource {
29+
pub name: QuotaResourceName,
30+
/// current usage of the resource
31+
pub usage: u64,
32+
/// resource limit
33+
pub limit: u64,
34+
}
35+
36+
impl<'a> From<QuotaResourceRef<'a>> for QuotaResource {
37+
fn from(resource: QuotaResourceRef<'_>) -> Self {
38+
Self {
39+
name: resource.name.into(),
40+
usage: resource.usage,
41+
limit: resource.limit,
42+
}
43+
}
44+
}
45+
46+
/// 5.1. QUOTA Response (https://tools.ietf.org/html/rfc2087#section-5.1)
47+
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
48+
pub struct Quota {
49+
/// quota root name
50+
pub root_name: String,
51+
pub resources: Vec<QuotaResource>,
52+
}
53+
54+
impl<'a> From<QuotaRef<'a>> for Quota {
55+
fn from(quota: QuotaRef<'_>) -> Self {
56+
Self {
57+
root_name: quota.root_name.to_string(),
58+
resources: quota.resources.iter().map(|r| r.clone().into()).collect(),
59+
}
60+
}
61+
}
62+
63+
/// 5.2. QUOTAROOT Response (https://tools.ietf.org/html/rfc2087#section-5.2)
64+
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
65+
pub struct QuotaRoot {
66+
/// mailbox name
67+
pub mailbox_name: String,
68+
/// zero or more quota root names
69+
pub quota_root_names: Vec<String>,
70+
}
71+
72+
impl<'a> From<QuotaRootRef<'a>> for QuotaRoot {
73+
fn from(root: QuotaRootRef<'_>) -> Self {
74+
Self {
75+
mailbox_name: root.mailbox_name.to_string(),
76+
quota_root_names: root
77+
.quota_root_names
78+
.iter()
79+
.map(|n| n.to_string())
80+
.collect(),
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)