Commit ca55d816 authored by Jeremy Soller's avatar Jeremy Soller

Merge branch 'warnings_and_cleanup_2' into 'master'

Warnings and cleanup - 2nd try

See merge request redox-os/extrautils!49
parents 6b683dcf a82c1a71
......@@ -146,18 +146,18 @@ fn consume_until_new_token(input: &[char]) -> String {
// Addition and subtraction
pub fn e_expr(token_list: &[Token]) -> Result<IntermediateResult, ParseError> {
let mut t1 = try!(t_expr(token_list));
let mut t1 = t_expr(token_list)?;
let mut index = t1.tokens_read;
while index < token_list.len() {
match token_list[index] {
Token::Plus => {
let t2 = try!(t_expr(&token_list[index+1..]));
let t2 = t_expr(&token_list[index+1..])?;
t1.value += t2.value;
t1.tokens_read += t2.tokens_read + 1;
}
Token::Minus => {
let t2 = try!(t_expr(&token_list[index+1..]));
let t2 = t_expr(&token_list[index+1..])?;
t1.value -= t2.value;
t1.tokens_read += t2.tokens_read + 1;
}
......@@ -171,18 +171,18 @@ pub fn e_expr(token_list: &[Token]) -> Result<IntermediateResult, ParseError> {
// Multiplication and division
pub fn t_expr(token_list: &[Token]) -> Result<IntermediateResult, ParseError> {
let mut f1 = try!(f_expr(token_list));
let mut f1 = f_expr(token_list)?;
let mut index = f1.tokens_read;
while index < token_list.len() {
match token_list[index] {
Token::Multiply => {
let f2 = try!(f_expr(&token_list[index+1..]));
let f2 = f_expr(&token_list[index+1..])?;
f1.value *= f2.value;
f1.tokens_read += f2.tokens_read + 1;
}
Token::Divide => {
let f2 = try!(f_expr(&token_list[index+1..]));
let f2 = f_expr(&token_list[index+1..])?;
if f2.value == 0.0 {
return Err(ParseError::OtherError("Divide by zero error".to_owned()));
} else {
......@@ -200,13 +200,13 @@ pub fn t_expr(token_list: &[Token]) -> Result<IntermediateResult, ParseError> {
// Exponentiation
pub fn f_expr(token_list: &[Token]) -> Result<IntermediateResult, ParseError> {
let mut g1 = try!(g_expr(token_list));
let mut g1 = g_expr(token_list)?;
let mut index = g1.tokens_read;
let token_len = token_list.len();
while index < token_len {
match token_list[index] {
Token::Exponent => {
let f = try!(f_expr(&token_list[index+1..]));
let f = f_expr(&token_list[index+1..])?;
g1.value = g1.value.powf(f.value);
g1.tokens_read += f.tokens_read + 1;
}
......
......@@ -66,12 +66,21 @@ fn main() {
};
while let Some(filename) = args.next() {
let mut file = File::open(Path::new(filename.as_str())).try(&mut stderr);
run(filename.as_str(), &mut file, &mut stdin, io::stdout()).try(&mut stderr);
let file = File::open(Path::new(filename.as_str()));
match file {
Ok(mut open_file) => {
if let Err(err) = run(filename.as_str(), &mut open_file, &mut stdin, io::stdout()) {
eprintln!("{}: {}", filename, err);
}
}
Err(err) => {
eprintln!("{}: {}", filename, err);
}
}
}
}
fn run<W: IntoRawMode>(path: &str, file: &mut Read, controls: &mut Read, stdout: W) -> std::io::Result<()> {
fn run<W: IntoRawMode>(path: &str, file: &mut dyn Read, controls: &mut dyn Read, stdout: W) -> std::io::Result<()> {
let mut string = String::new();
file.read_to_string(&mut string)?;
......
......@@ -83,9 +83,19 @@ fn main() {
run(PathBuf::from("-"), &mut stdin, &mut terminal, &mut stdout).try(&mut stderr);
};
while let Some(filename) = args.next().map(|x| PathBuf::from(x)) {
let mut file = File::open(&filename).try(&mut stderr);
run(filename, &mut file, &mut stdin, &mut stdout).try(&mut stderr);
while let Some(filename) = args.next() {
let filepath = PathBuf::from(filename.as_str());
let file = File::open(&filepath);
match file {
Ok(mut open_file) => {
if let Err(err) = run(filepath, &mut open_file, &mut stdin, &mut io::stdout()) {
eprintln!("{}: {}", &filename, err);
}
}
Err(err) => {
eprintln!("{}: {}", &filename, err);
}
}
}
}
......@@ -274,7 +284,7 @@ impl Buffer {
}
}
fn read(&mut self, from: &mut Read) -> std::io::Result<usize> {
fn read(&mut self, from: &mut dyn Read) -> std::io::Result<usize> {
let mut tmp = String::new();
let res = from.read_to_string(&mut tmp)?;
......@@ -333,7 +343,7 @@ impl Buffer {
}
}
fn run<W: IntoRawMode>(mut path: PathBuf, file: &mut Read, controls: &mut Read, stdout: &mut W) -> std::io::Result<()> {
fn run<W: IntoRawMode>(mut path: PathBuf, file: &mut dyn Read, controls: &mut dyn Read, stdout: &mut W) -> std::io::Result<()> {
let mut stdout = stdout.into_raw_mode()?;
let (w, h) = {
......
......@@ -9,7 +9,7 @@ use termion::raw::IntoRawMode;
fn main() {
let mut stderr = io::stderr();
let mut terminal = termion::get_tty().try(&mut stderr);
let terminal = termion::get_tty().try(&mut stderr);
let mut terminal = terminal.into_raw_mode().try(&mut stderr);
write!(terminal, "{}", cursor::Save).unwrap();
......
......@@ -19,19 +19,19 @@ use bzip2::read::BzDecoder;
use filetime::FileTime;
fn create_inner<T: Write>(input: &str, ar: &mut Builder<T>) -> Result<()> {
if try!(fs::metadata(input)).is_dir() {
for entry_result in try!(fs::read_dir(input)) {
let entry = try!(entry_result);
if try!(fs::metadata(entry.path())).is_dir() {
try!(create_inner(entry.path().to_str().unwrap(), ar));
if fs::metadata(input)?.is_dir() {
for entry_result in fs::read_dir(input)? {
let entry = entry_result?;
if fs::metadata(entry.path())?.is_dir() {
create_inner(entry.path().to_str().unwrap(), ar)?;
} else {
println!("{}", entry.path().display());
try!(ar.append_path(entry.path()));
ar.append_path(entry.path())?;
}
}
} else {
println!("{}", input);
try!(ar.append_path(input));
ar.append_path(input)?;
}
Ok(())
......@@ -41,14 +41,14 @@ fn create(input: &str, tar: &str) -> Result<()> {
if tar == "-" {
create_inner(input, &mut Builder::new(stdout()))
} else {
create_inner(input, &mut Builder::new(try!(File::create(tar))))
create_inner(input, &mut Builder::new(File::create(tar)?))
}
}
fn list_inner<T: Read>(ar: &mut Archive<T>) -> Result<()> {
for entry_result in try!(ar.entries()) {
let entry = try!(entry_result);
let path = try!(entry.path());
for entry_result in ar.entries()? {
let entry = entry_result?;
let path = entry.path()?;
println!("{}", path.display());
}
......@@ -59,7 +59,7 @@ fn list(tar: &str) -> Result<()> {
if tar == "-" {
list_inner(&mut Archive::new(stdin()))
} else {
list_inner(&mut Archive::new(try!(File::open(tar))))
list_inner(&mut Archive::new(File::open(tar)?))
}
}
......@@ -72,11 +72,11 @@ fn create_symlink(link: PathBuf, target: &Path) -> Result<()> {
}
fn extract_inner<T: Read>(ar: &mut Archive<T>, verbose: bool, strip: usize) -> Result<()> {
for entry_result in try!(ar.entries()) {
let mut entry = try!(entry_result);
for entry_result in ar.entries()? {
let mut entry = entry_result?;
let path = {
let path = try!(entry.path());
let path = entry.path()?;
let mut components = path.components();
for _ in 0..strip {
components.next();
......@@ -93,31 +93,29 @@ fn extract_inner<T: Read>(ar: &mut Archive<T>, verbose: bool, strip: usize) -> R
{
let mut file = {
if let Some(parent) = path.parent() {
try!(fs::create_dir_all(parent));
fs::create_dir_all(parent)?;
}
try!(
fs::OpenOptions::new()
.read(true)
.write(true)
.truncate(true)
.create(true)
.mode(entry.header().mode().unwrap_or(644))
.open(&path)
)
fs::OpenOptions::new()
.read(true)
.write(true)
.truncate(true)
.create(true)
.mode(entry.header().mode().unwrap_or(644))
.open(&path)?
};
try!(copy(&mut entry, &mut file));
copy(&mut entry, &mut file)?;
}
if let Ok(mtime) = entry.header().mtime() {
let mtime = FileTime::from_seconds_since_1970(mtime, 0);
try!(filetime::set_file_times(&path, mtime, mtime));
filetime::set_file_times(&path, mtime, mtime)?;
}
},
EntryType::Directory => {
try!(fs::create_dir_all(&path));
fs::create_dir_all(&path)?;
},
EntryType::Symlink => {
if let Some(target) = entry.link_name().expect(&format!("Can't parse symlink target for: {:?}", path)) {
try!(create_symlink(path, &target))
create_symlink(path, &target)?
}
},
other => {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment