From 5bc73e4e6375a22e6b71300e0befc9cf4c97c278 Mon Sep 17 00:00:00 2001 From: Jeron Aldaron Lau Date: Wed, 14 Feb 2024 00:28:42 -0600 Subject: [PATCH] Preserve OS case for hostnames (#86) --- CHANGELOG.md | 3 ++- Cargo.toml | 2 +- examples/os-strings.rs | 25 +++++++++++++------------ examples/web/src/lib.rs | 7 ++++--- examples/whoami-demo.rs | 21 +++++++++++---------- src/fallible.rs | 13 ++++--------- src/lib.rs | 34 +++++++++++++++++++++------------- src/os.rs | 1 + 8 files changed, 57 insertions(+), 49 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bb15d3..a11c093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ The format is based on [Keep a Changelog], and this project adheres to - `whoami::fallible::devicename()` - `whoami::fallible::devicename_os()` - `whoami::fallible::distro()` - - `whoami::fallible::hostname()` + - `whoami::fallible::hostname()` - notably doesn't normalize to lowercase - `whoami::fallible::realname()` - `whoami::fallible::realname_os()` - `whoami::fallible::username()` @@ -30,6 +30,7 @@ The format is based on [Keep a Changelog], and this project adheres to ### Changed - Deprecated `whoami::distro_os()` + - Deprecated `whoami::hostname()` - Deprecated `whoami::hostname_os()` - Deprecated `whoami::lang()` diff --git a/Cargo.toml b/Cargo.toml index 87082b7..b54095c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "whoami" -version = "1.4.1" +version = "1.5.0-pre.0" edition = "2018" license = "Apache-2.0 OR BSL-1.0 OR MIT" documentation = "https://docs.rs/whoami" diff --git a/examples/os-strings.rs b/examples/os-strings.rs index 1e8ed68..6cb2adf 100644 --- a/examples/os-strings.rs +++ b/examples/os-strings.rs @@ -2,7 +2,7 @@ fn main() { println!("WhoAmI {}", env!("CARGO_PKG_VERSION")); println!(); println!( - "User's Language whoami::lang(): {:?}", + "User's Language whoami::langs(): {:?}", whoami::langs() .map(|l| l .map(|l| l.to_string()) @@ -10,35 +10,36 @@ fn main() { .collect::>(), ); println!( - "User's Name whoami::realname_os(): {:?}", + "User's Name whoami::realname_os(): {:?}", whoami::realname_os(), ); println!( - "User's Username whoami::username_os(): {:?}", - whoami::username(), + "User's Username whoami::username_os(): {:?}", + whoami::username_os(), ); println!( - "Device's Pretty Name whoami::devicename_os(): {:?}", - whoami::devicename(), + "Device's Pretty Name whoami::devicename_os(): {:?}", + whoami::devicename_os(), ); println!( - "Device's Hostname whoami::hostname_os(): {:?}", - whoami::hostname(), + "Device's Hostname whoami::fallible::hostname(): {:?}", + whoami::fallible::hostname() + .unwrap_or_else(|_| "localhost".to_string()), ); println!( - "Device's Platform whoami::platform_os(): {:?}", + "Device's Platform whoami::platform(): {:?}", whoami::platform(), ); println!( - "Device's OS Distro whoami::distro_os(): {:?}", + "Device's OS Distro whoami::distro(): {:?}", whoami::distro(), ); println!( - "Device's Desktop Env. whoami::desktop_env(): {:?}", + "Device's Desktop Env. whoami::desktop_env(): {:?}", whoami::desktop_env(), ); println!( - "Device's CPU Arch whoami::arch(): {:?}", + "Device's CPU Arch whoami::arch(): {:?}", whoami::arch(), ); } diff --git a/examples/web/src/lib.rs b/examples/web/src/lib.rs index 57f996b..89945b5 100644 --- a/examples/web/src/lib.rs +++ b/examples/web/src/lib.rs @@ -25,7 +25,7 @@ pub fn main() { whoami::username(), )); log(format!( - "User's Languages whoami::lang(): {:?}", + "User's Languages whoami::langs(): {:?}", whoami::langs() .map(|l| l.map(|l| l.to_string()).unwrap_or("??".to_string())) .collect::>(), @@ -35,8 +35,9 @@ pub fn main() { whoami::devicename(), )); log(format!( - "Device's Hostname whoami::hostname(): {}", - whoami::hostname(), + "Device's Hostname whoami::fallible::hostname(): {}", + whoami::fallible::hostname() + .unwrap_or_else(|_| "localhost".to_string()), )); log(format!( "Device's Platform whoami::platform(): {}", diff --git a/examples/whoami-demo.rs b/examples/whoami-demo.rs index 25b90d3..4ed6669 100644 --- a/examples/whoami-demo.rs +++ b/examples/whoami-demo.rs @@ -2,7 +2,7 @@ fn main() { println!("WhoAmI {}", env!("CARGO_PKG_VERSION")); println!(); println!( - "User's Language whoami::langs(): {}", + "User's Language whoami::langs(): {}", whoami::langs() .map(|l| l .map(|l| l.to_string()) @@ -11,35 +11,36 @@ fn main() { .join(", "), ); println!( - "User's Name whoami::realname(): {}", + "User's Name whoami::realname(): {}", whoami::realname(), ); println!( - "User's Username whoami::username(): {}", + "User's Username whoami::username(): {}", whoami::username(), ); println!( - "Device's Pretty Name whoami::devicename(): {}", + "Device's Pretty Name whoami::devicename(): {}", whoami::devicename(), ); println!( - "Device's Hostname whoami::hostname(): {}", - whoami::hostname(), + "Device's Hostname whoami::fallible::hostname(): {}", + whoami::fallible::hostname() + .unwrap_or_else(|_| "localhost".to_string()), ); println!( - "Device's Platform whoami::platform(): {}", + "Device's Platform whoami::platform(): {}", whoami::platform(), ); println!( - "Device's OS Distro whoami::distro(): {}", + "Device's OS Distro whoami::distro(): {}", whoami::distro(), ); println!( - "Device's Desktop Env. whoami::desktop_env(): {}", + "Device's Desktop Env. whoami::desktop_env(): {}", whoami::desktop_env(), ); println!( - "Device's CPU Arch whoami::arch(): {}", + "Device's CPU Arch whoami::arch(): {}", whoami::arch(), ); } diff --git a/src/fallible.rs b/src/fallible.rs index 21d8fbb..d6ff0d8 100644 --- a/src/fallible.rs +++ b/src/fallible.rs @@ -68,15 +68,10 @@ pub fn devicename_os() -> Result { /// Get the host device's hostname. /// -/// Limited to a-z (case insensitve), 0-9, and dashes. This limit also applies -/// to `devicename()` when targeting Windows. Since the hostname is -/// case-insensitive, this method normalizes to lowercase (unlike -/// [`devicename()`]). +/// Limited to a-z, A-Z, 0-9, and dashes. This limit also applies to +/// [`devicename()`] when targeting Windows. Usually hostnames are +/// case-insensitive, but it's not a hard requirement. #[inline(always)] pub fn hostname() -> Result { - let mut hostname = Target::hostname(Os)?; - - hostname.make_ascii_lowercase(); - - Ok(hostname) + Target::hostname(Os) } diff --git a/src/lib.rs b/src/lib.rs index af5ab21..f2b4cea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -497,27 +497,35 @@ pub fn devicename_os() -> OsString { /// Get the host device's hostname. /// -/// Limited to a-z (case insensitve), 0-9, and dashes. This limit also applies -/// to `devicename()` when targeting Windows. Since the hostname is -/// case-insensitive, this method normalizes to lowercase (unlike -/// [`devicename()`]). +/// Limited to a-z (case insensitive), 0-9, and dashes. This limit also applies +/// to `devicename()` with the exeception of case sensitivity when targeting +/// Windows. This method normalizes to lowercase. Usually hostnames will be +/// case-insensitive, but it's not a hard requirement. +/// +/// Use [`fallible::hostname()`] for case-sensitive hostname. #[inline(always)] +#[deprecated(note = "use `fallible::hostname()` instead", since = "1.5.0")] pub fn hostname() -> String { - fallible::hostname().unwrap_or_else(|_| DEFAULT_HOSTNAME.to_lowercase()) + let mut hostname = fallible::hostname() + .unwrap_or_else(|_| DEFAULT_HOSTNAME.to_lowercase()); + + hostname.make_ascii_lowercase(); + hostname } /// Get the host device's hostname. /// -/// Limited to a-z (case insensitve), 0-9, and dashes. This limit also applies -/// to `devicename()` when targeting Windows. Since the hostname is -/// case-insensitive, this method normalizes to lowercase (unlike -/// [`devicename()`]). +/// Limited to a-z (case insensitive), 0-9, and dashes. This limit also applies +/// to `devicename()` with the exeception of case sensitivity when targeting +/// Windows. This method normalizes to lowercase. Usually hostnames will be +/// case-insensitive, but it's not a hard requirement. +/// +/// Use [`fallible::hostname()`] for case-sensitive hostname. #[inline(always)] -#[deprecated(note = "use `hostname()` instead", since = "1.5.0")] +#[deprecated(note = "use `fallible::hostname()` instead", since = "1.5.0")] pub fn hostname_os() -> OsString { - fallible::hostname() - .map(OsString::from) - .unwrap_or_else(|_| DEFAULT_HOSTNAME.to_lowercase().into()) + #[allow(deprecated)] + hostname().into() } /// Get the name of the operating system distribution and (possibly) version. diff --git a/src/os.rs b/src/os.rs index 0220ab7..ccd9c89 100644 --- a/src/os.rs +++ b/src/os.rs @@ -62,6 +62,7 @@ pub(crate) struct Os; /// Target platform support pub(crate) trait Target { /// Return a list of languages. + #[allow(dead_code)] // FIXME fn langs(self) -> Vec; /// Return the user's "real" / "full" name. fn realname(self) -> Result;