melib/
lib.rs

1/*
2 * meli - lib.rs
3 *
4 * Copyright 2017 Manos Pitsidianakis
5 *
6 * This file is part of meli.
7 *
8 * meli is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * meli is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with meli. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#![deny(
23    rustdoc::redundant_explicit_links,
24    unsafe_op_in_unsafe_fn,
25    /* groups */
26    clippy::correctness,
27    clippy::suspicious,
28    clippy::complexity,
29    clippy::perf,
30    clippy::cargo,
31    clippy::nursery,
32    clippy::style,
33    /* restriction */
34    clippy::dbg_macro,
35    clippy::rc_buffer,
36    clippy::as_underscore,
37    clippy::assertions_on_result_states,
38    /* rustdoc */
39    rustdoc::broken_intra_doc_links,
40    /* pedantic */
41    //clippy::cast_lossless,
42    //clippy::cast_possible_wrap,
43    //clippy::ptr_as_ptr,
44    //clippy::bool_to_int_with_if,
45    clippy::doc_markdown,
46    clippy::expect_fun_call,
47    clippy::or_fun_call,
48    clippy::borrow_as_ptr,
49    clippy::case_sensitive_file_extension_comparisons,
50    //clippy::cast_lossless,
51    //clippy::cast_ptr_alignment,
52    clippy::large_futures,
53    clippy::waker_clone_wake,
54    clippy::unused_enumerate_index,
55    clippy::unnecessary_fallible_conversions,
56    clippy::struct_field_names,
57    clippy::manual_hash_one,
58    clippy::into_iter_without_iter,
59)]
60#![allow(
61    clippy::option_if_let_else,
62    clippy::missing_const_for_fn,
63    clippy::significant_drop_tightening,
64    clippy::multiple_crate_versions,
65    clippy::significant_drop_in_scrutinee,
66    clippy::cognitive_complexity
67)]
68/* Source Code Annotation Tags:
69 *
70 * Global tags (in tagref format <https://github.com/stepchowfun/tagref>) for source code
71 * annotation:
72 *
73 * - [tag:needs_unit_test]
74 * - [tag:needs_user_doc]
75 * - [tag:needs_dev_doc]
76 * - [tag:FIXME]
77 * - [tag:TODO]
78 * - [tag:VERIFY] Verify whether this is the correct way to do something
79 * - [tag:DEBT] Technical debt
80 */
81#![doc = include_str!("../README.md")]
82//!
83//! ## Description
84//!
85//! A crate that performs mail client operations such as
86//! - Hold an [`Envelope`] with methods convenient for mail client use. (see
87//!   module [`email`])
88//! - Abstract through mail storages through the [`MailBackend`] trait, and
89//!   handle read/writes/updates through it. (see module [`backends`])
90//! - Decode attachments (see module [`email::attachments`])
91//! - Create new mail (see [`email::Draft`](email::compose::Draft))
92//! - Send mail with an SMTP client (see module [`smtp`])
93//! - Manage contacts (see module [`contacts`])
94//! - Build thread structures out of a list of mail via their `In-Reply-To` and
95//!   `References` header values (see module [`thread`])
96//!
97//! Other exports are
98//! - Basic mail account configuration to use with [`backends`] (see module
99//!   [`conf`])
100//! - A `debug` macro that works like `std::dbg` but for multiple threads. (see
101//!   [`debug` macro](debug!))
102
103#[macro_use]
104pub mod dbg {
105
106    #[allow(clippy::redundant_closure)]
107    #[macro_export]
108    macro_rules! debug {
109        ($val:literal) => {
110            {
111                if cfg!(feature="debug-tracing") {
112                    $crate::log::debug!($val);
113                }
114                $val
115            }
116        };
117        ($val:expr) => {
118            if cfg!(feature="debug-tracing") {
119                let stringify = stringify!($val);
120                // Use of `match` here is intentional because it affects the lifetimes
121                // of temporaries - https://stackoverflow.com/a/48732525/1063961
122                match $val {
123                    tmp => {
124                        $crate::log::debug!("{} = {:?}", stringify, tmp);
125                        tmp
126                    }
127                }
128            } else {
129                $val
130            }
131        };
132        ($fmt:literal, $($arg:tt)*) => {
133            if cfg!(feature="debug-tracing") {
134                $crate::log::debug!($fmt, $($arg)*);
135            }
136        };
137    }
138}
139
140pub mod text;
141
142pub use utils::{
143    datetime::UnixTimestamp,
144    logging::{LogLevel, StderrLogger},
145    SortField, SortOrder,
146};
147
148pub mod contacts;
149pub use contacts::*;
150pub mod backends;
151pub use backends::*;
152mod collection;
153pub mod sieve;
154pub use collection::*;
155pub mod conf;
156pub use conf::*;
157pub mod email;
158pub use email::*;
159pub mod error;
160pub use error::*;
161pub mod thread;
162pub use thread::*;
163pub mod search;
164
165#[macro_use]
166pub mod utils;
167
168#[cfg(feature = "gpgme")]
169pub mod gpgme;
170#[cfg(feature = "imap")]
171pub mod imap;
172#[cfg(feature = "jmap")]
173pub mod jmap;
174#[cfg(feature = "maildir")]
175pub mod maildir;
176pub mod mbox;
177#[cfg(feature = "nntp")]
178pub mod nntp;
179#[cfg(feature = "notmuch")]
180pub mod notmuch;
181#[cfg(feature = "smtp")]
182pub mod smtp;
183
184#[macro_use]
185extern crate serde_derive;
186pub extern crate log;
187/* parser */
188extern crate data_encoding;
189pub extern crate nom;
190
191#[macro_use]
192extern crate bitflags;
193pub extern crate futures;
194#[allow(unused_imports)]
195#[macro_use]
196pub extern crate indexmap;
197#[cfg(feature = "sqlite3")]
198pub extern crate rusqlite;
199pub extern crate serde_path_to_error;
200pub extern crate smallvec;
201pub extern crate smol;
202pub extern crate uuid;
203
204#[derive(Clone, Copy, Debug)]
205#[repr(transparent)]
206pub struct BytesDisplay(pub usize);
207
208impl BytesDisplay {
209    pub const KILOBYTE: f64 = 1024.0;
210    pub const MEGABYTE: f64 = Self::KILOBYTE * 1024.0;
211    pub const GIGABYTE: f64 = Self::MEGABYTE * 1024.0;
212    pub const PETABYTE: f64 = Self::GIGABYTE * 1024.0;
213}
214
215impl std::fmt::Display for BytesDisplay {
216    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
217        let bytes: f64 = self.0 as f64;
218        if bytes == 0.0 {
219            write!(fmt, "0")
220        } else if bytes < Self::KILOBYTE {
221            write!(fmt, "{:.2} bytes", bytes)
222        } else if bytes < Self::MEGABYTE {
223            write!(fmt, "{:.2} KiB", bytes / Self::KILOBYTE)
224        } else if bytes < Self::GIGABYTE {
225            write!(fmt, "{:.2} MiB", bytes / Self::MEGABYTE)
226        } else if bytes < Self::PETABYTE {
227            write!(fmt, "{:.2} GiB", bytes / Self::GIGABYTE)
228        } else {
229            write!(fmt, "{:.2} PiB", bytes / Self::PETABYTE)
230        }
231    }
232}
233
234pub use utils::shellexpand::ShellExpandTrait;