Better error handling, remove all unwraps
This commit is contained in:
parent
55bd19bb0d
commit
b32010f905
4 changed files with 141 additions and 14 deletions
16
src/io.rs
16
src/io.rs
|
@ -1,5 +1,9 @@
|
|||
extern crate gpw;
|
||||
extern crate linked_hash_map;
|
||||
extern crate rand;
|
||||
|
||||
use rand::{Rng, thread_rng};
|
||||
use rand::distributions::Alphanumeric;
|
||||
|
||||
use linked_hash_map::LinkedHashMap;
|
||||
|
||||
|
@ -9,7 +13,7 @@ use std::cell::RefCell;
|
|||
|
||||
lazy_static! {
|
||||
static ref ENTRIES: RwLock<LinkedHashMap<String, String>> = RwLock::new(LinkedHashMap::new());
|
||||
static ref BUFFER_SIZE: usize = env::var("BIN_BUFFER_SIZE").map(|f| f.parse::<usize>().unwrap()).unwrap_or(1000usize);
|
||||
static ref BUFFER_SIZE: usize = env::var("BIN_BUFFER_SIZE").map(|f| f.parse::<usize>().expect("Failed to parse value of BIN_BUFFER_SIZE")).unwrap_or(1000usize);
|
||||
}
|
||||
|
||||
/// Ensures `ENTRIES` is less than the size of `BIN_BUFFER_SIZE`. If it isn't then
|
||||
|
@ -17,12 +21,12 @@ lazy_static! {
|
|||
///
|
||||
/// During the purge, `ENTRIES` is locked and the current thread will block.
|
||||
fn purge_old() {
|
||||
let entries_len = ENTRIES.read().unwrap().len();
|
||||
let entries_len = ENTRIES.read().unwrap_or_else(|p| p.into_inner()).len();
|
||||
|
||||
if entries_len > *BUFFER_SIZE {
|
||||
let to_remove = entries_len - *BUFFER_SIZE;
|
||||
|
||||
let mut entries = ENTRIES.write().unwrap();
|
||||
let mut entries = ENTRIES.write().unwrap_or_else(|p| p.into_inner());
|
||||
|
||||
for _ in 0..to_remove {
|
||||
entries.pop_front();
|
||||
|
@ -33,18 +37,18 @@ fn purge_old() {
|
|||
/// Generates a 'pronounceable' random ID using gpw
|
||||
pub fn generate_id() -> String {
|
||||
thread_local!(static KEYGEN: RefCell<gpw::PasswordGenerator> = RefCell::new(gpw::PasswordGenerator::default()));
|
||||
KEYGEN.with(|k| k.borrow_mut().next().unwrap())
|
||||
KEYGEN.with(|k| k.borrow_mut().next().unwrap_or_else(|| thread_rng().sample_iter(&Alphanumeric).take(6).collect::<String>()))
|
||||
}
|
||||
|
||||
/// Stores a paste under the given id
|
||||
pub fn store_paste(id: String, content: String) {
|
||||
purge_old();
|
||||
ENTRIES.write().unwrap().insert(id, content);
|
||||
ENTRIES.write().unwrap_or_else(|p| p.into_inner()).insert(id, content);
|
||||
}
|
||||
|
||||
/// Get a paste by id.
|
||||
///
|
||||
/// Returns `None` if the paste doesn't exist.
|
||||
pub fn get_paste(id: &str) -> Option<String> {
|
||||
ENTRIES.read().unwrap().get(id).cloned()
|
||||
ENTRIES.read().unwrap_or_else(|p| p.into_inner()).get(id).cloned()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue