I try to compile my project with the command cargo build
.
build.rs
extern crate csv;
use std::path::Path;
use std::fs::OpenOptions;
use std::io::BufWriter;
use std::io::Write;
#[allow(non_snake_case)]
fn processCSV(filename: &str, sourcePath: &str, enumName: &str) {
println!("Generate rust source code from schema {}",filename);
let mut ret: Vec<String> = Vec::new();
let mut rdr = csv::Reader::from_file(filename).unwrap().flexible(true);
for record in rdr.records().map(|r| r.unwrap()) {
}
let path = Path::new(sourcePath);
let file = match OpenOptions::new().write(true).create(true).open(&path) {
Ok(file) => file,
Err(..) => panic!("Cannot create file {}",path.display()),
};
let mut writer = BufWriter::new(file);
writer.write_all(b"test\n");
}
fn main() {
processCSV("../schemas/Test.csv", "./src/mod/common/StatusCode.rs", "StatusCode");
}
and Cargo.toml
[package]
name = "rust-test"
version = "0.0.1"
build = "build.rs"
[lib]
path = "src/lib.rs"
[dependencies]
[build-dependencies]
csv = "*"
I can see this error :
src/lib.rs:1:1: 1:18 error: can't find crate for
csv
src/lib.rs:1 extern crate csv;
but when I change flexible(true)
to flexible(false)
it compiles just fine without any errors. What do I need to do to fix this?
I am using Rust 1.2.0 on Windows 7 64-bit.
Changing flexible(false)
for flexible(true)
makes no difference for me; both fail. The problem is that you've chosen build-dependencies
for some reason, instead of just dependencies
.
Using the src/lib.rs file that you provided in your answer, and this Cargo.toml file:
[package]
name = "stack-overflow"
version = "0.1.0"
authors = ["A. Developer <[email protected]>"]
[dependencies]
csv = "*"
It compiles fine.
If you need to access a dependency both in your build.rs and in your project, you need to include the dependency in both sections.
A build dependency is a dependency for a build script, which is a helper binary compiled and run before your main crate is built (designed to be used for code-generation, and building/finding native C libraries, etc.).
Normal dependencies used by the main code should just fall into the "dependencies" section, e.g.
[dependencies]
csv = "0.14"
There's also a "dev-dependencies" section, which are dependencies that are only needed for testing, i.e. they are compiled and used only for cargo test
. This allows crates to depend on, for example, quickcheck
for running tests without contaminating the main artifact.
In summary, running cargo build
will do something like:
build-dependencies
dependencies
Running cargo test
adds:
dev-dependencies
--test
to create a test runner for any in-source #[test]
s (pointing the compiler to both the dependencies and dev-dependencies)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With