Skip to content

Commit

Permalink
Merge pull request nickel-org#7 from Ryman/master
Browse files Browse the repository at this point in the history
fix(rustup): new io & new path
  • Loading branch information
Ryman committed Mar 4, 2015
2 parents 1fec611 + 0075f26 commit 21bbfa7
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 71 deletions.
32 changes: 19 additions & 13 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::HashMap;
use std::old_io::{File, FileNotFound};
use std::io::ErrorKind::FileNotFound;
use std::io::Read;
use std::fs::File;
use std::str;
use std::path::AsPath;

use parser::{Parser, Token};
use super::Context;
Expand All @@ -27,13 +30,11 @@ impl<T: Iterator<Item=char>> Compiler<T> {
}

/// Construct a default compiler.
pub fn new_with(
ctx: Context,
reader: T,
partials: HashMap<String, Vec<Token>>,
otag: String,
ctag: String
) -> Compiler<T> {
pub fn new_with(ctx: Context,
reader: T,
partials: HashMap<String, Vec<Token>>,
otag: String,
ctag: String) -> Compiler<T> {
Compiler {
ctx: ctx,
reader: reader,
Expand All @@ -52,14 +53,18 @@ impl<T: Iterator<Item=char>> Compiler<T> {

// Compile the partials if we haven't done so already.
for name in partials.into_iter() {
let path = self.ctx.template_path.join(name.clone() + "." + self.ctx.template_extension.as_slice());
let path = self.ctx.template_path
.as_path()
.join(&(name.clone() + "." + self.ctx.template_extension.as_slice()));

if !self.partials.contains_key(&name) {
// Insert a placeholder so we don't recurse off to infinity.
self.partials.insert(name.to_string(), Vec::new());

match File::open(&path).read_to_end() {
Ok(contents) => {
match File::open(&path) {
Ok(mut file) => {
let mut contents = vec![];
file.read_to_end(&mut contents).unwrap();
let string = match str::from_utf8(contents.as_slice()) {
Ok(string) => string.to_string(),
Err(_) => { panic!("Failed to parse file as UTF-8"); }
Expand All @@ -79,7 +84,7 @@ impl<T: Iterator<Item=char>> Compiler<T> {
},
Err(e) => {
// Ignore missing files.
if e.kind != FileNotFound {
if e.kind() != FileNotFound {
panic!("error reading file: {}", e);
}
}
Expand All @@ -98,9 +103,10 @@ mod tests {
use parser::{Token, Text, ETag, UTag, Section, IncompleteSection, Partial};
use super::Compiler;
use super::super::Context;
use std::path::PathBuf;

fn compile_str(template: &str) -> Vec<Token> {
let ctx = Context::new(Path::new("."));
let ctx = Context::new(PathBuf::new("."));
let (tokens, _) = Compiler::new(ctx, template.chars()).compile();
tokens
}
Expand Down
9 changes: 8 additions & 1 deletion src/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::fmt;
use std::old_io::IoError as StdIoError;
use std::io::Error as StdIoError;
use std::error::FromError;
use std::iter::repeat;
use rustc_serialize;

Expand Down Expand Up @@ -38,6 +39,12 @@ impl fmt::Debug for Error {
}
}

impl FromError<StdIoError> for Error {
fn from_error(err: StdIoError) -> Error {
IoError(err)
}
}

pub type EncoderResult = Result<(), Error>;

impl rustc_serialize::Encoder for Encoder {
Expand Down
39 changes: 20 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#![crate_type = "dylib"]
#![crate_type = "rlib"]

#![feature(core, collections, old_path, old_io)]
#![feature(core, collections, path, fs, io)]
#![cfg_attr(test, feature(tempdir))]
#![allow(unused_attributes)]

extern crate "rustc-serialize" as rustc_serialize;
Expand All @@ -13,8 +14,10 @@ extern crate log;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt;
use std::old_io::File;
use std::fs::File;
use std::io::Read;
use std::str;
use std::path::{PathBuf, AsPath};

pub use self::Data::*;
pub use builder::{MapBuilder, VecBuilder};
Expand Down Expand Up @@ -67,21 +70,21 @@ impl<'a> fmt::Debug for Data {
/// template.
#[derive(Clone)]
pub struct Context {
pub template_path: Path,
pub template_path: PathBuf,
pub template_extension: String,
}

impl fmt::Debug for Context {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Context {{ template_path: {}, template_extension: {} }}",
self.template_path.display(),
write!(f, "Context {{ template_path: {:?}, template_extension: {} }}",
self.template_path.as_path(),
self.template_extension)
}
}

impl Context {
impl< > Context {
/// Configures a mustache context the specified path to the templates.
pub fn new(path: Path) -> Context {
pub fn new(path: PathBuf) -> Context {
Context {
template_path: path,
template_extension: "mustache".to_string(),
Expand All @@ -97,19 +100,17 @@ impl Context {
}

/// Compiles a template from a path.
pub fn compile_path(&self, path: Path) -> Result<Template, Error> {
pub fn compile_path<U: AsPath>(&self, path: U) -> Result<Template, Error> {
// FIXME(#6164): This should use the file decoding tools when they are
// written. For now we'll just read the file and treat it as UTF-8file.
let mut path = self.template_path.join(path);
path.set_extension(self.template_extension.clone());

let s = match File::open(&path).read_to_end() {
Ok(s) => s,
Err(err) => { return Err(IoError(err)); }
};
let mut path = self.template_path.as_path().join(path.as_path());
path.set_extension(&self.template_extension);
let mut s = vec![];
let mut file = try!(File::open(&path));
try!(file.read_to_end(&mut s));

// TODO: maybe allow UTF-16 as well?
let template = match str::from_utf8(s.as_slice()) {
let template = match str::from_utf8(&*s) {
Ok(string) => string,
_ => { return Result::Err(Error::InvalidStr); }
};
Expand All @@ -120,13 +121,13 @@ impl Context {

/// Compiles a template from an `Iterator<char>`.
pub fn compile_iter<T: Iterator<Item=char>>(iter: T) -> Template {
Context::new(Path::new(".")).compile(iter)
Context::new(PathBuf::new(".")).compile(iter)
}

/// Compiles a template from a path.
/// returns None if the file cannot be read OR the file is not UTF-8 encoded
pub fn compile_path(path: Path) -> Result<Template, Error> {
Context::new(Path::new(".")).compile_path(path)
pub fn compile_path<U: AsPath>(path: U) -> Result<Template, Error> {
Context::new(PathBuf::new(".")).compile_path(path)
}

/// Compiles a template from a string.
Expand Down
Loading

0 comments on commit 21bbfa7

Please sign in to comment.