From 86c800554bc6ca3d28f6bb66def5b6c6b8d5fc0b Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 9 Sep 2019 14:45:07 -0700 Subject: [PATCH] Make toml an optional feature --- Cargo.toml | 5 ++++- examples/theme.rs | 40 ++++++++++++++++++++++++++++------------ src/cursive.rs | 7 +++++++ src/theme/mod.rs | 15 ++++++++++++++- src/theme/palette.rs | 4 +++- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a6dea2c..9ef77ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,7 +26,6 @@ enum-map = "0.6.0" enumset = "0.4.0" log = "0.4.8" owning_ref = "0.4.0" -toml = "0.5.1" unicode-segmentation = "1.3.0" unicode-width = "0.1.5" xi-unicode = "0.2.0" @@ -38,6 +37,10 @@ chrono = "0.4.7" cfg-if = "0.1.9" ahash = "0.2.12" +[dependencies.toml] +optional = true +version = "0.5.1" + [dependencies.num] default-features = false version = "0.2.0" diff --git a/examples/theme.rs b/examples/theme.rs index 30d3506..f03385f 100644 --- a/examples/theme.rs +++ b/examples/theme.rs @@ -3,20 +3,36 @@ use cursive::Cursive; fn main() { let mut siv = Cursive::default(); - // You can load a theme from a file at runtime for fast development. - siv.load_theme_file("assets/style.toml").unwrap(); - // Or you can directly load it from a string for easy deployment. - siv.load_toml(include_str!("../assets/style.toml")).unwrap(); + #[cfg(feature = "toml")] + { + // You can load a theme from a file at runtime for fast development. + siv.load_theme_file("assets/style.toml").unwrap(); - siv.add_layer( - Dialog::around(TextView::new( - "This application uses a \ - custom theme!", - )) - .title("Themed dialog") - .button("Quit", |s| s.quit()), - ); + // Or you can directly load it from a string for easy deployment. + siv.load_toml(include_str!("../assets/style.toml")).unwrap(); + + siv.add_layer( + Dialog::around(TextView::new( + "This application uses a \ + custom theme!", + )) + .title("Themed dialog") + .button("Quit", |s| s.quit()), + ); + } + + #[cfg(not(feature = "toml"))] + { + siv.add_layer( + Dialog::around(TextView::new( + "Run this example again with the `toml` feature!\n\n\ + cargo run --example theme --features toml", + )) + .title("Themed dialog - missing `toml` feature") + .button("Quit", |s| s.quit()), + ); + } siv.run(); } diff --git a/src/cursive.rs b/src/cursive.rs index a1b7ae4..1bbabdd 100644 --- a/src/cursive.rs +++ b/src/cursive.rs @@ -1,5 +1,6 @@ use std::any::Any; use std::num::NonZeroU32; +#[cfg(feature = "toml")] use std::path::Path; use std::time::Duration; @@ -405,9 +406,12 @@ impl Cursive { .clear(self.theme.palette[theme::PaletteColor::Background]); } + #[cfg(feature = "toml")] /// Loads a theme from the given file. /// /// `filename` must point to a valid toml file. + /// + /// Must have the `toml` feature enabled. pub fn load_theme_file>( &mut self, filename: P, @@ -415,9 +419,12 @@ impl Cursive { theme::load_theme_file(filename).map(|theme| self.set_theme(theme)) } + #[cfg(feature = "toml")] /// Loads a theme from the given string content. /// /// Content must be valid toml. + /// + /// Must have the `toml` feature enabled. pub fn load_toml(&mut self, content: &str) -> Result<(), theme::Error> { theme::load_toml(content).map(|theme| self.set_theme(theme)) } diff --git a/src/theme/mod.rs b/src/theme/mod.rs index aea149e..24d17a2 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -171,11 +171,13 @@ pub use self::color_style::{ColorStyle, ColorType}; pub use self::effect::Effect; pub use self::palette::{Palette, PaletteColor}; pub use self::style::Style; +#[cfg(feature = "toml")] use std::fs::File; use std::io; +#[cfg(feature = "toml")] use std::io::Read; +#[cfg(feature = "toml")] use std::path::Path; -use toml; /// Represents the style a Cursive application will use. #[derive(Clone, Debug)] @@ -199,6 +201,7 @@ impl Default for Theme { } impl Theme { + #[cfg(feature = "toml")] fn load_toml(&mut self, table: &toml::value::Table) { if let Some(&toml::Value::Boolean(shadow)) = table.get("shadow") { self.shadow = shadow; @@ -219,23 +222,30 @@ impl Theme { pub enum Error { /// An error occured when reading the file. Io(io::Error), + + #[cfg(feature = "toml")] /// An error occured when parsing the toml content. Parse(toml::de::Error), } +#[cfg(feature = "toml")] impl From for Error { fn from(err: io::Error) -> Self { Error::Io(err) } } +#[cfg(feature = "toml")] impl From for Error { fn from(err: toml::de::Error) -> Self { Error::Parse(err) } } +#[cfg(feature = "toml")] /// Loads a theme from file and sets it as active. +/// +/// Must have the `toml` feature enabled. pub fn load_theme_file>(filename: P) -> Result { let content = { let mut content = String::new(); @@ -248,6 +258,9 @@ pub fn load_theme_file>(filename: P) -> Result { } /// Loads a theme string and sets it as active. +/// +/// Must have the `toml` feature enabled. +#[cfg(feature = "toml")] pub fn load_toml(content: &str) -> Result { let table = toml::de::from_str(content)?; diff --git a/src/theme/palette.rs b/src/theme/palette.rs index 858b68d..83e6eac 100644 --- a/src/theme/palette.rs +++ b/src/theme/palette.rs @@ -1,7 +1,7 @@ use super::Color; use enum_map::{enum_map, Enum, EnumMap}; +#[cfg(feature = "toml")] use log::warn; -use toml; use std::ops::{Index, IndexMut}; @@ -173,6 +173,7 @@ impl Default for Palette { } // Iterate over a toml +#[cfg(feature = "toml")] fn iterate_toml<'a>( table: &'a toml::value::Table, ) -> impl Iterator + 'a { @@ -216,6 +217,7 @@ fn iterate_toml<'a>( } /// Fills `palette` with the colors from the given `table`. +#[cfg(feature = "toml")] pub(crate) fn load_toml(palette: &mut Palette, table: &toml::value::Table) { // TODO: use serde for that? // Problem: toml-rs doesn't do well with Enums...