1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//! # Objective:
//! 
//! A collection of convenience functions, macros and traits to shorten repetitive code.
//! 
//! # Status
//! 
//! Experimental
//! 
//! # Version
//! 
//! 0.1.1
//! 
//! ## Example 1:
//! 
//! Shorten the convertion to a String.
//! 
//! `s!("Hello")` is the same as `String::from("Hello"))`
//! 
//! ## Example 2:
//! 
//! Concatenate two string(s) (slices) and return a **string slice**.
//! 
//! `ss!("Hello", ", world")` is the same as `"Hello, world";`
//! 
//! The same macro works also with an arbitrarz combination of String objects and string slices
//! 
//! ```rust
//! // #[macro_use] extern crate shorten;
//! // let s1 = s!("Hello");
//! // let s2 = s!(", world");
//! // assert_eq!(ss!(s1, s2), "Hello, world");
//! ```
//! 
//! ## Example 3:
//! ```rust
//! use shorten::*;
//! let data = fread("README.md").unwrap();
//! assert!(data.len() > 100);
//! ```

// Shorten String::from("hello") to s!("hello")
macro_rules! s { ( $x:expr ) => { String::from($x); }; }

// Concatenate two string(s) (slices) and return a string slice
macro_rules! ss {	($x:expr, $y:expr) => ( &*format!("{}{}", $x, $y); ) }

use std::fs::File;
use std::path::Path;
use std::io;

#[allow(unused_imports)]
use std::io::{Read, Write};

/// Read a file into a Vec[u8]
pub fn fread<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
  let mut file = File::open(path)?;
  let meta = file.metadata()?;
  let size = meta.len() as usize;
  let mut data = Vec::with_capacity(size);
  data.resize(size, 0);
  file.read_exact(&mut data)?;
  Ok(data)
}

#[cfg(test)]
mod tests {
	use super::*;
	#[test]
	fn s() {
		assert_eq!(s!("Hello"), String::from("Hello"));
	}

	#[test]
	fn ss() {
		assert_eq!(ss!("Hello", ", world"), "Hello, world");

		let s1 = s!("Hello");
		let s2 = s!(", world");
		assert_eq!(ss!(s1, s2), "Hello, world");
	}

	#[test]
	fn fread_test() {
		let data = fread("README.md").unwrap();
		assert!(data.len() > 100);
	}
}