From 02190c44f7072b4fdaca50836a583f955866f8de Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Fri, 11 Jan 2019 01:24:16 +0000 Subject: Check the DIALOG environment variable in default_backend This patch changes the logic in default_backend to respect the DIALOG environment variable that may contain the name of the backend to use. --- src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 87234bd..3a6a090 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,6 +85,8 @@ mod error; /// [`Backend`]: trait.Backend.html pub mod backends; +use std::env; + pub use crate::error::{Error, Result}; /// A dialog box that can be shown using a backend. @@ -335,9 +337,19 @@ impl DialogBox for Question { /// Creates a new instance of the default backend. /// -/// The current implementation always returns a [`Dialog`][] instance. +/// The following steps are performed to determine the default backend: +/// - If the `DIALOG` environment variable is set to a valid backend name, this backend is used. +/// A valid backend name is the name of a struct in the `backends` module implementing the +/// `Backend` trait in any case. +/// - Otherwise, a [`Dialog`][] instance is returned. /// /// [`Dialog`]: backends/struct.Dialog.html pub fn default_backend() -> Box { + if let Ok(backend) = env::var("DIALOG") { + if let Some(backend) = backends::from_str(&backend) { + return backend; + } + } + Box::new(backends::Dialog::new()) } -- cgit v1.2.1