diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-02-26 21:00:43 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-04 15:59:30 -0800 |
| commit | 95d904625b4d45af80b4e40d51a3a0fde1abaa8a (patch) | |
| tree | b0872e63b8d75543ce5141ceba44c12c459474f2 /src/rustbook | |
| parent | 3b3bb0e682c2d252e9f62dd9df5cff9552af91ad (diff) | |
| download | rust-95d904625b4d45af80b4e40d51a3a0fde1abaa8a.tar.gz rust-95d904625b4d45af80b4e40d51a3a0fde1abaa8a.zip | |
std: Deprecate std::old_io::fs
This commit deprecates the majority of std::old_io::fs in favor of std::fs and its new functionality. Some functions remain non-deprecated but are now behind a feature gate called `old_fs`. These functions will be deprecated once suitable replacements have been implemented. The compiler has been migrated to new `std::fs` and `std::path` APIs where appropriate as part of this change.
Diffstat (limited to 'src/rustbook')
| -rw-r--r-- | src/rustbook/book.rs | 26 | ||||
| -rw-r--r-- | src/rustbook/build.rs | 72 | ||||
| -rw-r--r-- | src/rustbook/error.rs | 68 | ||||
| -rw-r--r-- | src/rustbook/help.rs | 2 | ||||
| -rw-r--r-- | src/rustbook/main.rs | 30 | ||||
| -rw-r--r-- | src/rustbook/serve.rs | 2 | ||||
| -rw-r--r-- | src/rustbook/term.rs | 2 | ||||
| -rw-r--r-- | src/rustbook/test.rs | 30 |
8 files changed, 92 insertions, 140 deletions
diff --git a/src/rustbook/book.rs b/src/rustbook/book.rs index 8900b60d191..ac7f2f824cb 100644 --- a/src/rustbook/book.rs +++ b/src/rustbook/book.rs @@ -10,14 +10,16 @@ //! Basic data structures for representing a book. -use std::old_io::BufferedReader; +use std::io::prelude::*; +use std::io::BufReader; use std::iter; use std::iter::AdditiveIterator; +use std::path::{Path, PathBuf}; pub struct BookItem { pub title: String, - pub path: Path, - pub path_to_root: Path, + pub path: PathBuf, + pub path_to_root: PathBuf, pub children: Vec<BookItem>, } @@ -76,7 +78,7 @@ impl Book { } /// Construct a book by parsing a summary (markdown table of contents). -pub fn parse_summary<R: Reader>(input: R, src: &Path) -> Result<Book, Vec<String>> { +pub fn parse_summary(input: &mut Read, src: &Path) -> Result<Book, Vec<String>> { fn collapse(stack: &mut Vec<BookItem>, top_items: &mut Vec<BookItem>, to_level: usize) { @@ -100,16 +102,16 @@ pub fn parse_summary<R: Reader>(input: R, src: &Path) -> Result<Book, Vec<String // always include the introduction top_items.push(BookItem { title: "Introduction".to_string(), - path: Path::new("README.md"), - path_to_root: Path::new("."), + path: PathBuf::new("README.md"), + path_to_root: PathBuf::new("."), children: vec!(), }); - for line_result in BufferedReader::new(input).lines() { + for line_result in BufReader::new(input).lines() { let line = match line_result { Ok(line) => line, Err(err) => { - errors.push(err.desc.to_string()); // FIXME: include detail + errors.push(err.to_string()); return Err(errors); } }; @@ -125,16 +127,16 @@ pub fn parse_summary<R: Reader>(input: R, src: &Path) -> Result<Book, Vec<String let title = line[start_bracket + 1..end_bracket].to_string(); let indent = &line[..star_idx]; - let path_from_root = match src.join(given_path).path_relative_from(src) { - Some(p) => p, + let path_from_root = match src.join(given_path).relative_from(src) { + Some(p) => p.to_path_buf(), None => { errors.push(format!("paths in SUMMARY.md must be relative, \ but path '{}' for section '{}' is not.", given_path, title)); - Path::new("") + PathBuf::new("") } }; - let path_to_root = Path::new(iter::repeat("../") + let path_to_root = PathBuf::new(&iter::repeat("../") .take(path_from_root.components().count() - 1) .collect::<String>()); let item = BookItem { diff --git a/src/rustbook/build.rs b/src/rustbook/build.rs index f36d97d6d12..1fb30e15400 100644 --- a/src/rustbook/build.rs +++ b/src/rustbook/build.rs @@ -11,13 +11,14 @@ //! Implementation of the `build` subcommand, used to compile a book. use std::env; -use std::os; -use std::old_io; -use std::old_io::{fs, File, BufferedWriter, TempDir, IoResult}; +use std::fs::{self, File, TempDir}; +use std::io::prelude::*; +use std::io::{self, BufWriter}; +use std::path::{Path, PathBuf}; use subcommand::Subcommand; use term::Term; -use error::{Error, CliResult, CommandResult}; +use error::{err, CliResult, CommandResult}; use book; use book::{Book, BookItem}; use css; @@ -29,17 +30,17 @@ struct Build; pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> { if name == "build" { - Some(box Build as Box<Subcommand>) + Some(Box::new(Build)) } else { None } } -fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()> { +fn write_toc(book: &Book, path_to_root: &Path, out: &mut Write) -> io::Result<()> { fn walk_items(items: &[BookItem], section: &str, path_to_root: &Path, - out: &mut Writer) -> IoResult<()> { + out: &mut Write) -> io::Result<()> { for (i, item) in items.iter().enumerate() { try!(walk_item(item, &format!("{}{}.", section, i + 1)[..], path_to_root, out)); } @@ -48,9 +49,9 @@ fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()> fn walk_item(item: &BookItem, section: &str, path_to_root: &Path, - out: &mut Writer) -> IoResult<()> { + out: &mut Write) -> io::Result<()> { try!(writeln!(out, "<li><a href='{}'><b>{}</b> {}</a>", - path_to_root.join(item.path.with_extension("html")).display(), + path_to_root.join(&item.path.with_extension("html")).display(), section, item.title)); if !item.children.is_empty() { @@ -75,30 +76,35 @@ fn write_toc(book: &Book, path_to_root: &Path, out: &mut Writer) -> IoResult<()> fn render(book: &Book, tgt: &Path) -> CliResult<()> { let tmp = try!(TempDir::new("rust-book")); - for (section, item) in book.iter() { - println!("{} {}", section, item.title); - - let out_path = tgt.join(item.path.dirname()); + for (_section, item) in book.iter() { + let out_path = match item.path.parent() { + Some(p) => tgt.join(p), + None => tgt.to_path_buf(), + }; let src; if env::args().len() < 3 { - src = os::getcwd().unwrap().clone(); + src = env::current_dir().unwrap().clone(); } else { - src = Path::new(env::args().nth(2).unwrap().clone()); + src = PathBuf::new(&env::args().nth(2).unwrap()); } // preprocess the markdown, rerouting markdown references to html references - let markdown_data = try!(File::open(&src.join(&item.path)).read_to_string()); - let preprocessed_path = tmp.path().join(item.path.filename().unwrap()); + let mut markdown_data = String::new(); + try!(File::open(&src.join(&item.path)).and_then(|mut f| { + f.read_to_string(&mut markdown_data) + })); + let preprocessed_path = tmp.path().join(item.path.file_name().unwrap()); { let urls = markdown_data.replace(".md)", ".html)"); - try!(File::create(&preprocessed_path) - .write_str(&urls[..])); + try!(File::create(&preprocessed_path).and_then(|mut f| { + f.write_all(urls.as_bytes()) + })); } // write the prelude to a temporary HTML file for rustdoc inclusion let prelude = tmp.path().join("prelude.html"); { - let mut toc = BufferedWriter::new(try!(File::create(&prelude))); + let mut toc = BufWriter::new(try!(File::create(&prelude))); try!(writeln!(&mut toc, r#"<div id="nav"> <button id="toggle-nav"> <span class="sr-only">Toggle navigation</span> @@ -115,12 +121,12 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> { // write the postlude to a temporary HTML file for rustdoc inclusion let postlude = tmp.path().join("postlude.html"); { - let mut toc = BufferedWriter::new(try!(File::create(&postlude))); - try!(toc.write_str(javascript::JAVASCRIPT)); + let mut toc = BufWriter::new(try!(File::create(&postlude))); + try!(toc.write_all(javascript::JAVASCRIPT.as_bytes())); try!(writeln!(&mut toc, "</div></div>")); } - try!(fs::mkdir_recursive(&out_path, old_io::USER_DIR)); + try!(fs::create_dir_all(&out_path)); let rustdoc_args: &[String] = &[ "".to_string(), @@ -135,7 +141,7 @@ fn render(book: &Book, tgt: &Path) -> CliResult<()> { if output_result != 0 { let message = format!("Could not execute `rustdoc` with {:?}: {}", rustdoc_args, output_result); - return Err(box message as Box<Error>); + return Err(err(&message)); } } @@ -150,28 +156,30 @@ impl Subcommand for Build { } fn usage(&self) {} fn execute(&mut self, term: &mut Term) -> CommandResult<()> { - let cwd = os::getcwd().unwrap(); + let cwd = env::current_dir().unwrap(); let src; let tgt; if env::args().len() < 3 { src = cwd.clone(); } else { - src = Path::new(env::args().nth(2).unwrap().clone()); + src = PathBuf::new(&env::args().nth(2).unwrap()); } if env::args().len() < 4 { tgt = cwd.join("_book"); } else { - tgt = Path::new(env::args().nth(3).unwrap().clone()); + tgt = PathBuf::new(&env::args().nth(3).unwrap()); } - try!(fs::mkdir(&tgt, old_io::USER_DIR)); + try!(fs::create_dir(&tgt)); - try!(File::create(&tgt.join("rust-book.css")).write_str(css::STYLE)); + try!(File::create(&tgt.join("rust-book.css")).and_then(|mut f| { + f.write_all(css::STYLE.as_bytes()) + })); - let summary = try!(File::open(&src.join("SUMMARY.md"))); - match book::parse_summary(summary, &src) { + let mut summary = try!(File::open(&src.join("SUMMARY.md"))); + match book::parse_summary(&mut summary, &src) { Ok(book) => { // execute rustdoc on the whole book render(&book, &tgt) @@ -182,7 +190,7 @@ impl Subcommand for Build { term.err(&format!("error: {}", err)[..]); } - Err(box format!("{} errors occurred", n) as Box<Error>) + Err(err(&format!("{} errors occurred", n))) } } } diff --git a/src/rustbook/error.rs b/src/rustbook/error.rs index 43c882c7d5b..294b4e55669 100644 --- a/src/rustbook/error.rs +++ b/src/rustbook/error.rs @@ -10,10 +10,8 @@ //! Error handling utilities. WIP. +use std::error::Error; use std::fmt; -use std::fmt::{Debug, Formatter}; - -use std::old_io::IoError; pub type CliError = Box<Error + 'static>; pub type CliResult<T> = Result<T, CliError>; @@ -21,63 +19,17 @@ pub type CliResult<T> = Result<T, CliError>; pub type CommandError = Box<Error + 'static>; pub type CommandResult<T> = Result<T, CommandError>; -pub trait Error { - fn description(&self) -> &str; - - fn detail(&self) -> Option<&str> { None } - fn cause(&self) -> Option<&Error> { None } -} - -pub trait FromError<E> { - fn from_err(err: E) -> Self; -} - -impl Debug for Box<Error + 'static> { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}", self.description()) - } -} - -impl<E: Error + 'static> FromError<E> for Box<Error + 'static> { - fn from_err(err: E) -> Box<Error + 'static> { - box err as Box<Error> - } -} +pub fn err(s: &str) -> CliError { + struct E(String); -impl<'a> Error for &'a str { - fn description<'b>(&'b self) -> &'b str { - *self + impl Error for E { + fn description(&self) -> &str { &self.0 } } -} - -impl Error for String { - fn description<'a>(&'a self) -> &'a str { - &self[..] + impl fmt::Display for E { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.0.fmt(f) + } } -} - -impl<'a> Error for Box<Error + 'a> { - fn description(&self) -> &str { (**self).description() } - fn detail(&self) -> Option<&str> { (**self).detail() } - fn cause(&self) -> Option<&Error> { (**self).cause() } -} - -impl FromError<()> for () { - fn from_err(_: ()) -> () { () } -} -impl FromError<IoError> for IoError { - fn from_err(error: IoError) -> IoError { error } + Box::new(E(s.to_string())) } - -impl Error for IoError { - fn description(&self) -> &str { - self.desc - } - fn detail(&self) -> Option<&str> { - self.detail.as_ref().map(|s| &s[..]) - } -} - - -//fn iter_map_err<T, U, E, I: Iterator<Result<T,E>>>(iter: I, diff --git a/src/rustbook/help.rs b/src/rustbook/help.rs index 7fd8214f731..995d2f2494a 100644 --- a/src/rustbook/help.rs +++ b/src/rustbook/help.rs @@ -19,7 +19,7 @@ struct Help; pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> { match name { - "help" | "--help" | "-h" | "-?" => Some(box Help as Box<Subcommand>), + "help" | "--help" | "-h" | "-?" => Some(Box::new(Help)), _ => None } } diff --git a/src/rustbook/main.rs b/src/rustbook/main.rs index b9fc011e8b9..848f960839e 100644 --- a/src/rustbook/main.rs +++ b/src/rustbook/main.rs @@ -8,31 +8,24 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(box_syntax)] -#![feature(collections)] +#![deny(warnings)] + #![feature(core)] +#![feature(exit_status)] +#![feature(fs)] +#![feature(io)] #![feature(old_io)] -#![feature(env)] -#![feature(os)] -#![feature(old_path)] +#![feature(path)] #![feature(rustdoc)] +#![feature(tempdir)] extern crate rustdoc; use std::env; +use std::error::Error; use subcommand::Subcommand; use term::Term; -macro_rules! try ( - ($expr:expr) => ({ - use error; - match $expr { - Ok(val) => val, - Err(err) => return Err(error::FromError::from_err(err)) - } - }) -); - mod term; mod error; mod book; @@ -56,15 +49,12 @@ fn main() { } else { match subcommand::parse_name(&cmd[1][..]) { Some(mut subcmd) => { - match subcmd.parse_args(cmd.tail()) { + match subcmd.parse_args(&cmd[..cmd.len()-1]) { Ok(_) => { match subcmd.execute(&mut term) { Ok(_) => (), Err(err) => { - term.err(&format!("error: {}", err.description())[..]); - err.detail().map(|detail| { - term.err(&format!("detail: {}", detail)[..]); - }); + term.err(&format!("error: {}", err)); } } } diff --git a/src/rustbook/serve.rs b/src/rustbook/serve.rs index 808527dcef9..2fa7b7eed7b 100644 --- a/src/rustbook/serve.rs +++ b/src/rustbook/serve.rs @@ -19,7 +19,7 @@ struct Serve; pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> { if name == "serve" { - Some(box Serve as Box<Subcommand>) + Some(Box::new(Serve)) } else { None } diff --git a/src/rustbook/term.rs b/src/rustbook/term.rs index 98aa3fca184..06595cb0455 100644 --- a/src/rustbook/term.rs +++ b/src/rustbook/term.rs @@ -21,7 +21,7 @@ pub struct Term { impl Term { pub fn new() -> Term { Term { - err: box stdio::stderr() as Box<Writer>, + err: Box::new(stdio::stderr()) } } diff --git a/src/rustbook/test.rs b/src/rustbook/test.rs index 727a385a8f0..72df0768e7b 100644 --- a/src/rustbook/test.rs +++ b/src/rustbook/test.rs @@ -11,19 +11,19 @@ //! Implementation of the `test` subcommand. Just a stub for now. use subcommand::Subcommand; -use error::CliResult; -use error::CommandResult; -use error::Error; +use error::{err, CliResult, CommandResult}; use term::Term; use book; -use std::old_io::{Command, File}; -use std::os; + +use std::fs::File; +use std::env; +use std::process::Command; struct Test; pub fn parse_cmd(name: &str) -> Option<Box<Subcommand>> { if name == "test" { - Some(box Test as Box<Subcommand>) + Some(Box::new(Test)) } else { None } @@ -35,11 +35,11 @@ impl Subcommand for Test { } fn usage(&self) {} fn execute(&mut self, term: &mut Term) -> CommandResult<()> { - let cwd = os::getcwd().unwrap(); + let cwd = env::current_dir().unwrap(); let src = cwd.clone(); - let summary = File::open(&src.join("SUMMARY.md")); - match book::parse_summary(summary, &src) { + let mut summary = try!(File::open(&src.join("SUMMARY.md"))); + match book::parse_summary(&mut summary, &src) { Ok(book) => { for (_, item) in book.iter() { let output_result = Command::new("rustdoc") @@ -50,15 +50,15 @@ impl Subcommand for Test { Ok(output) => { if !output.status.success() { term.err(&format!("{}\n{}", - String::from_utf8_lossy(&output.output[..]), - String::from_utf8_lossy(&output.error[..]))[..]); - return Err(box "Some tests failed." as Box<Error>); + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr))); + return Err(err("some tests failed")); } } Err(e) => { - let message = format!("Could not execute `rustdoc`: {}", e); - return Err(box message as Box<Error>); + let message = format!("could not execute `rustdoc`: {}", e); + return Err(err(&message)) } } } @@ -67,7 +67,7 @@ impl Subcommand for Test { for err in errors { term.err(&err[..]); } - return Err(box "There was an error." as Box<Error>); + return Err(err("there was an error")) } } Ok(()) // lol |
