Skip to content

Commit

Permalink
Preserve OS case for hostnames (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
AldaronLau authored Feb 14, 2024
1 parent 29d5f22 commit 5bc73e4
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 49 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
Expand All @@ -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()`

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
25 changes: 13 additions & 12 deletions examples/os-strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,44 @@ 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())
.unwrap_or_else(|_| "??".to_string()))
.collect::<Vec<String>>(),
);
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(),
);
}
7 changes: 4 additions & 3 deletions examples/web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<String>>(),
Expand All @@ -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(): {}",
Expand Down
21 changes: 11 additions & 10 deletions examples/whoami-demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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(),
);
}
13 changes: 4 additions & 9 deletions src/fallible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,10 @@ pub fn devicename_os() -> Result<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, 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<String> {
let mut hostname = Target::hostname(Os)?;

hostname.make_ascii_lowercase();

Ok(hostname)
Target::hostname(Os)
}
34 changes: 21 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Language>;
/// Return the user's "real" / "full" name.
fn realname(self) -> Result<OsString>;
Expand Down

0 comments on commit 5bc73e4

Please sign in to comment.