From 169a95313244f28086e01b345396c8154d78a648 Mon Sep 17 00:00:00 2001 From: Wang Xuerui Date: Sat, 11 May 2019 18:54:11 +0800 Subject: [PATCH] Use the cfg-if crate for platform support conditionals --- Cargo.toml | 3 +++ src/lib.rs | 29 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cfc839f..13853dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,9 @@ repository = "/~https://github.com/soc/dirs-rs" maintenance = { status = "actively-developed" } keywords = ["xdg", "basedir", "app_dirs", "path", "folder"] +[dependencies] +cfg-if = "0.1" + [target.'cfg(unix)'.dependencies] libc = "0.2" diff --git a/src/lib.rs b/src/lib.rs index 131f221..0ab431a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,18 +13,29 @@ #![deny(missing_docs)] +#[macro_use] +extern crate cfg_if; + use std::path::PathBuf; -#[cfg(target_os = "windows")] mod win; -#[cfg(target_os = "macos")] mod mac; -#[cfg(target_os = "redox")] mod redox; -#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "redox")))] mod lin; -#[cfg(unix)] mod unix; +#[cfg(unix)] +mod unix; -#[cfg(target_os = "windows")] use win as sys; -#[cfg(target_os = "macos")] use mac as sys; -#[cfg(target_os = "redox")] use redox as sys; -#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "redox")))] use lin as sys; +cfg_if! { + if #[cfg(target_os = "windows")] { + mod win; + use win as sys; + } else if #[cfg(target_os = "macos")] { + mod mac; + use mac as sys; + } else if #[cfg(target_os = "redox")] { + mod redox; + use redox as sys; + } else { + mod lin; + use lin as sys; + } +} /// Returns the path to the user's home directory. ///