Derive and use Display trait for Viewtype

This commit is contained in:
Dmitry Bogatov
2019-07-30 08:52:43 +00:00
parent c04c8ff103
commit 3358d09148
3 changed files with 15 additions and 22 deletions

View File

@@ -3,7 +3,6 @@
use num_traits::{FromPrimitive, ToPrimitive};
use rusqlite as sql;
use rusqlite::types::*;
use std::fmt;
pub const DC_VERSION_STR: &'static [u8; 14] = b"1.0.0-alpha.3\x00";
@@ -147,7 +146,7 @@ pub const DC_LP_IMAP_SOCKET_FLAGS: usize =
pub const DC_LP_SMTP_SOCKET_FLAGS: usize =
(DC_LP_SMTP_SOCKET_STARTTLS | DC_LP_SMTP_SOCKET_SSL | DC_LP_SMTP_SOCKET_PLAIN);
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)]
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, FromPrimitive, ToPrimitive)]
#[repr(i32)]
pub enum Viewtype {
Unknown = 0,
@@ -192,6 +191,16 @@ pub enum Viewtype {
File = 60,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn derive_display_works_as_expected() {
assert_eq!(format!("{}", Viewtype::Audio), "Audio");
}
}
impl ToSql for Viewtype {
fn to_sql(&self) -> sql::Result<ToSqlOutput> {
let num: i64 = self
@@ -209,17 +218,6 @@ impl FromSql for Viewtype {
}
}
impl fmt::Display for Viewtype {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
self.to_i64()
.expect("impossible: Viewtype -> i64 conversion failed")
)
}
}
// These constants are used as events
// reported to the callback given to dc_context_new().
// If you do not want to handle an event, it is always safe to return 0,

View File

@@ -179,15 +179,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: u32) -> *mut libc::c_ch
if (*msg).type_0 != Viewtype::Text {
ret += "Type: ";
match (*msg).type_0 {
Viewtype::Audio => ret += "Audio",
Viewtype::File => ret += "File",
Viewtype::Gif => ret += "GIF",
Viewtype::Image => ret += "Image",
Viewtype::Video => ret += "Video",
Viewtype::Voice => ret += "Voice",
_ => ret += &format!("{}", (*msg).type_0),
}
ret += &format!("{}", (*msg).type_0);
ret += "\n";
p = dc_msg_get_filemime(msg);
ret += &format!("Mimetype: {}\n", as_str(p));

View File

@@ -8,6 +8,9 @@ extern crate num_derive;
extern crate smallvec;
#[macro_use]
extern crate rusqlite;
extern crate strum;
#[macro_use]
extern crate strum_macros;
#[macro_use]
mod log;