Compile in definitions.units by default

This makes the default compilation of rink invoke the secondary
licensing clause, which results in the binaries being under the GPL.

You can still use your own definitions.units - it will only use the
built in one if it can't find another.
This commit is contained in:
Tiffany Bennett 2016-08-22 12:03:00 -04:00
parent 3181e24e29
commit b75e67f86c
3 changed files with 31 additions and 28 deletions

View file

@ -10,10 +10,11 @@ license = "MPL-2.0"
keywords = ["unit", "math", "conversion", "cli", "tool"]
[features]
default = ["rustyline", "chrono-humanize"]
default = ["rustyline", "chrono-humanize", "gpl"]
ircbot = ["irc", "glob"]
web = ["hyper", "url", "sandbox"]
sandbox = ["libc", "ipc-channel"]
gpl = []
[dependencies]
rust-gmp = "0.3.2"

View file

@ -13,12 +13,6 @@ Select features:
`cargo install rink`
You must then download
[definitions.units](https://raw.githubusercontent.com/tiffany352/rink-rs/master/definitions.units)
and install it in `~/.config/rink/definitions.units` (Linux),
`%APPDATA%\rink\definitions.units` (Windows), or `~/Library/Application
Support/rink/definitions.units` (MacOS).
## Examples
```

View file

@ -89,34 +89,42 @@ fn config_dir() -> Result<PathBuf, String> {
.map(|mut x: PathBuf| { x.push("Library/Application Support"); x})
}
#[cfg(feature = "gpl")]
static DEFAULT_FILE: Option<&'static str> = Some(include_str!("../definitions.units"));
#[cfg(not(feature = "gpl"))]
static DEFAULT_FILE: Option<&'static str> = None;
/// Creates a context by searching standard directories for definitions.units.
pub fn load() -> Result<Context, String> {
use std::io::Read;
use std::fs::File;
use std::path::Path;
let f = File::open("definitions.units");
let mut f = match f {
Ok(f) => f,
Err(_) => {
let mut path = try!(config_dir());
path.push("rink/definitions.units");
let f = File::open(&path);
match f {
Ok(f) => f,
Err(e) => return Err(format!(
"Failed to open definitions.units: {}\nIf you installed using \
`cargo install`, then you need to obtain definitions.units separately. Here \
is the URL, download it and put it in {:?}.\n\n{}\n\n",
e, &path, DATA_FILE_URL))
}
}
let mut path = try!(config_dir());
path.push("rink/definitions.units");
let load = |name| {
File::open(name)
.and_then(|mut f| {
let mut buf = vec![];
try!(f.read_to_end(&mut buf));
Ok(String::from_utf8_lossy(&*buf).into_owned())
})
};
let string =
load(Path::new("definitions.units"))
.or_else(|_| load(path.as_ref()))
.or_else(|_| DEFAULT_FILE.map(|x| x.to_owned()).ok_or(format!(
"Did not exist in search path and binary is not compiled with `gpl` feature")))
.map_err(|e| format!(
"Failed to open definitions.units: {}\n\
If you installed with `gpl` disabled, then you need to obtain definitions.units \
separately. Here is the URL, download it and put it in {:?}.\n\
\n\
{}\n\
\n",
e, &path, DATA_FILE_URL));
let string = try!(string);
let mut buf = vec![];
f.read_to_end(&mut buf).unwrap();
let string = String::from_utf8_lossy(&*buf);
//let mut iter = unit_defs::TokenIterator::new(&*string).peekable();
//let res = unit_defs::parse(&mut iter);
let mut iter = gnu_units::TokenIterator::new(&*string).peekable();
let res = gnu_units::parse(&mut iter);