Skip to content

Commit

Permalink
refactor: Move locate feature to hard optional dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Oct 25, 2024
1 parent 31f4b41 commit 72dac43
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 45 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,5 @@ jobs:
uses: dtolnay/rust-toolchain@stable
- name: Install wasm-pack
uses: taiki-e/install-action@wasm-pack
- name: Run wasm tests (`--no-default-features`)
run: wasm-pack test --node --no-default-features
- name: Run wasm tests (`--all-features`)
run: wasm-pack test --node --all-features
- name: Run wasm tests
run: wasm-pack test --node
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 2 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ categories = ["config", "filesystem"]
rust-version = "1.70.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
default = ["locate"]
locate = ["locate_backend"]

[dependencies]
crc = "3.0"
keyvalues-parser = "0.2"
Expand All @@ -29,26 +24,19 @@ serde = { version = "1.0.0", features = ["derive"] }

# Platform-specific dependencies used for locating the steam dir
[target."cfg(target_os=\"windows\")".dependencies]
locate_backend = { package = "winreg", version = "0.51", optional = true }
winreg = "0.51"
[target."cfg(any(target_os=\"macos\", target_os=\"linux\"))".dependencies]
locate_backend = { package = "home", version = "0.5.9", optional = true }
# Other platforms aren't supported for locating, so we use a dummy package that
# we already depend on since it won't be used for anything
[target."cfg(not(any(target_os=\"windows\", target_os=\"macos\", target_os=\"linux\")))".dependencies]
locate_backend = { package = "serde_derive", version = "1.0.0", optional = true }
home = "0.5.9"

[dev-dependencies]
insta = { version = "1.34.0", features = ["ron"] }
wasm-bindgen-test = "0.3.39"

[[example]]
name = "appmanifest"
required-features = ["locate"]

[[example]]
name = "overview"
required-features = ["locate"]

[[example]]
name = "shortcuts"
required-features = ["locate"]
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ Simply add `steamlocate` using
$ cargo add steamlocate
```

## Feature flags

Default: `locate`

| Feature flag | Description |
| :---: | :--- |
| `locate` | Enables automatically detecting the Steam installation on supported platforms (currently Windows, MacOS, and Linux). Unsupported platforms will return a runtime error. |

# Examples

## Locate the Steam installation and a specific game
Expand Down
9 changes: 6 additions & 3 deletions src/__private_tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use wasm_bindgen_test::wasm_bindgen_test;

#[wasm_bindgen_test]
#[cfg_attr(not(feature = "locate"), ignore = "Needs `locate` feature")]
#[cfg_attr(
not(any(target_os = "windows", target_os = "macos", target_os = "linux")),
ignore = "Needs `locate` feature"
)]
fn locate() {
#[cfg(not(feature = "locate"))]
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
unreachable!("Don't run ignored tests silly");
#[cfg(feature = "locate")]
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
let _ = crate::SteamDir::locate().unwrap_err();
}
17 changes: 8 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl fmt::Display for Error {
impl std::error::Error for Error {}

impl Error {
#[cfg(feature = "locate")]
pub(crate) fn locate(locate: LocateError) -> Self {
Self::FailedLocate(locate)
}
Expand Down Expand Up @@ -85,14 +84,14 @@ pub enum LocateError {
}

impl LocateError {
#[cfg(all(feature = "locate", target_os = "windows"))]
#[cfg(target_os = "windows")]
pub(crate) fn winreg(io: io::Error) -> Self {
Self::Backend(BackendError {
inner: BackendErrorInner(std::sync::Arc::new(io)),
})
}

#[cfg(all(feature = "locate", not(target_os = "windows")))]
#[cfg(any(target_os = "macos", target_os = "linux"))]
pub(crate) fn no_home() -> Self {
Self::Backend(BackendError {
inner: BackendErrorInner::NoHome,
Expand All @@ -111,24 +110,24 @@ impl fmt::Display for LocateError {

#[derive(Clone, Debug)]
pub struct BackendError {
#[cfg(feature = "locate")]
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
#[allow(dead_code)] // Only used for displaying currently
inner: BackendErrorInner,
}

impl fmt::Display for BackendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(all(feature = "locate", target_os = "windows"))]
#[cfg(target_os = "windows")]
{
write!(f, "{}", self.inner.0)
}
#[cfg(all(feature = "locate", not(target_os = "windows")))]
#[cfg(any(target_os = "macos", target_os = "linux"))]
{
match self.inner {
BackendErrorInner::NoHome => f.write_str("Unable to locate the user's $HOME"),
}
}
#[cfg(not(feature = "locate"))]
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
{
// "Use" the unused value
let _ = f;
Expand All @@ -140,10 +139,10 @@ impl fmt::Display for BackendError {
// TODO: move all this conditional junk into different modules, so that I don't have to keep
// repeating it everywhere
#[derive(Clone, Debug)]
#[cfg(all(feature = "locate", target_os = "windows"))]
#[cfg(target_os = "windows")]
struct BackendErrorInner(std::sync::Arc<io::Error>);
#[derive(Clone, Debug)]
#[cfg(all(feature = "locate", not(target_os = "windows")))]
#[cfg(any(target_os = "macos", target_os = "linux"))]
enum BackendErrorInner {
NoHome,
}
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ pub mod app;
pub mod config;
pub mod error;
pub mod library;
#[cfg(feature = "locate")]
mod locate;
pub mod shortcut;
// NOTE: exposed publicly, so that we can use them in doctests
Expand Down Expand Up @@ -170,7 +169,6 @@ impl SteamDir {
/// [`LocateError::Unsupported`][error::LocateError::Unsupported]
///
/// [See the struct docs][Self#example] for an example
#[cfg(feature = "locate")]
pub fn locate() -> Result<Self> {
let path = locate::locate_steam_dir()?;

Expand Down
4 changes: 0 additions & 4 deletions src/locate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ fn locate_steam_dir_helper() -> Result<PathBuf> {
fn locate_steam_dir_helper() -> Result<PathBuf> {
use crate::error::{Error, LocateError};

use locate_backend as winreg;
use winreg::{
enums::{HKEY_LOCAL_MACHINE, KEY_READ},
RegKey,
Expand Down Expand Up @@ -49,7 +48,6 @@ fn locate_steam_dir_helper() -> Result<PathBuf> {
#[cfg(target_os = "macos")]
fn locate_steam_dir_helper() -> Result<PathBuf> {
use crate::{error::LocateError, Error};
use locate_backend as home;
// Steam's installation location is pretty easy to find on macOS, as it's always in
// $USER/Library/Application Support
let home_dir = home::home_dir().ok_or_else(|| Error::locate(LocateError::no_home()))?;
Expand All @@ -65,8 +63,6 @@ fn locate_steam_dir_helper() -> Result<PathBuf> {

use crate::error::{Error, LocateError, ValidationError};

use locate_backend as home;

// Steam's installation location is pretty easy to find on Linux, too, thanks to the symlink in $USER
let home_dir = home::home_dir().ok_or_else(|| Error::locate(LocateError::no_home()))?;
let snap_dir = match env::var("SNAP_USER_DATA") {
Expand Down

0 comments on commit 72dac43

Please sign in to comment.