From 4a18a4a053d0db933829d4995517fcabac5bdb4a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 23 Mar 2017 22:36:48 +0300 Subject: [PATCH 1/2] Convert try! to question mark --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 132bba6..d43f637 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,12 +6,12 @@ use std::io::{Read, Write}; /// Read a file into `Vec` from the given path. /// The path can be a string or a `Path`. pub fn get>(path: P) -> io::Result> { - let mut file = try!(File::open(path)); + let mut file = File::open(path)?; let mut data = Vec::new(); if let Ok(meta) = file.metadata() { data.reserve(meta.len() as usize); // Safe to truncate, since it's only a suggestion } - try!(file.read_to_end(&mut data)); + file.read_to_end(&mut data)?; Ok(data) } @@ -19,8 +19,8 @@ pub fn get>(path: P) -> io::Result> { /// Overwrites, non-atomically, if the file exists. /// The path can be a string or a `Path`. pub fn put, Bytes: AsRef<[u8]>>(path: P, data: Bytes) -> io::Result<()> { - let mut file = try!(File::create(path)); - try!(file.write_all(data.as_ref())); + let mut file = File::create(path)?; + file.write_all(data.as_ref())?; Ok(()) } From 1ff9a361d18468bd43579c95efed1b35df206a38 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 23 Mar 2017 22:54:28 +0300 Subject: [PATCH 2/2] Add get/put for UTF-8 data --- src/lib.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index d43f637..900cc99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,23 @@ pub fn put, Bytes: AsRef<[u8]>>(path: P, data: Bytes) -> io::Resu Ok(()) } +/// Read an UTF-8 encoded file into `String` from the given path. +/// The path can be a string or a `Path`. +pub fn get_text>(path: P) -> io::Result { + let bytes = get(path)?; + String::from_utf8(bytes).map_err(|_| { + io::Error::new(io::ErrorKind::InvalidData, "file did not contain valid UTF-8") + }) +} + +/// Creates a file at the given path with given text contents, encoded as UTF-8. +/// Overwrites, non-atomically, if the file exists. +/// The path can be a string or a `Path`. +pub fn put_text, S: AsRef>(path: P, data: S) -> io::Result<()> { + put(path, data.as_ref().as_bytes()) +} + + #[test] fn it_works() { let s = String::from_utf8(get(file!()).unwrap()).unwrap(); @@ -44,3 +61,25 @@ fn it_works() { std::fs::remove_file(tmp_name).ok(); } + +#[test] +fn it_works_with_text() { + let s = String::from_utf8(get(file!()).unwrap()).unwrap(); + assert!(s.contains("it_works()")); + + let mut tmp_name = std::env::temp_dir(); + tmp_name.push("hello"); + + put(&tmp_name, [0x80]).unwrap(); + if let Err(e) = get_text(&tmp_name) { + assert_eq!(e.kind(), io::ErrorKind::InvalidData); + } else { + panic!("Should error on invalid UTF-8") + } + + let text = "Hello, World!"; + put_text(&tmp_name, text).unwrap(); + assert_eq!(text, get_text(&tmp_name).unwrap()); + + std::fs::remove_file(tmp_name).ok(); +}