<commit_before>extern crate cc;
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate semver;

use regex::Regex;
use semver::Version;
use std::env;
use std::ffi::OsStr;
use std::io::{self, ErrorKind};
use std::path::PathBuf;
use std::process::Command;

lazy_static!{
    /// LLVM version used by this version of the crate.
    static ref CRATE_VERSION: Version = {
        let crate_version = Version::parse(env!("CARGO_PKG_VERSION"))
            .expect("Crate version is somehow not valid semver");
        Version {
            major: crate_version.major / 10,
            minor: crate_version.major % 10,
            .. crate_version
        }
    };

    static ref LLVM_CONFIG_BINARY_NAMES: Vec<String> = {
        vec![
            "llvm-config".into(),
            format!("llvm-config-{}", CRATE_VERSION.major),
            format!("llvm-config-{}.{}", CRATE_VERSION.major, CRATE_VERSION.minor),
        ]
    };

    /// Filesystem path to an llvm-config binary for the correct version.
    static ref LLVM_CONFIG_PATH: PathBuf = {
        // Try llvm-config via PATH first.
        if let Some(name) = locate_system_llvm_config() {
            return name.into();
        } else {
            println!("Didn't find usable system-wide LLVM.");
        }

        // Did the user give us a binary path to use? If yes, try
        // to use that and fail if it doesn't work.
        let binary_prefix_var = format!("LLVM_SYS_{}_PREFIX",
                                        env!("CARGO_PKG_VERSION_MAJOR"));
        if let Some(path) = env::var_os(&binary_prefix_var) {
            for binary_name in LLVM_CONFIG_BINARY_NAMES.iter() {
                let mut pb: PathBuf = path.clone().into();
                pb.push("bin");
                pb.push(binary_name);

                let ver = llvm_version(&pb)
                    .expect(&format!("Failed to execute {:?}", &pb));
                if is_compatible_llvm(&ver) {
                    return pb;
                } else {
                    println!("LLVM binaries specified by {} are the wrong version.
                              (Found {}, need {}.)", binary_prefix_var, ver, *CRATE_VERSION);
                }
            }
        }

        println!("No suitable version of LLVM was found system-wide or pointed
                  to by {}.

                  Consider using `llvmenv` to compile an appropriate copy of LLVM, and
                  refer to the llvm-sys documentation for more information.

                  llvm-sys: https://crates.io/crates/llvm-sys
                  llvmenv: https://crates.io/crates/llvmenv", binary_prefix_var);
        panic!("Could not find a compatible version of LLVM");
    };
}

/// Try to find a system-wide version of llvm-config that is compatible with
/// this crate.
///
/// Returns None on failure.
fn locate_system_llvm_config() -> Option<&'static str> {
    for binary_name in LLVM_CONFIG_BINARY_NAMES.iter() {
        match llvm_version(binary_name) {
            Ok(ref version) if is_compatible_llvm(version) => {
                // Compatible version found. Nice.
                return Some(binary_name);
            }
            Ok(version) => {
                // Version mismatch. Will try further searches, but warn that
                // we're not using the system one.
                println!("Found LLVM version {} on PATH, but need {}.",
                         version,
                         *CRATE_VERSION);
            }
            Err(ref e) if e.kind() == ErrorKind::NotFound => {
                // Looks like we failed to execute any llvm-config. Keep
                // searching.
            }
            // Some other error, probably a weird failure. Give up.
            Err(e) => panic!("Failed to search PATH for llvm-config: {}", e),
        }
    }

    None
}

/// Check whether the given version of LLVM is blacklisted,
/// returning `Some(reason)` if it is.
fn is_blacklisted_llvm(llvm_version: &Version) -> Option<&'static str> {
    static BLACKLIST: &'static [(u64, u64, u64, &'static str)] = &[];

    let blacklist_var = format!("LLVM_SYS_{}_IGNORE_BLACKLIST",
                                env!("CARGO_PKG_VERSION_MAJOR"));
    if let Some(x) = env::var_os(&blacklist_var) {
        if &x == "YES" {
            println!("cargo:warning=Ignoring blacklist entry for LLVM {}",
                     llvm_version);
            return None;
        } else {
            println!("cargo:warning={} is set but not exactly \"YES\"; blacklist is still honored.",
                     &blacklist_var);
        }
    }

    for &(major, minor, patch, reason) in BLACKLIST.iter() {
        let bad_version = Version {
            major: major,
            minor: minor,
            patch: patch,
            pre: vec![],
            build: vec![],
        };

        if &bad_version == llvm_version {
            return Some(reason);
        }
    }
    None
}

/// Check whether the given LLVM version is compatible with this version of
/// the crate.
fn is_compatible_llvm(llvm_version: &Version) -> bool {
    if let Some(reason) = is_blacklisted_llvm(llvm_version) {
        println!("Found LLVM {}, which is blacklisted: {}",
                 llvm_version,
                 reason);
        return false;
    }

    let strict = env::var_os(format!("LLVM_SYS_{}_STRICT_VERSIONING",
                                     env!("CARGO_PKG_VERSION_MAJOR")))
            .is_some() || cfg!(feature = "strict-versioning");
    if strict {
        llvm_version.major == CRATE_VERSION.major && llvm_version.minor == CRATE_VERSION.minor
    } else {
        llvm_version.major >= CRATE_VERSION.major ||
        (llvm_version.major == CRATE_VERSION.major && llvm_version.minor >= CRATE_VERSION.minor)
    }
}

/// Get the output from running `llvm-config` with the given argument.
///
/// Lazily searches for or compiles LLVM as configured by the environment
/// variables.
fn llvm_config(arg: &str) -> String {
    llvm_config_ex(&*LLVM_CONFIG_PATH, arg).expect("Surprising failure from llvm-config")
}

/// Invoke the specified binary as llvm-config.
///
/// Explicit version of the `llvm_config` function that bubbles errors
/// up.
fn llvm_config_ex<S: AsRef<OsStr>>(binary: S, arg: &str) -> io::Result<String> {
    Command::new(binary)
        .arg(arg)
        .arg("--link-static")   // Don't use dylib for >= 3.9
        .output()
        .map(|output| String::from_utf8(output.stdout)
            .expect("Output from llvm-config was not valid UTF-8"))
}

/// Get the LLVM version using llvm-config.
fn llvm_version<S: AsRef<OsStr>>(binary: S) -> io::Result<Version> {
    let version_str = try!(llvm_config_ex(binary.as_ref(), "--version"));

    // LLVM isn't really semver and uses version suffixes to build
    // version strings like '3.8.0svn', so limit what we try to parse
    // to only the numeric bits.
    let re = Regex::new(r"^(?P<major>\d+)\.(?P<minor>\d+)(?:\.(?P<patch>\d+))??").unwrap();
    let c = re.captures(&version_str).expect("Could not determine LLVM version from llvm-config.");

    // some systems don't have a patch number but Version wants it so we just append .0 if it isn't
    // there
    let s = match c.name("patch") {
        None => format!("{}.0", &c[0]),
        Some(_) => c[0].to_string(),
    };
    Ok(Version::parse(&s).unwrap())
}

/// Get the names of the dylibs required by LLVM, including the C++ standard
/// library.
fn get_system_libraries() -> Vec<String> {
    llvm_config("--system-libs")
        .split(&[' ', '\n'] as &[char])
        .filter(|s| !s.is_empty())
        .map(|flag| {
            if cfg!(target_env = "msvc") {
                // Same as --libnames, foo.lib
                assert!(flag.ends_with(".lib"));
                &flag[..flag.len() - 4]
            } else {
                // Linker flags style, -lfoo
                assert!(flag.starts_with("-l"));
                &flag[2..]
            }
        })
        .chain(get_system_libcpp())
        .map(str::to_owned)
        .collect::<Vec<String>>()
}

/// Get the library that must be linked for C++, if any.
fn get_system_libcpp() -> Option<&'static str> {
    if cfg!(target_env = "msvc") {
        // MSVC doesn't need an explicit one.
        None
    } else if cfg!(target_os = "macos") {
        // On OS X 10.9 and later, LLVM's libc++ is the default. On earlier
        // releases GCC's libstdc++ is default. Unfortunately we can't
        // reasonably detect which one we need (on older ones libc++ is
        // available and can be selected with -stdlib=lib++), so assume the
        // latest, at the cost of breaking the build on older OS releases
        // when LLVM was built against libstdc++.
        Some("c++")
    } else if cfg!(target_os = "freebsd") {
        Some("c++")
    } else {
        // Otherwise assume GCC's libstdc++.
        // This assumption is probably wrong on some platforms, but would need
        // testing on them.
        Some("stdc++")
    }
}

/// Get the names of libraries to link against.
fn get_link_libraries() -> Vec<String> {
    // Using --libnames in conjunction with --libdir is particularly important
    // for MSVC when LLVM is in a path with spaces, but it is generally less of
    // a hack than parsing linker flags output from --libs and --ldflags.
    llvm_config("--libnames")
        .split(&[' ', '\n'] as &[char])
        .filter(|s| !s.is_empty())
        .map(|name| {
            // --libnames gives library filenames. Extract only the name that
            // we need to pass to the linker.
            if cfg!(target_env = "msvc") {
                // LLVMfoo.lib
                assert!(name.ends_with(".lib"));
                &name[..name.len() - 4]
            } else {
                // libLLVMfoo.a
                assert!(name.starts_with("lib") && name.ends_with(".a"));
                &name[3..name.len() - 2]
            }
        })
        .map(str::to_owned)
        .collect::<Vec<String>>()
}

fn get_llvm_cflags() -> String {
    let output = llvm_config("--cflags");

    // llvm-config includes cflags from its own compilation with --cflags that
    // may not be relevant to us. In particularly annoying cases, these might
    // include flags that aren't understood by the default compiler we're
    // using. Unless requested otherwise, clean CFLAGS of options that are
    // known to be possibly-harmful.
    let no_clean = env::var_os(format!("LLVM_SYS_{}_NO_CLEAN_CFLAGS",
                                       env!("CARGO_PKG_VERSION_MAJOR")))
            .is_some();
    if no_clean || cfg!(target_env = "msvc") {
        // MSVC doesn't accept -W... options, so don't try to strip them and
        // possibly strip something that should be retained. Also do nothing if
        // the user requests it.
        return output;
    }

    llvm_config("--cflags")
        .split(&[' ', '\n'][..])
        .filter(|word| !word.starts_with("-W"))
        .collect::<Vec<_>>()
        .join(" ")
}

fn main() {
    if cfg!(feature = "no-llvm-linking") {
        return;
    }

    let libdir = llvm_config("--libdir");

    // Export information to other crates
    println!("cargo:config_path={}", LLVM_CONFIG_PATH.display()); // will be DEP_LLVM_CONFIG_PATH
    println!("cargo:libdir={}", libdir); // DEP_LLVM_LIBDIR

    // Link LLVM libraries
    println!("cargo:rustc-link-search=native={}", libdir);
    for name in get_link_libraries() {
        println!("cargo:rustc-link-lib=static={}", name);
    }

    // Link system libraries
    for name in get_system_libraries() {
        println!("cargo:rustc-link-lib=dylib={}", name);
    }

    // Link libffi if the user requested this workaround.
    // See https://bitbucket.org/tari/llvm-sys.rs/issues/12/
    let force_ffi = env::var_os(format!("LLVM_SYS_{}_FFI_WORKAROUND",
                                        env!("CARGO_PKG_VERSION_MAJOR")))
            .is_some();
    if force_ffi {
        println!("cargo:rustc-link-lib=dylib={}", "ffi");
    }

    // Build the extra wrapper functions.
    std::env::set_var("CFLAGS", get_llvm_cflags());
    cc::Build::new().file("wrappers/target.c").compile("targetwrappers");
}
<commit_msg>Build wrappers even if no-llvm-linking is enabled<commit_after>extern crate cc;
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate semver;

use regex::Regex;
use semver::Version;
use std::env;
use std::ffi::OsStr;
use std::io::{self, ErrorKind};
use std::path::PathBuf;
use std::process::Command;

lazy_static!{
    /// LLVM version used by this version of the crate.
    static ref CRATE_VERSION: Version = {
        let crate_version = Version::parse(env!("CARGO_PKG_VERSION"))
            .expect("Crate version is somehow not valid semver");
        Version {
            major: crate_version.major / 10,
            minor: crate_version.major % 10,
            .. crate_version
        }
    };

    static ref LLVM_CONFIG_BINARY_NAMES: Vec<String> = {
        vec![
            "llvm-config".into(),
            format!("llvm-config-{}", CRATE_VERSION.major),
            format!("llvm-config-{}.{}", CRATE_VERSION.major, CRATE_VERSION.minor),
        ]
    };

    /// Filesystem path to an llvm-config binary for the correct version.
    static ref LLVM_CONFIG_PATH: PathBuf = {
        // Try llvm-config via PATH first.
        if let Some(name) = locate_system_llvm_config() {
            return name.into();
        } else {
            println!("Didn't find usable system-wide LLVM.");
        }

        // Did the user give us a binary path to use? If yes, try
        // to use that and fail if it doesn't work.
        let binary_prefix_var = format!("LLVM_SYS_{}_PREFIX",
                                        env!("CARGO_PKG_VERSION_MAJOR"));
        if let Some(path) = env::var_os(&binary_prefix_var) {
            for binary_name in LLVM_CONFIG_BINARY_NAMES.iter() {
                let mut pb: PathBuf = path.clone().into();
                pb.push("bin");
                pb.push(binary_name);

                let ver = llvm_version(&pb)
                    .expect(&format!("Failed to execute {:?}", &pb));
                if is_compatible_llvm(&ver) {
                    return pb;
                } else {
                    println!("LLVM binaries specified by {} are the wrong version.
                              (Found {}, need {}.)", binary_prefix_var, ver, *CRATE_VERSION);
                }
            }
        }

        println!("No suitable version of LLVM was found system-wide or pointed
                  to by {}.

                  Consider using `llvmenv` to compile an appropriate copy of LLVM, and
                  refer to the llvm-sys documentation for more information.

                  llvm-sys: https://crates.io/crates/llvm-sys
                  llvmenv: https://crates.io/crates/llvmenv", binary_prefix_var);
        panic!("Could not find a compatible version of LLVM");
    };
}

/// Try to find a system-wide version of llvm-config that is compatible with
/// this crate.
///
/// Returns None on failure.
fn locate_system_llvm_config() -> Option<&'static str> {
    for binary_name in LLVM_CONFIG_BINARY_NAMES.iter() {
        match llvm_version(binary_name) {
            Ok(ref version) if is_compatible_llvm(version) => {
                // Compatible version found. Nice.
                return Some(binary_name);
            }
            Ok(version) => {
                // Version mismatch. Will try further searches, but warn that
                // we're not using the system one.
                println!("Found LLVM version {} on PATH, but need {}.",
                         version,
                         *CRATE_VERSION);
            }
            Err(ref e) if e.kind() == ErrorKind::NotFound => {
                // Looks like we failed to execute any llvm-config. Keep
                // searching.
            }
            // Some other error, probably a weird failure. Give up.
            Err(e) => panic!("Failed to search PATH for llvm-config: {}", e),
        }
    }

    None
}

/// Check whether the given version of LLVM is blacklisted,
/// returning `Some(reason)` if it is.
fn is_blacklisted_llvm(llvm_version: &Version) -> Option<&'static str> {
    static BLACKLIST: &'static [(u64, u64, u64, &'static str)] = &[];

    let blacklist_var = format!("LLVM_SYS_{}_IGNORE_BLACKLIST",
                                env!("CARGO_PKG_VERSION_MAJOR"));
    if let Some(x) = env::var_os(&blacklist_var) {
        if &x == "YES" {
            println!("cargo:warning=Ignoring blacklist entry for LLVM {}",
                     llvm_version);
            return None;
        } else {
            println!("cargo:warning={} is set but not exactly \"YES\"; blacklist is still honored.",
                     &blacklist_var);
        }
    }

    for &(major, minor, patch, reason) in BLACKLIST.iter() {
        let bad_version = Version {
            major: major,
            minor: minor,
            patch: patch,
            pre: vec![],
            build: vec![],
        };

        if &bad_version == llvm_version {
            return Some(reason);
        }
    }
    None
}

/// Check whether the given LLVM version is compatible with this version of
/// the crate.
fn is_compatible_llvm(llvm_version: &Version) -> bool {
    if let Some(reason) = is_blacklisted_llvm(llvm_version) {
        println!("Found LLVM {}, which is blacklisted: {}",
                 llvm_version,
                 reason);
        return false;
    }

    let strict = env::var_os(format!("LLVM_SYS_{}_STRICT_VERSIONING",
                                     env!("CARGO_PKG_VERSION_MAJOR")))
            .is_some() || cfg!(feature = "strict-versioning");
    if strict {
        llvm_version.major == CRATE_VERSION.major && llvm_version.minor == CRATE_VERSION.minor
    } else {
        llvm_version.major >= CRATE_VERSION.major ||
        (llvm_version.major == CRATE_VERSION.major && llvm_version.minor >= CRATE_VERSION.minor)
    }
}

/// Get the output from running `llvm-config` with the given argument.
///
/// Lazily searches for or compiles LLVM as configured by the environment
/// variables.
fn llvm_config(arg: &str) -> String {
    llvm_config_ex(&*LLVM_CONFIG_PATH, arg).expect("Surprising failure from llvm-config")
}

/// Invoke the specified binary as llvm-config.
///
/// Explicit version of the `llvm_config` function that bubbles errors
/// up.
fn llvm_config_ex<S: AsRef<OsStr>>(binary: S, arg: &str) -> io::Result<String> {
    Command::new(binary)
        .arg(arg)
        .arg("--link-static")   // Don't use dylib for >= 3.9
        .output()
        .map(|output| String::from_utf8(output.stdout)
            .expect("Output from llvm-config was not valid UTF-8"))
}

/// Get the LLVM version using llvm-config.
fn llvm_version<S: AsRef<OsStr>>(binary: S) -> io::Result<Version> {
    let version_str = try!(llvm_config_ex(binary.as_ref(), "--version"));

    // LLVM isn't really semver and uses version suffixes to build
    // version strings like '3.8.0svn', so limit what we try to parse
    // to only the numeric bits.
    let re = Regex::new(r"^(?P<major>\d+)\.(?P<minor>\d+)(?:\.(?P<patch>\d+))??").unwrap();
    let c = re.captures(&version_str).expect("Could not determine LLVM version from llvm-config.");

    // some systems don't have a patch number but Version wants it so we just append .0 if it isn't
    // there
    let s = match c.name("patch") {
        None => format!("{}.0", &c[0]),
        Some(_) => c[0].to_string(),
    };
    Ok(Version::parse(&s).unwrap())
}

/// Get the names of the dylibs required by LLVM, including the C++ standard
/// library.
fn get_system_libraries() -> Vec<String> {
    llvm_config("--system-libs")
        .split(&[' ', '\n'] as &[char])
        .filter(|s| !s.is_empty())
        .map(|flag| {
            if cfg!(target_env = "msvc") {
                // Same as --libnames, foo.lib
                assert!(flag.ends_with(".lib"));
                &flag[..flag.len() - 4]
            } else {
                // Linker flags style, -lfoo
                assert!(flag.starts_with("-l"));
                &flag[2..]
            }
        })
        .chain(get_system_libcpp())
        .map(str::to_owned)
        .collect::<Vec<String>>()
}

/// Get the library that must be linked for C++, if any.
fn get_system_libcpp() -> Option<&'static str> {
    if cfg!(target_env = "msvc") {
        // MSVC doesn't need an explicit one.
        None
    } else if cfg!(target_os = "macos") {
        // On OS X 10.9 and later, LLVM's libc++ is the default. On earlier
        // releases GCC's libstdc++ is default. Unfortunately we can't
        // reasonably detect which one we need (on older ones libc++ is
        // available and can be selected with -stdlib=lib++), so assume the
        // latest, at the cost of breaking the build on older OS releases
        // when LLVM was built against libstdc++.
        Some("c++")
    } else if cfg!(target_os = "freebsd") {
        Some("c++")
    } else {
        // Otherwise assume GCC's libstdc++.
        // This assumption is probably wrong on some platforms, but would need
        // testing on them.
        Some("stdc++")
    }
}

/// Get the names of libraries to link against.
fn get_link_libraries() -> Vec<String> {
    // Using --libnames in conjunction with --libdir is particularly important
    // for MSVC when LLVM is in a path with spaces, but it is generally less of
    // a hack than parsing linker flags output from --libs and --ldflags.
    llvm_config("--libnames")
        .split(&[' ', '\n'] as &[char])
        .filter(|s| !s.is_empty())
        .map(|name| {
            // --libnames gives library filenames. Extract only the name that
            // we need to pass to the linker.
            if cfg!(target_env = "msvc") {
                // LLVMfoo.lib
                assert!(name.ends_with(".lib"));
                &name[..name.len() - 4]
            } else {
                // libLLVMfoo.a
                assert!(name.starts_with("lib") && name.ends_with(".a"));
                &name[3..name.len() - 2]
            }
        })
        .map(str::to_owned)
        .collect::<Vec<String>>()
}

fn get_llvm_cflags() -> String {
    let output = llvm_config("--cflags");

    // llvm-config includes cflags from its own compilation with --cflags that
    // may not be relevant to us. In particularly annoying cases, these might
    // include flags that aren't understood by the default compiler we're
    // using. Unless requested otherwise, clean CFLAGS of options that are
    // known to be possibly-harmful.
    let no_clean = env::var_os(format!("LLVM_SYS_{}_NO_CLEAN_CFLAGS",
                                       env!("CARGO_PKG_VERSION_MAJOR")))
            .is_some();
    if no_clean || cfg!(target_env = "msvc") {
        // MSVC doesn't accept -W... options, so don't try to strip them and
        // possibly strip something that should be retained. Also do nothing if
        // the user requests it.
        return output;
    }

    llvm_config("--cflags")
        .split(&[' ', '\n'][..])
        .filter(|word| !word.starts_with("-W"))
        .collect::<Vec<_>>()
        .join(" ")
}

fn main() {
    // Build the extra wrapper functions.
    std::env::set_var("CFLAGS", get_llvm_cflags());
    cc::Build::new().file("wrappers/target.c").compile("targetwrappers");

    if cfg!(feature = "no-llvm-linking") {
        return;
    }

    let libdir = llvm_config("--libdir");

    // Export information to other crates
    println!("cargo:config_path={}", LLVM_CONFIG_PATH.display()); // will be DEP_LLVM_CONFIG_PATH
    println!("cargo:libdir={}", libdir); // DEP_LLVM_LIBDIR

    // Link LLVM libraries
    println!("cargo:rustc-link-search=native={}", libdir);
    for name in get_link_libraries() {
        println!("cargo:rustc-link-lib=static={}", name);
    }

    // Link system libraries
    for name in get_system_libraries() {
        println!("cargo:rustc-link-lib=dylib={}", name);
    }

    // Link libffi if the user requested this workaround.
    // See https://bitbucket.org/tari/llvm-sys.rs/issues/12/
    let force_ffi = env::var_os(format!("LLVM_SYS_{}_FFI_WORKAROUND",
                                        env!("CARGO_PKG_VERSION_MAJOR")))
            .is_some();
    if force_ffi {
        println!("cargo:rustc-link-lib=dylib={}", "ffi");
    }
}
<|endoftext|>


<commit_before>// Copyright 2015 Nathan Sizemore <nathanrsizemore@gmail.com>
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL was not
// distributed with this file, You can obtain one at
// http://mozilla.org/MPL/2.0/.

extern crate gcc;

fn main() {
    if cfg!(linux) {
        gcc::compile_library("libshim.a", &["shim.c"]);
    } else {
        gcc::compile_library("libshim.a", &["shim-osx.c"]);
    }
}
<commit_msg>Messed up target os<commit_after>// Copyright 2015 Nathan Sizemore <nathanrsizemore@gmail.com>
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL was not
// distributed with this file, You can obtain one at
// http://mozilla.org/MPL/2.0/.

extern crate gcc;

fn main() {
    if cfg!(target_os = "lniux") {
        gcc::compile_library("libshim.a", &["shim.c"]);
    } else {
        gcc::compile_library("libshim.a", &["shim-osx.c"]);
    }
}
<|endoftext|>


<commit_before>#![feature(old_io, old_path)]
use std::old_io::Command;
use std::old_io::fs::PathExtensions;
use std::old_io::fs;
use std::env;

fn main() {
  let out_dir = env::var("OUT_DIR").unwrap();
  let mut objects = Vec::new();

  let files = fs::readdir(&Path::new("src")).unwrap();
  let mut files = files.iter().filter(|p| p.is_file());

  for file in files {
    if let Some(filename) = file.filename_str() {
      let filepath = format!("src/{}", filename);
      let outpath;

      if let Some(basename) = eat_extension(filename, ".c") {
        outpath = format!("{}/{}.o", out_dir, basename);

        Command::new("cc").args(&[&*filepath, "-c", "-fPIC", "-o"])
                          .arg(outpath.clone())
                          .status().unwrap();
      }
      else { continue }

      objects.push(outpath);
    }
  }

  Command::new("ar").args(&["crus", "libcontext.a"])
                    .args(&*objects)
                    .cwd(&Path::new(&out_dir))
                    .status().unwrap();

  println!("cargo:rustc-flags=-L {} -l context:static", out_dir);
}

fn eat_extension<'a>(s: &'a str, ext: &str) -> Option<&'a str> {
  if s.ends_with(ext) {
    Some(&s[..s.len() - ext.len()])
  }
  else {
    None
  }
}
<commit_msg>that doesn't need to be mutable<commit_after>#![feature(old_io, old_path)]
use std::old_io::Command;
use std::old_io::fs::PathExtensions;
use std::old_io::fs;
use std::env;

fn main() {
  let out_dir = env::var("OUT_DIR").unwrap();
  let mut objects = Vec::new();

  let files = fs::readdir(&Path::new("src")).unwrap();
  let files = files.iter().filter(|p| p.is_file());

  for file in files {
    if let Some(filename) = file.filename_str() {
      let filepath = format!("src/{}", filename);
      let outpath;

      if let Some(basename) = eat_extension(filename, ".c") {
        outpath = format!("{}/{}.o", out_dir, basename);

        Command::new("cc").args(&[&*filepath, "-c", "-fPIC", "-o"])
                          .arg(outpath.clone())
                          .status().unwrap();
      }
      else { continue }

      objects.push(outpath);
    }
  }

  Command::new("ar").args(&["crus", "libcontext.a"])
                    .args(&*objects)
                    .cwd(&Path::new(&out_dir))
                    .status().unwrap();

  println!("cargo:rustc-flags=-L {} -l context:static", out_dir);
}

fn eat_extension<'a>(s: &'a str, ext: &str) -> Option<&'a str> {
  if s.ends_with(ext) {
    Some(&s[..s.len() - ext.len()])
  }
  else {
    None
  }
}
<|endoftext|>


<commit_before>#[link(name="du", vers="1.0.0", author="Derek Chiang")];

/*
 * This file is part of the uutils coreutils package.
 *
 * (c) Derek Chiang <derekchiang93@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

extern mod extra;

use std::os;
use std::io::stderr;
use std::io::fs;
use std::io::FileStat;
use std::option::Option;
use std::path::Path;
use extra::arc::Arc;
use extra::future::Future;
use extra::getopts::groups;
use extra::time::Timespec;

static VERSION: &'static str = "1.0.0";

struct Options {
    all: bool,
    max_depth: Option<uint>,
    total: bool,
    separate_dirs: bool,
}

fn du(path: &Path, options_arc: Arc<Options>, depth: uint) -> ~[Arc<FileStat>] {
    let mut stats = ~[];
    let mut futures = ~[];
    let options = options_arc.get();
    let mut my_stat = path.stat();

    for f in fs::readdir(path).move_iter() {
        match f.is_file() {
            true => {
                let stat = f.stat();
                my_stat.size += stat.size;
                my_stat.unstable.blocks += stat.unstable.blocks;
                if options.all {
                    stats.push(Arc::new(stat))
                }
            }
            false => {
                let oa_clone = options_arc.clone();
                futures.push(do Future::spawn { du(&f, oa_clone, depth + 1) })
            }
        }
    }

    for future in futures.mut_iter() {
        for stat_arc in future.get().move_rev_iter() {
            let stat = stat_arc.get();
            if !options.separate_dirs && stat.path.dir_path() == my_stat.path {
                my_stat.size += stat.size;
                my_stat.unstable.blocks += stat.unstable.blocks;
            }
            if options.max_depth == None || depth < options.max_depth.unwrap() {
                stats.push(stat_arc.clone());
            }
        }
    }

    stats.push(Arc::new(my_stat));

    return stats;
}

fn main() {
    let args = os::args();
    let program = args[0].clone();
    let opts = ~[
        // In task
        groups::optflag("a", "all", " write counts for all files, not just directories"),
        // In main
        groups::optflag("", "apparent-size", "print apparent sizes,  rather  than  disk  usage;
            although  the apparent  size is usually smaller, it may be larger due to holes
            in ('sparse') files, internal  fragmentation,  indirect  blocks, and the like"),
        // In main
        groups::optopt("B", "block-size", "scale sizes  by  SIZE before printing them.
            E.g., '-BM' prints sizes in units of 1,048,576 bytes.  See SIZE format below.",
            "SIZE"),
        // In main
        groups::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"),
        // In main
        groups::optflag("c", "total", "produce a grand total"),
        // In task
        // groups::optflag("D", "dereference-args", "dereference only symlinks that are listed
        //     on the command line"),
        // In main
        // groups::optopt("", "files0-from", "summarize disk usage of the NUL-terminated file
        //                   names specified in file F;
        //                   If F is - then read names from standard input", "F"),
        // // In task
        // groups::optflag("H", "", "equivalent to --dereference-args (-D)"),
        // In main
        groups::optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"),
        // In main
        groups::optflag("", "si", "like -h, but use powers of 1000 not 1024"),
        // In main
        groups::optflag("k", "", "like --block-size=1K"),
        // In task
        groups::optflag("l", "count-links", "count sizes many times if hard linked"),
        // // In main
        groups::optflag("m", "", "like --block-size=1M"),
        // // In task
        // groups::optflag("L", "dereference", "dereference all symbolic links"),
        // // In task
        // groups::optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"),
        // // In main
        groups::optflag("0", "null", "end each output line with 0 byte rather than newline"),
        // In main
        groups::optflag("S", "separate-dirs", "do not include size of subdirectories"),
        // In main
        groups::optflag("s", "summarize", "display only a total for each argument"),
        // // In task
        // groups::optflag("x", "one-file-system", "skip directories on different file systems"),
        // // In task
        // groups::optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"),
        // // In task
        // groups::optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"),
        // In main
        groups::optopt("d", "max-depth", "print the total for a directory (or file, with --all)
            only if it is N or fewer levels below the command
            line argument;  --max-depth=0 is the same as --summarize", "N"),
        // In main
        groups::optflag("", "time", "show time of the last modification of any file in the
            directory, or any of its subdirectories"),
        // In main
        groups::optopt("", "time", "show time as WORD instead of modification time:
            atime, access, use, ctime or status", "WORD"),
        // In main
        groups::optopt("", "time-style", "show times using style STYLE:
            full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE"),
        groups::optflag("", "help", "display this help and exit"),
        groups::optflag("", "version", "output version information and exit"),
    ];

    let matches = match groups::getopts(args.tail(), opts) {
        Ok(m) => m,
        Err(f) => {
            writeln!(&mut stderr() as &mut Writer,
                "Invalid options\n{}", f.to_err_msg());
            os::set_exit_status(1);
            return
        }
    };

    if matches.opt_present("help") {
        println("du " + VERSION + " - estimate file space usage");
        println("");
        println("Usage:");
        println!("  {0:s} [OPTION]... [FILE]...", program);
        println!("  {0:s} [OPTION]... --files0-from=F", program);
        println("");
        println(groups::usage("Summarize disk usage of each FILE, recursively for directories.", opts));
        println("Display  values  are  in  units  of  the  first  available  SIZE from
--block-size,  and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environ‐
ment variables.  Otherwise, units default to  1024  bytes  (or  512  if
POSIXLY_CORRECT is set).

SIZE  is  an  integer and optional unit (example: 10M is 10*1024*1024).
Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ...  (pow‐
ers of 1000).");
        return
    }

    let options = Options{
        all: matches.opt_present("all"),
        max_depth: match (matches.opt_present("summarize"), matches.opt_str("max-depth")) {
            (true, Some(s)) => match from_str::<uint>(s) {
                Some(_) => {
                    println!("du: warning: summarizing conflicts with --max-depth={:s}", s);
                    return
                },
                None => {
                    println!("du: invalid maximum depth '{:s}'", s);
                    return
                }
            },
            (true, None) => Some(0),
            (false, Some(s)) => match from_str::<uint>(s) {
                Some(u) => Some(u),
                None => {
                    println!("du: invalid maximum depth '{:s}'", s);
                    return
                }
            },
            (false, None) => None
        },
        total: matches.opt_present("total"),
        separate_dirs: matches.opt_present("S"),
    };

    let strs = matches.free.clone();
    let strs = match strs.is_empty() {
        true => ~[~"./"],
        false => strs
    };

    let options_arc = Arc::new(options);

    let MB = match matches.opt_present("si") {
        true => 1000 * 1000,
        false => 1024 * 1024,
    };
    let KB = match matches.opt_present("si") {
        true => 1000,
        false => 1024,
    };

    let block_size = match matches.opt_str("block-size") {
        Some(s) => {
            let mut found_number = false;
            let mut found_letter = false;
            let mut numbers = ~[];
            let mut letters = ~[];
            for c in s.chars() {
                if found_letter && c.is_digit() || !found_number && !c.is_digit() {
                    println!("du: invalid --block-size argument '{}'", s);
                    return
                } else if c.is_digit() {
                    found_number = true;
                    numbers.push(c as u8);
                } else if c.is_alphabetic() {
                    found_letter = true;
                    letters.push(c);
                }
            }
            let number = std::uint::parse_bytes(numbers, 10).unwrap();
            let multiple = match std::str::from_chars(letters).as_slice() {
                "K" => 1024, "M" => 1024 * 1024, "G" => 1024 * 1024 * 1024,
                "T" => 1024 * 1024 * 1024 * 1024, "P" => 1024 * 1024 * 1024 * 1024 * 1024,
                "E" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
                "Z" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
                "Y" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
                "KB" => 1000, "MB" => 1000 * 1000, "GB" => 1000 * 1000 * 1000,
                "TB" => 1000 * 1000 * 1000 * 1000, "PB" => 1000 * 1000 * 1000 * 1000 * 1000,
                "EB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
                "ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
                "YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
                _ => {
                    println!("du: invalid --block-size argument '{}'", s); return
                }
            };
            number * multiple
        },
        None => 1024
    };

    let convert_size = |size: u64| -> ~str {
        if matches.opt_present("human-readable") || matches.opt_present("si") {
            if size > MB {
                format!("{:.1f}M", (size as f64) / (MB as f64))
            } else if size > KB {
                format!("{:.1f}K", (size as f64) / (KB as f64))
            } else {
                format!("{}B", size)
            }
        } else if matches.opt_present("k") {
            format!("{}", ((size as f64) / (KB as f64)).ceil())
        } else if matches.opt_present("m") {
            format!("{}", ((size as f64) / (MB as f64)).ceil())
        } else {
            format!("{}", ((size as f64) / (block_size as f64)).ceil())
        }
    };

    let time_format_str = match matches.opt_str("time-style") {
        Some(s) => {
            match s.as_slice() {
                "full-iso" => "%Y-%m-%d %H:%M:%S.%N %z",
                "long-iso" => "%Y-%m-%d %H:%M",
                "iso" => "%Y-%m-%d",
                _ => {
                    println("
du: invalid argument 'awdwa' for 'time style'
Valid arguments are:
- 'full-iso'
- 'long-iso'
- 'iso'
Try 'du --help' for more information.");
                    return
                }
            }
        },
        None => "%Y-%m-%d %H:%M"
    };

    let line_separator = match matches.opt_present("0") {
        true => "\0",
        false => "\n",
    };

    let mut grand_total = 0;
    for path_str in strs.iter() {
        let path = Path::new(path_str.clone());
        let iter = du(&path, options_arc.clone(), 0).move_iter();
        let (_, len) = iter.size_hint();
        let len = len.unwrap();
        for (index, stat_arc) in iter.enumerate() {
            let stat = stat_arc.get();
            let size = match matches.opt_present("apparent-size") {
                true => stat.unstable.nlink * stat.size,
                // C's stat is such that each block is assume to be 512 bytes
                // See: http://linux.die.net/man/2/stat
                false => stat.unstable.blocks * 512,
            };
            if matches.opt_present("time") {
                let time_str = {
                    let (secs, nsecs) = {
                        let time = match matches.opt_str("time") {
                            Some(s) => match s.as_slice() {
                                "accessed" => stat.accessed,
                                "created" => stat.created,
                                "modified" => stat.modified,
                                _ => {
                                    println("du: invalid argument 'modified' for '--time'
    Valid arguments are:
      - 'accessed', 'created', 'modified'
    Try 'du --help' for more information.");
                                    return
                                }
                            },
                            None => stat.modified
                        };
                        ((time / 1000) as i64, (time % 1000 * 1000) as i32)
                    };
                    let time_spec = Timespec::new(secs, nsecs);
                    extra::time::at(time_spec).strftime(time_format_str)
                };
                print!("{:<10} {:<30} {}", convert_size(size), time_str, stat.path.display());
            } else {
                print!("{:<10} {}", convert_size(size), stat.path.display());
            }
            print(line_separator);
            if options.total && index == (len - 1) {
                // The last element will be the total size of the the path under
                // path_str.  We add it to the grand total.
                grand_total += size;
            }
        }
    }

    if options.total {
        print!("{:<10} total", convert_size(grand_total));
        print(line_separator);
    }
}
<commit_msg>Fixing time format string<commit_after>#[link(name="du", vers="1.0.0", author="Derek Chiang")];

/*
 * This file is part of the uutils coreutils package.
 *
 * (c) Derek Chiang <derekchiang93@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

extern mod extra;

use std::os;
use std::io::stderr;
use std::io::fs;
use std::io::FileStat;
use std::option::Option;
use std::path::Path;
use extra::arc::Arc;
use extra::future::Future;
use extra::getopts::groups;
use extra::time::Timespec;

static VERSION: &'static str = "1.0.0";

struct Options {
    all: bool,
    max_depth: Option<uint>,
    total: bool,
    separate_dirs: bool,
}

fn du(path: &Path, options_arc: Arc<Options>, depth: uint) -> ~[Arc<FileStat>] {
    let mut stats = ~[];
    let mut futures = ~[];
    let options = options_arc.get();
    let mut my_stat = path.stat();

    for f in fs::readdir(path).move_iter() {
        match f.is_file() {
            true => {
                let stat = f.stat();
                my_stat.size += stat.size;
                my_stat.unstable.blocks += stat.unstable.blocks;
                if options.all {
                    stats.push(Arc::new(stat))
                }
            }
            false => {
                let oa_clone = options_arc.clone();
                futures.push(do Future::spawn { du(&f, oa_clone, depth + 1) })
            }
        }
    }

    for future in futures.mut_iter() {
        for stat_arc in future.get().move_rev_iter() {
            let stat = stat_arc.get();
            if !options.separate_dirs && stat.path.dir_path() == my_stat.path {
                my_stat.size += stat.size;
                my_stat.unstable.blocks += stat.unstable.blocks;
            }
            if options.max_depth == None || depth < options.max_depth.unwrap() {
                stats.push(stat_arc.clone());
            }
        }
    }

    stats.push(Arc::new(my_stat));

    return stats;
}

fn main() {
    let args = os::args();
    let program = args[0].clone();
    let opts = ~[
        // In task
        groups::optflag("a", "all", " write counts for all files, not just directories"),
        // In main
        groups::optflag("", "apparent-size", "print apparent sizes,  rather  than  disk  usage;
            although  the apparent  size is usually smaller, it may be larger due to holes
            in ('sparse') files, internal  fragmentation,  indirect  blocks, and the like"),
        // In main
        groups::optopt("B", "block-size", "scale sizes  by  SIZE before printing them.
            E.g., '-BM' prints sizes in units of 1,048,576 bytes.  See SIZE format below.",
            "SIZE"),
        // In main
        groups::optflag("b", "bytes", "equivalent to '--apparent-size --block-size=1'"),
        // In main
        groups::optflag("c", "total", "produce a grand total"),
        // In task
        // groups::optflag("D", "dereference-args", "dereference only symlinks that are listed
        //     on the command line"),
        // In main
        // groups::optopt("", "files0-from", "summarize disk usage of the NUL-terminated file
        //                   names specified in file F;
        //                   If F is - then read names from standard input", "F"),
        // // In task
        // groups::optflag("H", "", "equivalent to --dereference-args (-D)"),
        // In main
        groups::optflag("h", "human-readable", "print sizes in human readable format (e.g., 1K 234M 2G)"),
        // In main
        groups::optflag("", "si", "like -h, but use powers of 1000 not 1024"),
        // In main
        groups::optflag("k", "", "like --block-size=1K"),
        // In task
        groups::optflag("l", "count-links", "count sizes many times if hard linked"),
        // // In main
        groups::optflag("m", "", "like --block-size=1M"),
        // // In task
        // groups::optflag("L", "dereference", "dereference all symbolic links"),
        // // In task
        // groups::optflag("P", "no-dereference", "don't follow any symbolic links (this is the default)"),
        // // In main
        groups::optflag("0", "null", "end each output line with 0 byte rather than newline"),
        // In main
        groups::optflag("S", "separate-dirs", "do not include size of subdirectories"),
        // In main
        groups::optflag("s", "summarize", "display only a total for each argument"),
        // // In task
        // groups::optflag("x", "one-file-system", "skip directories on different file systems"),
        // // In task
        // groups::optopt("X", "exclude-from", "exclude files that match any pattern in FILE", "FILE"),
        // // In task
        // groups::optopt("", "exclude", "exclude files that match PATTERN", "PATTERN"),
        // In main
        groups::optopt("d", "max-depth", "print the total for a directory (or file, with --all)
            only if it is N or fewer levels below the command
            line argument;  --max-depth=0 is the same as --summarize", "N"),
        // In main
        groups::optflagopt("", "time", "show time of the last modification of any file in the
            directory, or any of its subdirectories.  If WORD is given, show time as WORD instead of modification time:
            atime, access, use, ctime or status", "WORD"),
        // In main
        groups::optopt("", "time-style", "show times using style STYLE:
            full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'", "STYLE"),
        groups::optflag("", "help", "display this help and exit"),
        groups::optflag("", "version", "output version information and exit"),
    ];

    let matches = match groups::getopts(args.tail(), opts) {
        Ok(m) => m,
        Err(f) => {
            writeln!(&mut stderr() as &mut Writer,
                "Invalid options\n{}", f.to_err_msg());
            os::set_exit_status(1);
            return
        }
    };

    if matches.opt_present("help") {
        println("du " + VERSION + " - estimate file space usage");
        println("");
        println("Usage:");
        println!("  {0:s} [OPTION]... [FILE]...", program);
        println!("  {0:s} [OPTION]... --files0-from=F", program);
        println("");
        println(groups::usage("Summarize disk usage of each FILE, recursively for directories.", opts));
        println("Display  values  are  in  units  of  the  first  available  SIZE from
--block-size,  and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environ‐
ment variables.  Otherwise, units default to  1024  bytes  (or  512  if
POSIXLY_CORRECT is set).

SIZE  is  an  integer and optional unit (example: 10M is 10*1024*1024).
Units are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ...  (pow‐
ers of 1000).");
        return
    }

    let options = Options{
        all: matches.opt_present("all"),
        max_depth: match (matches.opt_present("summarize"), matches.opt_str("max-depth")) {
            (true, Some(s)) => match from_str::<uint>(s) {
                Some(_) => {
                    println!("du: warning: summarizing conflicts with --max-depth={:s}", s);
                    return
                },
                None => {
                    println!("du: invalid maximum depth '{:s}'", s);
                    return
                }
            },
            (true, None) => Some(0),
            (false, Some(s)) => match from_str::<uint>(s) {
                Some(u) => Some(u),
                None => {
                    println!("du: invalid maximum depth '{:s}'", s);
                    return
                }
            },
            (false, None) => None
        },
        total: matches.opt_present("total"),
        separate_dirs: matches.opt_present("S"),
    };

    let strs = matches.free.clone();
    let strs = match strs.is_empty() {
        true => ~[~"./"],
        false => strs
    };

    let options_arc = Arc::new(options);

    let MB = match matches.opt_present("si") {
        true => 1000 * 1000,
        false => 1024 * 1024,
    };
    let KB = match matches.opt_present("si") {
        true => 1000,
        false => 1024,
    };

    let block_size = match matches.opt_str("block-size") {
        Some(s) => {
            let mut found_number = false;
            let mut found_letter = false;
            let mut numbers = ~[];
            let mut letters = ~[];
            for c in s.chars() {
                if found_letter && c.is_digit() || !found_number && !c.is_digit() {
                    println!("du: invalid --block-size argument '{}'", s);
                    return
                } else if c.is_digit() {
                    found_number = true;
                    numbers.push(c as u8);
                } else if c.is_alphabetic() {
                    found_letter = true;
                    letters.push(c);
                }
            }
            let number = std::uint::parse_bytes(numbers, 10).unwrap();
            let multiple = match std::str::from_chars(letters).as_slice() {
                "K" => 1024, "M" => 1024 * 1024, "G" => 1024 * 1024 * 1024,
                "T" => 1024 * 1024 * 1024 * 1024, "P" => 1024 * 1024 * 1024 * 1024 * 1024,
                "E" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
                "Z" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
                "Y" => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
                "KB" => 1000, "MB" => 1000 * 1000, "GB" => 1000 * 1000 * 1000,
                "TB" => 1000 * 1000 * 1000 * 1000, "PB" => 1000 * 1000 * 1000 * 1000 * 1000,
                "EB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
                "ZB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
                "YB" => 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000,
                _ => {
                    println!("du: invalid --block-size argument '{}'", s); return
                }
            };
            number * multiple
        },
        None => 1024
    };

    let convert_size = |size: u64| -> ~str {
        if matches.opt_present("human-readable") || matches.opt_present("si") {
            if size > MB {
                format!("{:.1f}M", (size as f64) / (MB as f64))
            } else if size > KB {
                format!("{:.1f}K", (size as f64) / (KB as f64))
            } else {
                format!("{}B", size)
            }
        } else if matches.opt_present("k") {
            format!("{}", ((size as f64) / (KB as f64)).ceil())
        } else if matches.opt_present("m") {
            format!("{}", ((size as f64) / (MB as f64)).ceil())
        } else {
            format!("{}", ((size as f64) / (block_size as f64)).ceil())
        }
    };

    let time_format_str = match matches.opt_str("time-style") {
        Some(s) => {
            match s.as_slice() {
                "full-iso" => "%Y-%m-%d %H:%M:%S.%f %z",
                "long-iso" => "%Y-%m-%d %H:%M",
                "iso" => "%Y-%m-%d",
                _ => {
                    println("
du: invalid argument 'awdwa' for 'time style'
Valid arguments are:
- 'full-iso'
- 'long-iso'
- 'iso'
Try 'du --help' for more information.");
                    return
                }
            }
        },
        None => "%Y-%m-%d %H:%M"
    };

    let line_separator = match matches.opt_present("0") {
        true => "\0",
        false => "\n",
    };

    let mut grand_total = 0;
    for path_str in strs.iter() {
        let path = Path::new(path_str.clone());
        let iter = du(&path, options_arc.clone(), 0).move_iter();
        let (_, len) = iter.size_hint();
        let len = len.unwrap();
        for (index, stat_arc) in iter.enumerate() {
            let stat = stat_arc.get();
            let size = match matches.opt_present("apparent-size") {
                true => stat.unstable.nlink * stat.size,
                // C's stat is such that each block is assume to be 512 bytes
                // See: http://linux.die.net/man/2/stat
                false => stat.unstable.blocks * 512,
            };
            if matches.opt_present("time") {
                let time_str = {
                    let (secs, nsecs) = {
                        let time = match matches.opt_str("time") {
                            Some(s) => match s.as_slice() {
                                "accessed" => stat.accessed,
                                "created" => stat.created,
                                "modified" => stat.modified,
                                _ => {
                                    println("du: invalid argument 'modified' for '--time'
    Valid arguments are:
      - 'accessed', 'created', 'modified'
    Try 'du --help' for more information.");
                                    return
                                }
                            },
                            None => stat.modified
                        };
                        ((time / 1000) as i64, (time % 1000 * 1000000) as i32)
                    };
                    let time_spec = Timespec::new(secs, nsecs);
                    extra::time::at(time_spec).strftime(time_format_str)
                };
                print!("{:<10} {:<30} {}", convert_size(size), time_str, stat.path.display());
            } else {
                print!("{:<10} {}", convert_size(size), stat.path.display());
            }
            print(line_separator);
            if options.total && index == (len - 1) {
                // The last element will be the total size of the the path under
                // path_str.  We add it to the grand total.
                grand_total += size;
            }
        }
    }

    if options.total {
        print!("{:<10} total", convert_size(grand_total));
        print(line_separator);
    }
}
<|endoftext|>


<commit_before>use std::hash::Hash;
use std::path::{PathBuf, Path};

use perlin_core::index::Index;
use perlin_core::index::vocabulary::SharedVocabulary;
use perlin_core::index::posting::{Posting, DocId};
use perlin_core::page_manager::{RamPageCache, FsPageManager};

use language::CanApply;

use field::{FieldDefinition, Field, FieldId};

type Pipeline<T, Out> = Box<Fn(DocId, FieldId) -> Box<for<'r> CanApply<&'r str, T, Output=Out>>>;

/// `DocumentIndex` takes some of the basic building blocks in `perlin_core`
/// and provides an abstraction that can be used to index and query documents
/// using fields, metadata, taxonomies etc
pub struct DocumentIndex<TContainer, TPipelineContainer> {
    // We need to overwrite perlin_core's default DocIds as some Documents might contain
    // other fields than others. This counter acts as DocumentIndex global document counter.
    doc_id_counter: DocId,
    // The base path of this index.
    base_path: PathBuf,
    index_container: TContainer,
    pipeline_container: TPipelineContainer
}

pub trait IndexContainer<T: Hash + Eq> {
    fn manage_index(&mut self, FieldDefinition, Index<T>);
}

pub trait PipelineContainer<TContainer, TOutput> {
    fn manage_pipeline(&mut self, FieldDefinition, Pipeline<TContainer, TOutput>);
    fn apply_pipeline(&mut self, &mut TContainer, FieldDefinition, DocId, &str);
}

pub trait Commitable {
    fn commit(&mut self);
}

pub trait TermIndexer<T> {
    fn index_term(&mut self, FieldId, DocId, T);
}

pub trait TermQuerier<T> {
    fn query_term(&self, &Field<T>) -> Vec<Posting>;
}

impl<TContainer: Default + Commitable, TPipelineContainer: Default> DocumentIndex<TContainer, TPipelineContainer> {
    /// Create a new index.
    pub fn new(path: &Path) -> Self {
        DocumentIndex {
            doc_id_counter: DocId::none(),
            base_path: path.to_path_buf(),
            index_container: TContainer::default(),
            pipeline_container: TPipelineContainer::default(),
        }
    }

    pub fn add_field<TTerm: Hash + Eq + Ord>(&mut self,
                                             field_def: FieldDefinition,
                                             pipeline: Pipeline<TContainer, TTerm>)
                                             -> Result<(), ()>
        where TContainer: IndexContainer<TTerm>,
              TPipelineContainer: PipelineContainer<TContainer, TTerm>
    {
        let FieldDefinition(field_id, _) = field_def;
        let page_cache = RamPageCache::new(FsPageManager::new(&self.base_path
            .join(format!("{}_pages.bin", field_id.0).to_string())));
        self.index_container.manage_index(field_def,
                                          Index::<TTerm>::new(page_cache, SharedVocabulary::new()));
        self.pipeline_container.manage_pipeline(field_def,
                                                pipeline);
        Ok(())
    }

    /// Indexes a field
    pub fn index_field<TTerm>(&mut self, doc_id: DocId, field: FieldDefinition, content: &str)
        where TPipelineContainer: PipelineContainer<TContainer, TTerm>
    {
        self.pipeline_container.apply_pipeline(&mut self.index_container,field, doc_id, content);
    }

    pub fn get_next_doc_id(&mut self) -> DocId {
        self.doc_id_counter.inc();
        self.doc_id_counter
    }

    /// Commits this index
    pub fn commit(&mut self) {
        self.index_container.commit();
    }

    /// Runs an atom_query on a certain field
    pub fn query_field<TTerm: Hash + Eq>(&self, query: &Field<TTerm>) -> Vec<DocId>
        where TContainer: TermQuerier<TTerm>
    {
        self.index_container
            .query_term(query)
            .into_iter()
            .map(|posting| posting.doc_id())
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use rust_stemmers;

    use test_utils::create_test_dir;
    use document::DocumentBuilder;
    use field::{FieldDefinition, FieldType, FieldId, Field};
    use language::*;
    use language::integers::*;

    use std::collections::BTreeMap;

    use perlin_core::index::vocabulary::SharedVocabulary;
    use perlin_core::index::posting::{Posting, DocId};
    use perlin_core::index::Index;

    use std::borrow::Cow;

    struct TestPipelineContainer {
        text_pipelines: BTreeMap<FieldId, Pipeline<TestContainer, String>>,
        num_pipelines: BTreeMap<FieldId, Pipeline<TestContainer, u64>>
    }

    impl Default for TestPipelineContainer {
        fn default() -> Self {
            TestPipelineContainer {
                text_pipelines: BTreeMap::new(),
                num_pipelines: BTreeMap::new()
            }
        }
    }

    impl PipelineContainer<TestContainer, String> for TestPipelineContainer {
        fn manage_pipeline(&mut self, def: FieldDefinition, pipe: Pipeline<TestContainer, String>) {
            self.text_pipelines.insert(def.0, pipe);
        }

        fn apply_pipeline(&mut self, cont: &mut TestContainer, def: FieldDefinition, doc_id: DocId, input: &str) {
            if let Some(pipe) = self.text_pipelines.get(&def.0) {
                pipe(doc_id, def.0).apply(input, cont);
            }
        }
    }

    impl PipelineContainer<TestContainer, u64> for TestPipelineContainer {
        fn manage_pipeline(&mut self, def: FieldDefinition, pipe: Pipeline<TestContainer, u64>) {
            self.num_pipelines.insert(def.0, pipe);
        }

        fn apply_pipeline(&mut self, cont: &mut TestContainer, def: FieldDefinition, doc_id: DocId, input: &str) {
            if let Some(pipe) = self.num_pipelines.get(&def.0) {
                pipe(doc_id, def.0).apply(input, cont);
            }
        }
    }

    struct TestContainer {
        text_fields: BTreeMap<FieldId, Index<String>>,
        num_fields: BTreeMap<FieldId, Index<u64>>,
    }

    impl IndexContainer<String> for TestContainer {
        fn manage_index(&mut self, def: FieldDefinition, index: Index<String>) {
            self.text_fields.insert(def.0, index);
        }
    }

    impl IndexContainer<u64> for TestContainer {
        fn manage_index(&mut self, def: FieldDefinition, index: Index<u64>) {
            self.num_fields.insert(def.0, index);
        }
    }

    impl TermIndexer<String> for TestContainer {
        fn index_term(&mut self, field: FieldId, doc_id: DocId, term: String) {
            if let Some(index) = self.text_fields.get_mut(&field) {
                index.index_term(term, doc_id);
            }
        }
    }

    impl TermIndexer<u64> for TestContainer {
        fn index_term(&mut self, field: FieldId, doc_id: DocId, term: u64) {
            if let Some(index) = self.num_fields.get_mut(&field) {
                index.index_term(term, doc_id);
            }
        }
    }

    impl TermQuerier<String> for TestContainer {
        fn query_term(&self, field: &Field<String>) -> Vec<Posting> {
            if let Some(index) = self.text_fields.get(&(field.0).0) {
                index.query_atom(&field.1)
            } else {
                panic!();
            }
        }
    }

    impl TermQuerier<u64> for TestContainer {
        fn query_term(&self, field: &Field<u64>) -> Vec<Posting> {
            if let Some(index) = self.num_fields.get(&(field.0).0) {
                index.query_atom(&field.1)
            } else {
                panic!();
            }
        }
    }

    impl Commitable for TestContainer {
        fn commit(&mut self) {
            self.text_fields.values_mut().map(|index| index.commit()).count();
            self.num_fields.values_mut().map(|index| index.commit()).count();
        }
    }

    impl Default for TestContainer {
        fn default() -> Self {
            TestContainer {
                text_fields: BTreeMap::new(),
                num_fields: BTreeMap::new(),
            }
        }
    }

    fn new_index(name: &str) -> DocumentIndex<TestContainer, TestPipelineContainer> {
        DocumentIndex::new(&create_test_dir(format!("perlin_index/{}", name).as_str()))
    }

    #[test]
    fn pipeline_mode() {
        let mut index = new_index("pipeline_mode");
        let text_field_def = FieldDefinition(FieldId(0), FieldType::Text);
        index.add_field::<String>(text_field_def,
                                  pipeline!( WhitespaceTokenizer
                                             > LowercaseFilter
                                             > Stemmer(rust_stemmers::Algorithm::English)));

        index.index_field::<String>(DocId(0), text_field_def, "this is a TEST");
        index.index_field::<String>(DocId(1), text_field_def, "THIS is a title");
        index.commit();
        assert_eq!(index.query_field(&Field(text_field_def, "test".to_owned())),
                   vec![DocId(0)]);
        assert_eq!(index.query_field(&Field(text_field_def, "titl".to_owned() /*Because it is stemmed!*/)),
                   vec![DocId(1)]);
        assert_eq!(index.query_field(&Field(text_field_def, "this".to_owned())),
                   vec![DocId(0), DocId(1)]);
    }

    #[test]
    fn different_pipelines() {
        let mut index = new_index("different_pipelines");
        let text_def1 = FieldDefinition(FieldId(0), FieldType::Text);
        let text_def2 = FieldDefinition(FieldId(1), FieldType::Text);
        index.add_field::<String>(text_def1,
                                  pipeline!( WhitespaceTokenizer
                                             > LowercaseFilter
                                             > Stemmer(rust_stemmers::Algorithm::English)));
        index.add_field::<String>(text_def2,
                                  pipeline!( WhitespaceTokenizer > LowercaseFilter ));
        index.index_field::<String>(DocId(0), text_def1, "THIS is a title");
        index.index_field::<String>(DocId(1), text_def1, "this is a titles");
        index.index_field::<String>(DocId(0), text_def2, "THIS is a title");
        index.index_field::<String>(DocId(1), text_def2, "this is a titles");
        index.commit();
        assert_eq!(index.query_field(&Field(text_def1, "titl".to_owned())),
                   vec![DocId(0), DocId(1)]);
        assert_eq!(index.query_field(&Field(text_def2, "title".to_owned())),
                   vec![DocId(0)]);
    }

    #[test]
    fn multityped_pipeline() {
        let mut index = new_index("multityped_pipelines");
        let text_def1 = FieldDefinition(FieldId(0), FieldType::Text);
        let num_def2 = FieldDefinition(FieldId(1), FieldType::Number);
        index.add_field::<String>(text_def1,
                                  pipeline!( WhitespaceTokenizer
                                             > NumberFilter
                                             | [FieldId(1)]
                                             > LowercaseFilter ));
        index.add_field::<u64>(num_def2, pipeline!( ToU64 ));
        index.index_field::<String>(DocId(0), text_def1, "These are 10 tests");
        index.index_field::<String>(DocId(1), text_def1, "this is 1 single title");
        index.commit();
        assert_eq!(index.query_field(&Field(text_def1, "this".to_owned())),
                   vec![DocId(1)]);
        assert_eq!(index.query_field(&Field(num_def2, 10)),
                   vec![DocId(0)]);
    }

    // #[test]
    // fn one_document() {
    //     let mut index = new_index("one_document");
    //     let text_field_def = FieldDefinition(FieldId(0), FieldType::Text);
    //     // Add a field
    //     index.add_field::<String>(text_field_def);
    //     // Index a new documnet
    //     index.index_field(DocId(0), Field(text_field_def, "title".to_string()));
    //     // Commit the index.
    //     index.commit();
    // assert_eq!(index.query_field(&Field(text_field_def,
    // "title".to_string())),
    //                vec![DocId(0)]);
    // }

    // #[test]
    // fn multiple_documents() {
    //     let mut index = new_index("multiple_documents");
    //     let text_field_def = FieldDefinition(FieldId(0), FieldType::Text);
    //     // Add a field
    //     index.add_field::<String>(FieldDefinition(FieldId(0), FieldType::Text));
    //     // Index a new documnet
    // index.index_field(DocId(0), Field(text_field_def, "This is a test
    // title"));
    // index.index_field(DocId(1), Field(text_field_def, "This is a test
    // text"));

    //     // Commit the index.
    //     index.commit();
    //     assert_eq!(index.query_field(&Field(text_field_def, "title")),
    //                vec![DocId(0)]);
    //     assert_eq!(index.query_field(&Field(text_field_def, "test")),
    //                vec![DocId(0), DocId(1)]);
    // }

    // #[test]
    // fn multiple_fields() {
    //     let mut index = new_index("multiple_fields");
    //     let text_field0 = FieldDefinition(FieldId(0), FieldType::Text);
    //     let text_field1 = FieldDefinition(FieldId(1), FieldType::Text);
    //     index.add_field::<String>(text_field0);
    //     index.add_field::<String>(text_field1);

    //     index.index_field(DocId(0), Field(text_field0, "This is a test title"));
    // index.index_field(DocId(0), Field(text_field1, "This is a test
    // content"));

    //     index.index_field(DocId(1), Field(text_field0, "This is a test title"));
    // index.index_field(DocId(1), Field(text_field1, "This is a test
    // content"));

    //     index.commit();
    //     assert_eq!(index.query_field(&Field(text_field0, "content")), vec![]);
    //     assert_eq!(index.query_field(&Field(text_field0, "title")),
    //                vec![DocId(0), DocId(1)]);
    //     assert_eq!(index.query_field(&Field(text_field1, "content")),
    //                vec![DocId(0), DocId(1)]);
    //     assert_eq!(index.query_field(&Field(text_field1, "title")), vec![])
    // }

    // #[test]
    // fn multiple_fieldtypes() {
    //     let mut index = new_index("multiple_fieldtypes");
    //     let text_field0 = FieldDefinition(FieldId(0), FieldType::Text);
    //     let text_field1 = FieldDefinition(FieldId(1), FieldType::Text);
    //     // Planet Number in solar system
    //     let num_field2 = FieldDefinition(FieldId(2), FieldType::Number);
    //     // Object type. 1=star 2=planet 3=moon
    //     let num_field3 = FieldDefinition(FieldId(3), FieldType::Number);
    //     index.add_field::<String>(text_field0);
    //     index.add_field::<String>(text_field1);
    //     index.add_field::<u64>(num_field2);
    //     index.add_field::<u64>(num_field3);
    //     // Mars
    //     index.index_field(DocId(0), Field(text_field0, "Mars"));
    //     index.index_field(DocId(0),
    //                       Field(text_field1,
    // "Mars is the fourth planet from the Sun and the
    // second-smallest \
    //                              planet in the Solar System, after Mercury."));
    //     index.index_field(DocId(0), Field(num_field2, 4));
    //     index.index_field(DocId(0), Field(num_field3, 2));
    //     // Sun
    //     index.index_field(DocId(1), Field(text_field0, "Sun"));
    //     index.index_field(DocId(1),
    //                       Field(text_field1,
    // "The Sun is the star at the center of the Solar
    // System."));
    //     index.index_field(DocId(1), Field(num_field3, 1));

    //     // Moon

    //     index.index_field(DocId(2), Field(text_field0, "Moon"));
    //     index.index_field(DocId(2),
    //                       Field(text_field1,
    // "The Moon is an astronomical body that orbits
    // planet Earth, \
    // being Earth's only permanent natural
    // satellite."));
    //     index.index_field(DocId(2), Field(num_field3, 3));

    //     index.commit();
    //     assert_eq!(index.query_field(&Field(text_field0, "Moon")),
    //                vec![DocId(2)]);
    //     assert_eq!(index.query_field(&Field(num_field2, 4)), vec![DocId(0)]);
    //     assert_eq!(index.query_field(&Field(num_field3, 1)), vec![DocId(1)]);
    //     assert_eq!(index.query_field(&Field(text_field0, "is")), vec![]);
    //     assert_eq!(index.query_field(&Field(text_field1, "is")),
    //                vec![DocId(0), DocId(1), DocId(2)]);

    // }
}
<commit_msg>little cleanups<commit_after>use std::hash::Hash;
use std::path::{PathBuf, Path};

use perlin_core::index::Index;
use perlin_core::index::vocabulary::SharedVocabulary;
use perlin_core::index::posting::{Posting, DocId};
use perlin_core::page_manager::{RamPageCache, FsPageManager};

use language::CanApply;

use field::{FieldDefinition, Field, FieldId};

type Pipeline<T, Out> = Box<Fn(DocId, FieldId) -> Box<for<'r> CanApply<&'r str, T, Output=Out>>>;

/// `DocumentIndex` takes some of the basic building blocks in `perlin_core`
/// and provides an abstraction that can be used to index and query documents
/// using fields, metadata, taxonomies etc
pub struct DocumentIndex<TContainer, TPipelineContainer> {
    // We need to overwrite perlin_core's default DocIds as some Documents might contain
    // other fields than others. This counter acts as DocumentIndex global document counter.
    doc_id_counter: DocId,
    // The base path of this index.
    base_path: PathBuf,
    index_container: TContainer,
    pipeline_container: TPipelineContainer
}

pub trait IndexContainer<T: Hash + Eq> {
    fn manage_index(&mut self, FieldDefinition, Index<T>);
}

pub trait PipelineContainer<TContainer, TOutput> {
    fn manage_pipeline(&mut self, FieldDefinition, Pipeline<TContainer, TOutput>);
    fn apply_pipeline(&mut self, &mut TContainer, FieldDefinition, DocId, &str);
}

pub trait Commitable {
    fn commit(&mut self);
}

pub trait TermIndexer<T> {
    fn index_term(&mut self, FieldId, DocId, T);
}

pub trait TermQuerier<T> {
    fn query_term(&self, &Field<T>) -> Vec<Posting>;
}

impl<TContainer: Default + Commitable, TPipelineContainer: Default> DocumentIndex<TContainer, TPipelineContainer> {
    /// Create a new index.
    pub fn new(path: &Path) -> Self {
        DocumentIndex {
            doc_id_counter: DocId::none(),
            base_path: path.to_path_buf(),
            index_container: TContainer::default(),
            pipeline_container: TPipelineContainer::default(),
        }
    }

    pub fn add_field<TTerm: Hash + Eq + Ord>(&mut self,
                                             field_def: FieldDefinition,
                                             pipeline: Pipeline<TContainer, TTerm>)
                                             -> Result<(), ()>
        where TContainer: IndexContainer<TTerm>,
              TPipelineContainer: PipelineContainer<TContainer, TTerm>
    {
        let FieldDefinition(field_id, _) = field_def;
        let page_cache = RamPageCache::new(FsPageManager::new(&self.base_path
            .join(format!("{}_pages.bin", field_id.0).to_string())));
        self.index_container.manage_index(field_def,
                                          Index::<TTerm>::new(page_cache, SharedVocabulary::new()));
        self.pipeline_container.manage_pipeline(field_def,
                                                pipeline);
        Ok(())
    }

    /// Indexes a field
    pub fn index_field<TTerm>(&mut self, doc_id: DocId, field: FieldDefinition, content: &str)
        where TPipelineContainer: PipelineContainer<TContainer, TTerm>
    {
        self.pipeline_container.apply_pipeline(&mut self.index_container,field, doc_id, content);
    }

    pub fn get_next_doc_id(&mut self) -> DocId {
        self.doc_id_counter.inc();
        self.doc_id_counter
    }

    /// Commits this index
    pub fn commit(&mut self) {
        self.index_container.commit();
    }

    /// Runs an atom_query on a certain field
    pub fn query_field<TTerm: Hash + Eq>(&self, query: &Field<TTerm>) -> Vec<DocId>
        where TContainer: TermQuerier<TTerm>
    {
        self.index_container
            .query_term(query)
            .into_iter()
            .map(|posting| posting.doc_id())
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use rust_stemmers;

    use test_utils::create_test_dir;
    use field::{FieldDefinition, FieldType, FieldId, Field};
    use language::*;
    use language::integers::*;

    use std::collections::BTreeMap;

    use perlin_core::index::posting::{Posting, DocId};
    use perlin_core::index::Index;

    struct TestPipelineContainer {
        text_pipelines: BTreeMap<FieldId, Pipeline<TestContainer, String>>,
        num_pipelines: BTreeMap<FieldId, Pipeline<TestContainer, u64>>
    }

    impl Default for TestPipelineContainer {
        fn default() -> Self {
            TestPipelineContainer {
                text_pipelines: BTreeMap::new(),
                num_pipelines: BTreeMap::new()
            }
        }
    }

    impl PipelineContainer<TestContainer, String> for TestPipelineContainer {
        fn manage_pipeline(&mut self, def: FieldDefinition, pipe: Pipeline<TestContainer, String>) {
            self.text_pipelines.insert(def.0, pipe);
        }

        fn apply_pipeline(&mut self, cont: &mut TestContainer, def: FieldDefinition, doc_id: DocId, input: &str) {
            if let Some(pipe) = self.text_pipelines.get(&def.0) {
                pipe(doc_id, def.0).apply(input, cont);
            }
        }
    }

    impl PipelineContainer<TestContainer, u64> for TestPipelineContainer {
        fn manage_pipeline(&mut self, def: FieldDefinition, pipe: Pipeline<TestContainer, u64>) {
            self.num_pipelines.insert(def.0, pipe);
        }

        fn apply_pipeline(&mut self, cont: &mut TestContainer, def: FieldDefinition, doc_id: DocId, input: &str) {
            if let Some(pipe) = self.num_pipelines.get(&def.0) {
                pipe(doc_id, def.0).apply(input, cont);
            }
        }
    }

    struct TestContainer {
        text_fields: BTreeMap<FieldId, Index<String>>,
        num_fields: BTreeMap<FieldId, Index<u64>>,
    }

    impl IndexContainer<String> for TestContainer {
        fn manage_index(&mut self, def: FieldDefinition, index: Index<String>) {
            self.text_fields.insert(def.0, index);
        }
    }

    impl IndexContainer<u64> for TestContainer {
        fn manage_index(&mut self, def: FieldDefinition, index: Index<u64>) {
            self.num_fields.insert(def.0, index);
        }
    }

    impl TermIndexer<String> for TestContainer {
        fn index_term(&mut self, field: FieldId, doc_id: DocId, term: String) {
            if let Some(index) = self.text_fields.get_mut(&field) {
                index.index_term(term, doc_id);
            }
        }
    }

    impl TermIndexer<u64> for TestContainer {
        fn index_term(&mut self, field: FieldId, doc_id: DocId, term: u64) {
            if let Some(index) = self.num_fields.get_mut(&field) {
                index.index_term(term, doc_id);
            }
        }
    }

    impl TermQuerier<String> for TestContainer {
        fn query_term(&self, field: &Field<String>) -> Vec<Posting> {
            if let Some(index) = self.text_fields.get(&(field.0).0) {
                index.query_atom(&field.1)
            } else {
                panic!();
            }
        }
    }

    impl TermQuerier<u64> for TestContainer {
        fn query_term(&self, field: &Field<u64>) -> Vec<Posting> {
            if let Some(index) = self.num_fields.get(&(field.0).0) {
                index.query_atom(&field.1)
            } else {
                panic!();
            }
        }
    }

    impl Commitable for TestContainer {
        fn commit(&mut self) {
            self.text_fields.values_mut().map(|index| index.commit()).count();
            self.num_fields.values_mut().map(|index| index.commit()).count();
        }
    }

    impl Default for TestContainer {
        fn default() -> Self {
            TestContainer {
                text_fields: BTreeMap::new(),
                num_fields: BTreeMap::new(),
            }
        }
    }

    fn new_index(name: &str) -> DocumentIndex<TestContainer, TestPipelineContainer> {
        DocumentIndex::new(&create_test_dir(format!("perlin_index/{}", name).as_str()))
    }

    #[test]
    fn pipeline_mode() {
        let mut index = new_index("pipeline_mode");
        let text_field_def = FieldDefinition(FieldId(0), FieldType::Text);
        index.add_field::<String>(text_field_def,
                                  pipeline!( WhitespaceTokenizer
                                             > LowercaseFilter
                                             > Stemmer(rust_stemmers::Algorithm::English)));

        index.index_field::<String>(DocId(0), text_field_def, "this is a TEST");
        index.index_field::<String>(DocId(1), text_field_def, "THIS is a title");
        index.commit();
        assert_eq!(index.query_field(&Field(text_field_def, "test".to_owned())),
                   vec![DocId(0)]);
        assert_eq!(index.query_field(&Field(text_field_def, "titl".to_owned() /*Because it is stemmed!*/)),
                   vec![DocId(1)]);
        assert_eq!(index.query_field(&Field(text_field_def, "this".to_owned())),
                   vec![DocId(0), DocId(1)]);
    }

    #[test]
    fn different_pipelines() {
        let mut index = new_index("different_pipelines");
        let text_def1 = FieldDefinition(FieldId(0), FieldType::Text);
        let text_def2 = FieldDefinition(FieldId(1), FieldType::Text);
        index.add_field::<String>(text_def1,
                                  pipeline!( WhitespaceTokenizer
                                             > LowercaseFilter
                                             > Stemmer(rust_stemmers::Algorithm::English)));
        index.add_field::<String>(text_def2,
                                  pipeline!( WhitespaceTokenizer > LowercaseFilter ));
        index.index_field::<String>(DocId(0), text_def1, "THIS is a title");
        index.index_field::<String>(DocId(1), text_def1, "this is a titles");
        index.index_field::<String>(DocId(0), text_def2, "THIS is a title");
        index.index_field::<String>(DocId(1), text_def2, "this is a titles");
        index.commit();
        assert_eq!(index.query_field(&Field(text_def1, "titl".to_owned())),
                   vec![DocId(0), DocId(1)]);
        assert_eq!(index.query_field(&Field(text_def2, "title".to_owned())),
                   vec![DocId(0)]);
    }

    #[test]
    fn multityped_pipeline() {
        let mut index = new_index("multityped_pipelines");
        let text_def1 = FieldDefinition(FieldId(0), FieldType::Text);
        let num_def2 = FieldDefinition(FieldId(1), FieldType::Number);
        index.add_field::<String>(text_def1,
                                  pipeline!( WhitespaceTokenizer
                                             > NumberFilter
                                             | [FieldId(1)]
                                             > LowercaseFilter )).unwrap();
        index.add_field::<u64>(num_def2, pipeline!( ToU64 )).unwrap();
        index.index_field::<String>(DocId(0), text_def1, "These are 10 tests");
        index.index_field::<String>(DocId(1), text_def1, "this is 1 single title");
        index.index_field::<u64>(DocId(2), num_def2, "15");
        index.commit();
        assert_eq!(index.query_field(&Field(text_def1, "this".to_owned())),
                   vec![DocId(1)]);
        assert_eq!(index.query_field(&Field(text_def1, "10".to_owned())),
                   vec![]);
        assert_eq!(index.query_field(&Field(num_def2, 10)),
                   vec![DocId(0)]);
        assert_eq!(index.query_field(&Field(num_def2, 15)),
                   vec![DocId(2)]);
    }
}
<|endoftext|>


<commit_before>extern crate cql_bindgen;

use std::mem;
use std::ffi::CString;

use cql_bindgen::*;

fn print_error(future: &mut CassFuture) {
    unsafe {
        let mut message = mem::zeroed();
        let mut message_length = mem::zeroed();
        cass_future_error_message(future, &mut message, &mut message_length);
        println!("Error: {:?}", raw2utf8(message,message_length));
    }
}

fn create_cluster() -> *mut CassCluster {
    unsafe {
        let cluster = cass_cluster_new();
        let host = CString::new("127.0.0.1").unwrap();
        cass_cluster_set_contact_points(cluster, host.as_ptr());
        cluster
    }
}

fn connect_session(session: &mut CassSession, cluster: &CassCluster) -> CassError {
    unsafe {
        let future = cass_session_connect(session, cluster);
        cass_future_wait(future);
        let err = cass_future_error_code(future);
        cass_future_free(future);
        err
    }
}

fn execute_query(session: &mut CassSession, query: &str) -> Result<(), CassError> {
    unsafe {
        let query = CString::new(query).unwrap();
        let statement = cass_statement_new(query.as_ptr(), 0);
        let future = &mut*cass_session_execute(session, statement);
        cass_future_wait(future);
        cass_future_error_code(future);
        let result = match cass_future_error_code(future) {
            CASS_OK => Ok(()),
            rc => {
                print_error(future);
                Err(rc)
            }
        };
        cass_future_free(future);
        cass_statement_free(statement);
        result
    }
}

fn insert_into_tuple(session: &mut CassSession, uuid_gen: &mut CassUuidGen) -> Result<(), CassError> {
    unsafe {
        let mut id = mem::zeroed();
        let mut id_str: [i8; 37] = mem::zeroed();

        let query = CString::new("INSERT INTO examples.tuples (id, item) VALUES (?, ?)").unwrap();

        let statement = &mut*cass_statement_new(query.as_ptr(), 2);
        cass_uuid_gen_time(uuid_gen, &mut id);
        cass_uuid_string(id, id_str[..].as_mut_ptr());

        let item = cass_tuple_new(2);

        cass_tuple_set_string(item, 0, id_str[..].as_mut_ptr());
        cass_tuple_set_int64(item, 1, id.time_and_version as i64);

        cass_statement_bind_uuid(statement, 0, id);
        cass_statement_bind_tuple(statement, 1, item);

        let future = &mut*cass_session_execute(session, statement);
        cass_future_wait(future);

        let rc = cass_future_error_code(future);
        if rc != CASS_OK {
            print_error(future);
        }

        cass_future_free(future);
        cass_statement_free(statement);
        cass_tuple_free(item);

        Ok(())
    }
}

fn select_from_tuple(session: &mut CassSession) -> Result<(), CassError> {
    unsafe {
        let query = CString::new("SELECT * FROM examples.tuples").unwrap();

        let statement = cass_statement_new(query.as_ptr(), 0);

        let future = &mut*cass_session_execute(session, statement);
        cass_future_wait(future);

        match cass_future_error_code(future) {
            CASS_OK => {
                let result = cass_future_get_result(future);
                let rows = cass_iterator_from_result(result);

                while cass_iterator_next(rows) > 0 {
                    let mut id = mem::zeroed();
                    let mut id_str = mem::zeroed();
                    let row = cass_iterator_get_row(rows);
                    let id_value = cass_row_get_column_by_name(row, CString::new("id").unwrap().as_ptr());
                    let item_value = cass_row_get_column_by_name(row, CString::new("item").unwrap().as_ptr());
                    let item = cass_iterator_from_tuple(item_value);

                    cass_value_get_uuid(id_value, &mut id);
                    cass_uuid_string(id, &mut id_str);

                    print!("id {:?} ", &id_str);

                    while cass_iterator_next(item) > 0 {
                        let value = cass_iterator_get_value(item);

                        match cass_value_is_null(value) == 0 {
                            true => {
                                match cass_value_type(value) {
                                    CASS_VALUE_TYPE_VARCHAR => {
                                        let mut text = mem::zeroed();
                                        let mut text_length = mem::zeroed();
                                        cass_value_get_string(value, &mut text, &mut text_length);
                                        print!("{:?} ", raw2utf8(text,text_length));
                                    }
                                    CASS_VALUE_TYPE_BIGINT => {
                                        let mut i = mem::zeroed();
                                        cass_value_get_int64(value, &mut i);
                                        print!("{:?} ", i);
                                    }
                                    other_type => {
                                        print!("<invalid type {:?}> ", other_type);
                                    }
                                }

                            }
                            false => {
                                print!("<null> ");
                            }
                        }

                    }
                    println!("");
                }
                cass_result_free(result);
                cass_iterator_free(rows);
            }
            _ => {
                print_error(future);
            }
        }
        cass_future_free(future);
        cass_statement_free(statement);
        Ok(())
    }
}

fn main() {
    unsafe {
        let cluster = &mut*create_cluster();
        let mut session = &mut*cass_session_new();

        let uuid_gen = &mut*cass_uuid_gen_new();

        if connect_session(session, cluster) != CASS_OK {
            cass_cluster_free(cluster);
            cass_session_free(session);
            panic!();
        }

        execute_query(session, "CREATE KEYSPACE IF NOT EXISTS examples WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' }").unwrap();

        execute_query(session, "CREATE TABLE IF NOT EXISTS examples.tuples (id timeuuid, item frozen<tuple<text, bigint>>, PRIMARY KEY(id))").unwrap();

        insert_into_tuple(session, uuid_gen).unwrap();
        select_from_tuple(session).unwrap();

        let close_future = cass_session_close(session);
        cass_future_wait(close_future);
        cass_future_free(close_future);

        cass_cluster_free(cluster);
        cass_session_free(session);

        cass_uuid_gen_free(uuid_gen);
    }
}
<commit_msg>whitespace<commit_after>extern crate cql_bindgen;

use std::mem;
use std::ffi::CString;

use cql_bindgen::*;

fn print_error(future: &mut CassFuture) {
    unsafe {
        let mut message = mem::zeroed();
        let mut message_length = mem::zeroed();
        cass_future_error_message(future, &mut message, &mut message_length);
        println!("Error: {:?}", raw2utf8(message,message_length));
    }
}

fn create_cluster() -> *mut CassCluster {
    unsafe {
        let cluster = cass_cluster_new();
        let host = CString::new("127.0.0.1").unwrap();
        cass_cluster_set_contact_points(cluster, host.as_ptr());
        cluster
    }
}

fn connect_session(session: &mut CassSession, cluster: &CassCluster) -> CassError {
    unsafe {
        let future = cass_session_connect(session, cluster);
        cass_future_wait(future);
        let err = cass_future_error_code(future);
        cass_future_free(future);
        err
    }
}

fn execute_query(session: &mut CassSession, query: &str) -> Result<(), CassError> {
    unsafe {
        let query = CString::new(query).unwrap();
        let statement = cass_statement_new(query.as_ptr(), 0);
        let future = &mut*cass_session_execute(session, statement);
        cass_future_wait(future);
        cass_future_error_code(future);
        let result = match cass_future_error_code(future) {
            CASS_OK => Ok(()),
            rc => {
                print_error(future);
                Err(rc)
            }
        };
        cass_future_free(future);
        cass_statement_free(statement);
        result
    }
}

fn insert_into_tuple(session: &mut CassSession, uuid_gen: &mut CassUuidGen) -> Result<(), CassError> {
    unsafe {
        let mut id = mem::zeroed();
        let mut id_str: [i8; 37] = mem::zeroed();

        let query = CString::new("INSERT INTO examples.tuples (id, item) VALUES (?, ?)").unwrap();

        let statement = &mut*cass_statement_new(query.as_ptr(), 2);
        cass_uuid_gen_time(uuid_gen, &mut id);
        cass_uuid_string(id, id_str[..].as_mut_ptr());

        let item = cass_tuple_new(2);

        cass_tuple_set_string(item, 0, id_str[..].as_mut_ptr());
        cass_tuple_set_int64(item, 1, id.time_and_version as i64);

        cass_statement_bind_uuid(statement, 0, id);
        cass_statement_bind_tuple(statement, 1, item);

        let future = &mut*cass_session_execute(session, statement);
        cass_future_wait(future);

        let rc = cass_future_error_code(future);
        if rc != CASS_OK {
            print_error(future);
        }

        cass_future_free(future);
        cass_statement_free(statement);
        cass_tuple_free(item);

        Ok(())
    }
}

fn select_from_tuple(session: &mut CassSession) -> Result<(), CassError> {
    unsafe {
        let query = CString::new("SELECT * FROM examples.tuples").unwrap();

        let statement = cass_statement_new(query.as_ptr(), 0);

        let future = &mut*cass_session_execute(session, statement);
        cass_future_wait(future);

        match cass_future_error_code(future) {
            CASS_OK => {
                let result = cass_future_get_result(future);
                let rows = cass_iterator_from_result(result);

                while cass_iterator_next(rows) > 0 {
                    let mut id = mem::zeroed();
                    let mut id_str = mem::zeroed();
                    let row = cass_iterator_get_row(rows);
                    let id_value = cass_row_get_column_by_name(row, CString::new("id").unwrap().as_ptr());
                    let item_value = cass_row_get_column_by_name(row, CString::new("item").unwrap().as_ptr());
                    let item = cass_iterator_from_tuple(item_value);

                    cass_value_get_uuid(id_value, &mut id);
                    cass_uuid_string(id, &mut id_str);

                    print!("id {:?} ", &id_str);

                    while cass_iterator_next(item) > 0 {
                        let value = cass_iterator_get_value(item);

                        match cass_value_is_null(value) == 0 {
                            true => {
                                match cass_value_type(value) {
                                    CASS_VALUE_TYPE_VARCHAR => {
                                        let mut text = mem::zeroed();
                                        let mut text_length = mem::zeroed();
                                        cass_value_get_string(value, &mut text, &mut text_length);
                                        print!("{:?} ", raw2utf8(text,text_length));
                                    }
                                    CASS_VALUE_TYPE_BIGINT => {
                                        let mut i = mem::zeroed();
                                        cass_value_get_int64(value, &mut i);
                                        print!("{:?} ", i);
                                    }
                                    other_type => {
                                        print!("<invalid type {:?}> ", other_type);
                                    }
                                }
                            }
                            false => {
                                print!("<null> ");
                            }
                        }
                    }
                    println!("");
                }
                cass_result_free(result);
                cass_iterator_free(rows);
            }
            _ => {
                print_error(future);
            }
        }
        cass_future_free(future);
        cass_statement_free(statement);
        Ok(())
    }
}

fn main() {
    unsafe {
        let cluster = &mut*create_cluster();
        let mut session = &mut*cass_session_new();

        let uuid_gen = &mut*cass_uuid_gen_new();

        if connect_session(session, cluster) != CASS_OK {
            cass_cluster_free(cluster);
            cass_session_free(session);
            panic!();
        }

        execute_query(session, "CREATE KEYSPACE IF NOT EXISTS examples WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' }").unwrap();

        execute_query(session, "CREATE TABLE IF NOT EXISTS examples.tuples (id timeuuid, item frozen<tuple<text, bigint>>, PRIMARY KEY(id))").unwrap();

        insert_into_tuple(session, uuid_gen).unwrap();
        select_from_tuple(session).unwrap();

        let close_future = cass_session_close(session);
        cass_future_wait(close_future);
        cass_future_free(close_future);

        cass_cluster_free(cluster);
        cass_session_free(session);

        cass_uuid_gen_free(uuid_gen);
    }
}
<|endoftext|>