Skip to content

Commit

Permalink
less unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
vhdirk committed Nov 16, 2019
1 parent 084675f commit e16769c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/config_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use ffi;
use Database;
use Filenames;
use FilenamesOwner;
use utils::{ScopedSupercow, ScopedPhantomcow};
use utils::{ToStr, ScopedSupercow, ScopedPhantomcow};


#[derive(Debug)]
Expand Down Expand Up @@ -46,15 +46,15 @@ impl<'d> Iterator for ConfigList<'d>
}

let (k, v) = unsafe {
let key = CStr::from_ptr(ffi::notmuch_config_list_key(self.ptr));
let value = CStr::from_ptr(ffi::notmuch_config_list_value(self.ptr));
let key = ffi::notmuch_config_list_key(self.ptr);
let value = ffi::notmuch_config_list_value(self.ptr);

ffi::notmuch_config_list_move_to_next(self.ptr);

(key, value)
};

Some((k.to_str().unwrap().to_string(), v.to_str().unwrap().to_string()))
Some((k.to_string_lossy().to_string(), v.to_string_lossy().to_string()))
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use libc::{c_char, c_double, c_int, c_uint, c_ulong, c_void, time_t};

use error::{Error, Result};
use std::{error, fmt, str};

use std::borrow::Cow;
use utils::ToStr;

notmuch_enum! {
Expand Down Expand Up @@ -57,6 +57,14 @@ impl ToStr for Status {
fn to_str<'a>(&self) -> std::result::Result<&'a str, str::Utf8Error> {
unsafe { notmuch_status_to_string((*self).into()) }.to_str()
}

fn to_str_unchecked<'a>(&self) -> &'a str {
unsafe { notmuch_status_to_string((*self).into()) }.to_str_unchecked()
}

fn to_string_lossy<'a>(&self) -> Cow<'a, str> {
unsafe { notmuch_status_to_string((*self).into()) }.to_string_lossy()
}
}

impl fmt::Display for Status {
Expand Down
30 changes: 16 additions & 14 deletions src/message.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::ffi::{CString, CStr};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::cell::RefCell;
use std::borrow::Cow;
use std::ptr;

use supercow::{Supercow};

use error::{Error, Result};
Expand Down Expand Up @@ -54,14 +56,14 @@ where
}
}

pub fn id(self: &Self) -> String {
pub fn id(self: &Self) -> Cow<'_, str> {
let mid = unsafe { ffi::notmuch_message_get_message_id(self.ptr) };
mid.to_str().unwrap().to_string()
mid.to_string_lossy()
}

pub fn thread_id(self: &Self) -> String {
pub fn thread_id(self: &Self) -> Cow<'_, str> {
let tid = unsafe { ffi::notmuch_message_get_thread_id(self.ptr) };
tid.to_str().unwrap().to_string()
tid.to_string_lossy()
}

pub fn replies(self: &Self) -> Messages<'o, O> {
Expand Down Expand Up @@ -93,16 +95,18 @@ where
unsafe { ffi::notmuch_message_get_date(self.ptr) as i64 }
}

pub fn header(&self, name: &str) -> Result<Option<&str>> {
pub fn header(&self, name: &str) -> Result<Option<Cow<'_, str>>> {
let name = CString::new(name).unwrap();
let ret = unsafe { ffi::notmuch_message_get_header(self.ptr, name.as_ptr()) };
if ret.is_null() {
Err(Error::UnspecifiedError)
} else {
Ok(match ret.to_str().unwrap() {
"" => None,
ret => Some(ret),
})
let ret_str = ret.to_string_lossy();
if ret_str.is_empty() {
Ok(None)
} else{
Ok(Some(ret_str))
}
}
}

Expand Down Expand Up @@ -196,7 +200,7 @@ where
Ok(cnt)
}

pub fn property(&self, key: &str, exact: bool) -> Result<String>
pub fn property(&self, key: &str) -> Result<Cow<'_, str>>
{
let key_str = CString::new(key).unwrap();
let mut prop = ptr::null();
Expand All @@ -208,9 +212,7 @@ where
Err(Error::UnspecifiedError)
} else {
// TODO: the unwrap here is not good
Ok(unsafe{
CStr::from_ptr(prop)
}.to_str().unwrap().to_string())
Ok(prop.to_string_lossy())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/message_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ where
(key, value)
};

Some((k.to_str().unwrap().to_string(), v.to_str().unwrap().to_string()))
Some((k.to_string_lossy().to_string(), v.to_string_lossy().to_string()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
CStr::from_ptr(t)
};

Some(ctag.to_str().unwrap().to_string())
Some(ctag.to_string_lossy().to_string())
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/thread.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ops::Drop;
use std::borrow::Cow;

use ffi;
use utils::{ToStr, ScopedSupercow, ScopedPhantomcow};
Expand Down Expand Up @@ -43,9 +44,9 @@ where
}
}

pub fn id(self: &Self) -> String {
pub fn id(self: &Self) -> &str {
let tid = unsafe { ffi::notmuch_thread_get_thread_id(self.ptr) };
tid.to_str().unwrap().to_string()
tid.to_str().unwrap()
}

pub fn total_messages(self: &Self) -> i32 {
Expand Down Expand Up @@ -75,18 +76,16 @@ where
<Self as ThreadExt<'d, 'q>>::tags(self)
}

pub fn subject(self: &Self) -> String {
pub fn subject(self: &Self) -> Cow<'_, str> {
let sub = unsafe { ffi::notmuch_thread_get_subject(self.ptr) };

sub.to_str().unwrap().to_string()
sub.to_string_lossy()
}

pub fn authors(self: &Self) -> Vec<String> {
let athrs = unsafe { ffi::notmuch_thread_get_authors(self.ptr) };

athrs
.to_str()
.unwrap()
.to_string_lossy()
.split(',')
.map(|s| s.to_string())
.collect()
Expand Down
14 changes: 13 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
use libc;
use std::{ffi, str};

use std::borrow::Cow;
use supercow::{Supercow, DefaultFeatures/*, NonSyncFeatures*/};
use supercow::ext::{BoxedStorage};

pub trait ToStr {
fn to_str<'a>(&self) -> Result<&'a str, str::Utf8Error>;

fn to_str_unchecked<'a>(&self) -> &'a str;

fn to_string_lossy<'a>(&self) -> Cow<'a, str>;
}

impl ToStr for *const libc::c_char {
fn to_str<'a>(&self) -> Result<&'a str, str::Utf8Error> {
str::from_utf8(unsafe { ffi::CStr::from_ptr(*self) }.to_bytes())
}

fn to_str_unchecked<'a>(&self) -> &'a str {
unsafe { str::from_utf8_unchecked(ffi::CStr::from_ptr(*self).to_bytes()) }
}

fn to_string_lossy<'a>(&self) -> Cow<'a, str> {
unsafe { ffi::CStr::from_ptr(*self) }.to_string_lossy()
}
}

pub trait ToString {
Expand Down
14 changes: 7 additions & 7 deletions tests/test_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mod message {
#[test]
fn test_header() {
let msg = MessageFixture::new();
assert_eq!(msg.message.header(&"from").unwrap(), Some("<src@example.com>"));
assert_eq!(msg.message.header(&"from").unwrap().unwrap().to_string(), "<src@example.com>");
}

#[test]
Expand Down Expand Up @@ -172,10 +172,10 @@ mod properties {
fn test_add_single() {
let msg = MessageFixture::new();
msg.message.add_property(&"foo", &"bar").unwrap();
assert_eq!(msg.message.property(&"foo", true).unwrap(), "bar");
assert_eq!(msg.message.property(&"foo").unwrap(), "bar");

msg.message.add_property(&"bar", &"baz").unwrap();
assert_eq!(msg.message.property(&"bar", true).unwrap(), "baz");
assert_eq!(msg.message.property(&"bar").unwrap(), "baz");
}

#[test]
Expand All @@ -184,7 +184,7 @@ mod properties {
msg.message.add_property(&"foo", &"bar").unwrap();
msg.message.add_property(&"foo", &"baz").unwrap();

assert_eq!(msg.message.property(&"foo", true).unwrap(), "bar");
assert_eq!(msg.message.property(&"foo").unwrap(), "bar");

let props = msg.message.properties(&"foo", true);
let expect = vec![("foo", "bar"), ("foo", "baz")];
Expand Down Expand Up @@ -222,7 +222,7 @@ mod properties {
msg.message.add_property(&"foo", &"b").unwrap();

msg.message.remove_all_properties(Some(&"foo")).unwrap();
assert!(msg.message.property(&"foo", true).is_err());
assert!(msg.message.property(&"foo").is_err());
}

#[test]
Expand All @@ -232,7 +232,7 @@ mod properties {
msg.message.add_property(&"foo", &"b").unwrap();

msg.message.remove_property(&"foo", &"a").unwrap();
assert_eq!(msg.message.property(&"foo", true).unwrap(), "b");
assert_eq!(msg.message.property(&"foo").unwrap(), "b");
}

#[test]
Expand All @@ -241,7 +241,7 @@ mod properties {
msg.message.add_property(&"foo", &"a").unwrap();

msg.message.remove_all_properties(None).unwrap();
assert!(msg.message.property(&"foo", true).is_err());
assert!(msg.message.property(&"foo").is_err());
}

#[test]
Expand Down

0 comments on commit e16769c

Please sign in to comment.