Make toml an optional feature

This commit is contained in:
Alexandre Bury 2019-09-09 14:45:07 -07:00
parent 565c64c528
commit 86c800554b
5 changed files with 56 additions and 15 deletions

View File

@ -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"

View File

@ -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();
}

View File

@ -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<P: AsRef<Path>>(
&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))
}

View File

@ -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<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}
#[cfg(feature = "toml")]
impl From<toml::de::Error> 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<P: AsRef<Path>>(filename: P) -> Result<Theme, Error> {
let content = {
let mut content = String::new();
@ -248,6 +258,9 @@ pub fn load_theme_file<P: AsRef<Path>>(filename: P) -> Result<Theme, Error> {
}
/// 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<Theme, Error> {
let table = toml::de::from_str(content)?;

View File

@ -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<Item = (&'a str, PaletteNode)> + '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...