<commit_before>/* 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/. */

use std::env;
use std::path::Path;
use std::process::Command;
use std::str::from_utf8;

extern crate tempdir;
use self::tempdir::TempDir;

/// Use rustc to expand and pretty print the crate into a single file,
/// removing any macros in the process.
pub fn expand(manifest_path: &Path,
              crate_name: &str,
              version: &str) -> Result<String, String> {
    let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));

    // Create a temp directory to use as a target dir for cargo expand, for
    // hygenic purposes.
    let target_dir = TempDir::new("cbindgen-expand")
                             .map_err(|_| format!("couldn't create a temp target directory"))?;

    let mut cmd = Command::new(cargo);
    cmd.env("CARGO_TARGET_DIR", target_dir.path());
    cmd.arg("rustc");
    cmd.arg("--manifest-path");
    cmd.arg(manifest_path);
    cmd.arg("--all-features");
    cmd.arg("-p");
    cmd.arg(&format!("{}:{}", crate_name, version));
    cmd.arg("--");
    cmd.arg("-Z");
    cmd.arg("unstable-options");
    cmd.arg("--pretty=expanded");
    let output = cmd.output().unwrap();

    let src = from_utf8(&output.stdout).unwrap().to_owned();
    let error = from_utf8(&output.stderr).unwrap().to_owned();

    if src.len() == 0 {
        Err(error)
    } else {
        Ok(src)
    }
}
<commit_msg>Allow CARGO_TARGET_DIR override in cargo_expand<commit_after>/* 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/. */

use std::env;
use std::path::Path;
use std::process::Command;
use std::str::from_utf8;

extern crate tempdir;
use self::tempdir::TempDir;

/// Use rustc to expand and pretty print the crate into a single file,
/// removing any macros in the process.
pub fn expand(manifest_path: &Path,
              crate_name: &str,
              version: &str) -> Result<String, String> {
    let cargo = env::var("CARGO").unwrap_or_else(|_| String::from("cargo"));
    let run = |target_dir: &Path| {
        let mut cmd = Command::new(cargo);
        cmd.env("CARGO_TARGET_DIR", target_dir);
        cmd.arg("rustc");
        cmd.arg("--manifest-path");
        cmd.arg(manifest_path);
        cmd.arg("--all-features");
        cmd.arg("-p");
        cmd.arg(&format!("{}:{}", crate_name, version));
        cmd.arg("--");
        cmd.arg("-Z");
        cmd.arg("unstable-options");
        cmd.arg("--pretty=expanded");
        let output = cmd.output().unwrap();

        let src = from_utf8(&output.stdout).unwrap().to_owned();
        let error = from_utf8(&output.stderr).unwrap().to_owned();

        if src.len() == 0 {
            Err(error)
        } else {
            Ok(src)
        }
    };

    if let Ok(ref path) = env::var("CARGO_EXPAND_TARGET_DIR") {
        run(&Path::new(path))
    } else {
        // Create a temp directory to use as a target dir for cargo expand, for
        // hygenic purposes.
        let target_dir = TempDir::new("cbindgen-expand")
            .map_err(|_| format!("couldn't create a temp target directory"))?;

        run(target_dir.path())
    }
}

{'fraction_non_alphanumeric': 0.1621305238513316, 'fraction_numerical': 0.004682470002926544, 'mean_word_length': 2.6991341991341993, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '952561', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1084, 'n_words': 296}