Skip to content

Commit

Permalink
Implement Fallible API (#71)
Browse files Browse the repository at this point in the history
* Fallible for `username()`, `realname()` and `_os` variants

* Minor fixes

* Add remaining planned fallible APIs

* Compile error fixes

* Fix unused imports

* Address self-feedback

* Address `clippy::needless_doctest_main`

https://rust-lang.github.io/rust-clippy/master/index.html#needless_doctest_main

* Fix possible memory leak in `distro()` on Windows

* More windows bugs

* Move fallbacks to lib.rs

* Clean up

* Fix wasm build
  • Loading branch information
AldaronLau authored Nov 14, 2023
1 parent 1c076a5 commit f499c74
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 297 deletions.
2 changes: 1 addition & 1 deletion WASM.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ web-sys, and will instead return these mock values:
- `devicename()`: "Unknown"
- `hostname()`: "localhost"
- `platform()`: "Unknown"
- `distro()`: "Unknown Unknown"
- `distro()`: "Emulated"
- `desktop_env()`: "Unknown WebAssembly"
- `arch()`: "wasm32"

Expand Down
38 changes: 19 additions & 19 deletions src/fake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,56 @@ compile_error!("Unexpected pointer width for target platform");

use std::ffi::OsString;

use crate::{Arch, DesktopEnv, Platform};
use crate::{Arch, DesktopEnv, Platform, Result};

#[inline(always)]
pub(crate) fn lang() -> impl Iterator<Item = String> {
std::iter::once("en-US".to_string())
}

#[inline(always)]
pub(crate) fn username_os() -> OsString {
username().into()
pub(crate) fn username_os() -> Result<OsString> {
Ok(username()?.into())
}

#[inline(always)]
pub(crate) fn realname_os() -> OsString {
realname().into()
pub(crate) fn realname_os() -> Result<OsString> {
Ok(realname()?.into())
}

#[inline(always)]
pub(crate) fn devicename_os() -> OsString {
devicename().into()
pub(crate) fn devicename_os() -> Result<OsString> {
Ok(devicename()?.into())
}

#[inline(always)]
pub(crate) fn distro_os() -> Option<OsString> {
distro().map(|a| a.into())
pub(crate) fn distro_os() -> Result<OsString> {
Ok(distro()?.into())
}

#[inline(always)]
pub(crate) fn username() -> String {
"anonymous".to_string()
pub(crate) fn username() -> Result<String> {
Ok("anonymous".to_string())
}

#[inline(always)]
pub(crate) fn realname() -> String {
"Anonymous".to_string()
pub(crate) fn realname() -> Result<String> {
Ok("Anonymous".to_string())
}

#[inline(always)]
pub(crate) fn devicename() -> String {
"Unknown".to_string()
pub(crate) fn devicename() -> Result<String> {
Ok("Unknown".to_string())
}

#[inline(always)]
pub(crate) fn hostname() -> String {
"localhost".to_string()
pub(crate) fn hostname() -> Result<String> {
Ok("localhost".to_string())
}

#[inline(always)]
pub(crate) fn distro() -> Option<String> {
None
pub(crate) fn distro() -> Result<String> {
Ok("Emulated".to_string())
}

#[inline(always)]
Expand Down
97 changes: 97 additions & 0 deletions src/fallible.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! Fallible versions of the whoami APIs.
//!
//! Some of the functions in the root module will return "Unknown" or
//! "localhost" on error. This might not be desirable in some situations. The
//! functions in this module all return a [`Result`].
use std::ffi::OsString;

use crate::{platform, Result};

/// Get the user's username.
///
/// On unix-systems this differs from [`realname()`] most notably in that spaces
/// are not allowed in the username.
#[inline(always)]
pub fn username() -> Result<String> {
platform::username()
}

/// Get the user's username.
///
/// On unix-systems this differs from [`realname_os()`] most notably in that
/// spaces are not allowed in the username.
#[inline(always)]
pub fn username_os() -> Result<OsString> {
platform::username_os()
}

/// Get the user's real (full) name.
#[inline(always)]
pub fn realname() -> Result<String> {
platform::realname()
}

/// Get the user's real (full) name.
#[inline(always)]
pub fn realname_os() -> Result<OsString> {
platform::realname_os()
}

/// Get the name of the operating system distribution and (possibly) version.
///
/// Example: "Windows 10" or "Fedora 26 (Workstation Edition)"
#[inline(always)]
pub fn distro() -> Result<String> {
platform::distro()
}

/// Get the name of the operating system distribution and (possibly) version.
///
/// Example: "Windows 10" or "Fedora 26 (Workstation Edition)"
#[inline(always)]
pub fn distro_os() -> Result<OsString> {
platform::distro_os()
}

/// Get the device name (also known as "Pretty Name").
///
/// Often used to identify device for bluetooth pairing.
#[inline(always)]
pub fn devicename() -> Result<String> {
platform::devicename()
}

/// Get the device name (also known as "Pretty Name").
///
/// Often used to identify device for bluetooth pairing.
#[inline(always)]
pub fn devicename_os() -> Result<OsString> {
platform::devicename_os()
}

/// 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()`]).
#[inline(always)]
pub fn hostname() -> Result<String> {
let mut hostname = platform::hostname()?;

hostname.make_ascii_lowercase();

Ok(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()`]).
#[inline(always)]
pub fn hostname_os() -> Result<OsString> {
Ok(hostname()?.into())
}
Loading

0 comments on commit f499c74

Please sign in to comment.