Skip to content

Commit

Permalink
Make the named module and named::from_str optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogeon committed Feb 2, 2016
1 parent 57460f8 commit a401917
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ branches:
script:
- cargo build -v --features strict
- cargo test -v --features strict
- bash scripts/test_features.sh
- cargo doc
after_success:
- sh scripts/upload_doc.sh
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ license = "MIT OR Apache-2.0"
build = "build/main.rs"

[features]
default = ["named_from_str"]
named_from_str = ["named", "phf", "phf_codegen"]
named = []

#internal
strict = []

[dependencies]
num = "0.1"
phf = "0.7"

[dependencies.phf]
version = "0.7"
optional = true

[dev-dependencies]
image = "0.4"
approx = "0.1"
clap = "1"

[build-dependencies]
phf_codegen = "0.7"
[build-dependencies.phf_codegen]
version = "0.7"
optional = true
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ Add the following lines to your `Cargo.toml` file:
palette = "0.2"
```

###Optional Features

These features are enabled by default:

* `"named"` - Enables color constants, located in the `named` module.
* `"named_from_str"` - Enables the `named::from_str`, which maps name string to colors.

# Linear?

Colors in, for example, images are often "gamma corrected" or stored in sRGB
Expand Down
1 change: 1 addition & 0 deletions build/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "phf_codegen")]
extern crate phf_codegen;

mod named;
Expand Down
29 changes: 24 additions & 5 deletions build/named.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fs::File;
use std::path::Path;
use std::io::{Write, BufRead, BufReader};

#[cfg(feature = "named")]
pub fn build() {
use std::path::Path;
use std::io::{Write, BufRead, BufReader};

let out_dir = ::std::env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("named.rs");

Expand All @@ -25,11 +27,28 @@ pub fn build() {
entries.push((name.to_owned(), name.to_uppercase()));
}

gen_from_str(&mut writer, &entries)
}

#[cfg(feature = "named_from_str")]
fn gen_from_str(writer: &mut File, entries: &[(String, String)]) {
use std::io::Write;

write!(writer, "static COLORS: ::phf::Map<&'static str, (u8, u8, u8)> = ").unwrap();
let mut map = ::phf_codegen::Map::new();
for &(ref key, ref value) in &entries {
for &(ref key, ref value) in entries {
map.entry(&**key, value);
}
map.build(&mut writer).unwrap();
map.build(writer).unwrap();
writeln!(writer, ";").unwrap();
}
}




#[cfg(not(feature = "named"))]
pub fn build() {}

#[allow(unused)]
#[cfg(not(feature = "named_from_str"))]
fn gen_from_str(_writer: &mut File, _entries: &[(String, String)]) {}
45 changes: 45 additions & 0 deletions scripts/test_features.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
set -e

#List of features to test
features=""

#Features that will always be activated
required_features="strict"


#Find features
walking_features=false
current_dependency=""

while read -r line || [[ -n "$line" ]]; do
if [[ "$line" == "[features]" ]]; then
walking_features=true
elif [[ $walking_features == true ]] && [[ "$line" == "#internal" ]]; then
walking_features=false
elif [[ $walking_features == true ]] && echo "$line" | grep -E "^\[.*\]" > /dev/null; then
walking_features=false
elif [[ $walking_features == true ]] && echo "$line" | grep -E ".*=.*" > /dev/null; then
feature="$(echo "$line" | cut -f1 -d"=")"
feature="$(echo -e "${feature}" | tr -d '[[:space:]]')"
if [[ "$feature" != "default" ]]; then
features="$features $feature"
fi
elif echo "$line" | grep -E "^\[dependencies\..*\]" > /dev/null; then
current_dependency="$(echo "$line" | sed 's/.*\[dependencies\.\([^]]*\)\].*/\1/g')"
elif [[ "$line" == "#feature" ]] && [[ "$current_dependency" != "" ]]; then
echo "found dependency feature '$current_dependency'"
features="$features $current_dependency"
fi
done < "Cargo.toml"

echo -e "features: $features\n"

#Test without any optional feature
echo compiling with --no-default-features --features "$required_features"
cargo build --no-default-features --features "$required_features"

#Isolated test of each optional feature
for feature in $features; do
echo compiling with --no-default-features --features "\"$feature $required_features\""
cargo build --no-default-features --features "$feature $required_features"
done
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@
#[cfg(test)]
#[macro_use]
extern crate approx;

extern crate num;

#[cfg(feature = "phf")]
extern crate phf;

use num::{Float, ToPrimitive, NumCast};
Expand Down Expand Up @@ -276,6 +279,8 @@ macro_rules! assert_ranges {

pub mod gradient;
pub mod pixel;

#[cfg(feature = "named")]
pub mod named;

mod alpha;
Expand Down
7 changes: 5 additions & 2 deletions src/named.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//!A collection of named color constants.
//!A collection of named color constants. Can be toggled with the `"named"`
//!Cargo feature.
//!
//!They are taken from the [SVG keyword
//!colors](https://www.w3.org/TR/SVG/types.html#ColorKeywords) (same as in
Expand All @@ -21,9 +22,11 @@
include!(concat!(env!("OUT_DIR"), "/named.rs"));

///Get a SVG/CSS3 color by name.
///Get a SVG/CSS3 color by name. Can be toggled with the `"named_from_str"`
///Cargo feature.
///
///The names are the same as the constants, but lower case.
#[cfg(feature = "named_from_str")]
pub fn from_str(name: &str) -> Option<(u8, u8, u8)> {
COLORS.get(name).cloned()
}

0 comments on commit a401917

Please sign in to comment.