diff --git a/examples/appmanifest.rs b/examples/appmanifest.rs index de732b3..f3753f2 100644 --- a/examples/appmanifest.rs +++ b/examples/appmanifest.rs @@ -1,6 +1,6 @@ use std::{env, process::exit}; -use steamlocate::InstallDir; +use steamlocate::SteamDir; fn main() { let args: Vec<_> = env::args().collect(); @@ -10,7 +10,7 @@ fn main() { } let app_id: u32 = args[1].parse().expect(" should be a u32"); - let steam_dir = InstallDir::locate().unwrap(); + let steam_dir = SteamDir::locate().unwrap(); match steam_dir.app(app_id) { Ok(Some(app)) => println!("Found app - {:#?}", app), Ok(None) => println!("No app found for {}", app_id), diff --git a/examples/overview.rs b/examples/overview.rs index 91fbf16..7b6f4ae 100644 --- a/examples/overview.rs +++ b/examples/overview.rs @@ -1,7 +1,7 @@ -use steamlocate::InstallDir; +use steamlocate::SteamDir; fn main() { - let steamdir = InstallDir::locate().unwrap(); + let steamdir = SteamDir::locate().unwrap(); println!("Steam Dir - {:?}", steamdir.path()); // TODO: use `anyhow` to make error handling here simpler diff --git a/examples/shortcuts.rs b/examples/shortcuts.rs index 39ea689..a070d07 100644 --- a/examples/shortcuts.rs +++ b/examples/shortcuts.rs @@ -1,7 +1,7 @@ //! Just prints all discovered shortcuts aka all non-Steam added games fn main() { - let mut steamdir = steamlocate::InstallDir::locate().unwrap(); + let mut steamdir = steamlocate::SteamDir::locate().unwrap(); println!("Shortcuts:"); for maybe_shortcut in steamdir.shortcuts().unwrap() { match maybe_shortcut { diff --git a/src/app.rs b/src/app.rs index ad4ff51..b38c169 100644 --- a/src/app.rs +++ b/src/app.rs @@ -43,8 +43,8 @@ impl<'library> Iterator for Iter<'library> { /// An instance of an installed Steam app. /// # Example /// ```ignore -/// # use steamlocate::InstallDir; -/// let mut steamdir = InstallDir::locate().unwrap(); +/// # use steamlocate::SteamDir; +/// let mut steamdir = SteamDir::locate().unwrap(); /// let gmod = steamdir.app(&4000); /// println!("{:#?}", gmod.unwrap()); /// ``` @@ -248,27 +248,27 @@ impl From for Universe { pub struct StateFlags(pub u64); impl StateFlags { - pub fn flags(self) -> FlagIter { + pub fn flags(self) -> StateFlagIter { self.into() } } #[derive(Clone, Debug, Default)] -pub struct FlagIter(Option); +pub struct StateFlagIter(Option); -impl FlagIter { +impl StateFlagIter { fn from_valid(valid: ValidIter) -> Self { - Self(Some(FlagIterInner::Valid(valid))) + Self(Some(StateFlagIterInner::Valid(valid))) } } -impl From for FlagIter { +impl From for StateFlagIter { fn from(state: StateFlags) -> Self { Self(Some(state.into())) } } -impl Iterator for FlagIter { +impl Iterator for StateFlagIter { type Item = StateFlag; fn next(&mut self) -> Option { @@ -278,8 +278,8 @@ impl Iterator for FlagIter { // - Valid will pull on the inner iterator till it's finished let current = std::mem::take(self); let (next, ret) = match current.0? { - FlagIterInner::Invalid => (Self::default(), StateFlag::Invalid), - FlagIterInner::Valid(mut valid) => { + StateFlagIterInner::Invalid => (Self::default(), StateFlag::Invalid), + StateFlagIterInner::Valid(mut valid) => { let ret = valid.next()?; (Self::from_valid(valid), ret) } @@ -290,13 +290,13 @@ impl Iterator for FlagIter { } #[derive(Clone, Debug, Default)] -enum FlagIterInner { +enum StateFlagIterInner { #[default] Invalid, Valid(ValidIter), } -impl From for FlagIterInner { +impl From for StateFlagIterInner { fn from(state: StateFlags) -> Self { if state.0 == 0 { Self::Invalid diff --git a/src/error.rs b/src/error.rs index ab8e595..bbf2145 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,7 +9,7 @@ pub type Result = std::result::Result; #[non_exhaustive] pub enum Error { // TODO: people would probably appreciate more context here even if it has to be opaque - FailedLocatingInstallDir, + FailedLocatingSteamDir, Io { inner: std::io::Error, path: PathBuf, @@ -31,7 +31,7 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::FailedLocatingInstallDir => f.write_str("Failed locating the steam dir"), + Self::FailedLocatingSteamDir => f.write_str("Failed locating the steam dir"), Self::Io { inner: err, path } => { write!(f, "Encountered an I/O error: {} at {}", err, path.display()) } diff --git a/src/lib.rs b/src/lib.rs index 88e3eea..cc16492 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ //! # Caching //! All functions in this crate cache their results, meaning you can call them as many times as you like and they will always return the same reference. //! -//! If you need to get uncached results, simply instantiate a new [InstallDir](https://docs.rs/steamlocate/*/steamlocate/struct.InstallDir.html). +//! If you need to get uncached results, simply instantiate a new [SteamDir](https://docs.rs/steamlocate/*/steamlocate/struct.SteamDir.html). //! //! # steamid-ng Support //! This crate supports [steamid-ng](https://docs.rs/steamid-ng) and can automatically convert [App::last_user](struct.App.html#structfield.last_user) to a [SteamID](https://docs.rs/steamid-ng/*/steamid_ng/struct.SteamID.html) for you. @@ -34,15 +34,15 @@ //! ### Locate the installed Steam directory //! ```rust,ignore //! extern crate steamlocate; -//! use steamlocate::InstallDir; +//! use steamlocate::SteamDir; //! -//! match InstallDir::locate() { +//! match SteamDir::locate() { //! Ok(steamdir) => println!("{:#?}", steamdir), //! Err(_) => panic!("Couldn't locate Steam on this computer!") //! } //! ``` //! ```ignore -//! InstallDir ( +//! SteamDir ( //! path: PathBuf: "C:\\Program Files (x86)\\Steam" //! ) //! ``` @@ -51,9 +51,9 @@ //! This will locate Garry's Mod anywhere on the filesystem. //! ```ignore //! extern crate steamlocate; -//! use steamlocate::InstallDir; +//! use steamlocate::SteamDir; //! -//! let mut steamdir = InstallDir::locate().unwrap(); +//! let mut steamdir = SteamDir::locate().unwrap(); //! match steamdir.app(&4000) { //! Some(app) => println!("{:#?}", app), //! None => panic!("Couldn't locate Garry's Mod on this computer!") @@ -72,10 +72,10 @@ //! ### Locate all Steam apps on this filesystem //! ```ignore //! extern crate steamlocate; -//! use steamlocate::{InstallDir, App}; +//! use steamlocate::{SteamDir, App}; //! use std::collections::HashMap; //! -//! let mut steamdir = InstallDir::locate().unwrap(); +//! let mut steamdir = SteamDir::locate().unwrap(); //! let apps: &HashMap> = steamdir.apps(); //! //! println!("{:#?}", apps); @@ -96,10 +96,10 @@ //! ### Locate all Steam library folders //! ```ignore //! extern crate steamlocate; -//! use steamlocate::{InstallDir, LibraryFolders}; +//! use steamlocate::{SteamDir, LibraryFolders}; //! use std::{vec, path::PathBuf}; //! -//! let mut steamdir: InstallDir = InstallDir::locate().unwrap(); +//! let mut steamdir: SteamDir = SteamDir::locate().unwrap(); //! let libraryfolders: &LibraryFolders = steamdir.libraryfolders(); //! let paths: &Vec = &libraryfolders.paths; //! @@ -146,25 +146,25 @@ pub use crate::shortcut::Shortcut; /// /// All functions of this struct will cache their results. /// -/// If you'd like to dispose of the cache or get uncached results, just instantiate a new `InstallDir`. +/// If you'd like to dispose of the cache or get uncached results, just instantiate a new `SteamDir`. /// /// # Example /// ```rust,ignore -/// # use steamlocate::InstallDir; -/// let steamdir = InstallDir::locate(); +/// # use steamlocate::SteamDir; +/// let steamdir = SteamDir::locate(); /// println!("{:#?}", steamdir.unwrap()); /// ``` /// ```ignore -/// InstallDir ( +/// SteamDir ( /// path: "C:\\Program Files (x86)\\Steam" /// ) /// ``` #[derive(Clone, Debug)] -pub struct InstallDir { +pub struct SteamDir { path: PathBuf, } -impl InstallDir { +impl SteamDir { /// The path to the Steam installation directory on this computer. /// /// Example: `C:\Program Files (x86)\Steam` @@ -190,8 +190,8 @@ impl InstallDir { /// /// # Example /// ```ignore - /// # use steamlocate::InstallDir; - /// let mut steamdir = InstallDir::locate().unwrap(); + /// # use steamlocate::SteamDir; + /// let mut steamdir = SteamDir::locate().unwrap(); /// let gmod = steamdir.app(&4000); /// println!("{:#?}", gmod.unwrap()); /// ``` @@ -235,9 +235,9 @@ impl InstallDir { shortcut::Iter::new(&self.path) } - pub fn from_steam_dir(path: &Path) -> Result { + pub fn from_steam_dir(path: &Path) -> Result { if !path.is_dir() { - return Err(Error::FailedLocatingInstallDir); + return Err(Error::FailedLocatingSteamDir); } // TODO(cosmic): should we do some kind of extra validation here? Could also use validation @@ -247,12 +247,12 @@ impl InstallDir { }) } - /// Locates the Steam installation directory on the filesystem and initializes a `InstallDir` (Windows) + /// Locates the Steam installation directory on the filesystem and initializes a `SteamDir` (Windows) /// /// Returns `None` if no Steam installation can be located. #[cfg(feature = "locate")] - pub fn locate() -> Result { - let path = locate::locate_steam_dir().ok_or(Error::FailedLocatingInstallDir)?; + pub fn locate() -> Result { + let path = locate::locate_steam_dir().ok_or(Error::FailedLocatingSteamDir)?; Ok(Self { path }) } diff --git a/src/shortcut.rs b/src/shortcut.rs index b4b606e..69c8340 100644 --- a/src/shortcut.rs +++ b/src/shortcut.rs @@ -252,14 +252,22 @@ mod tests { ); } - #[cfg(feature = "shortcuts_extras")] + #[cfg_attr( + not(feature = "shortcuts_extras"), + ignore = "Needs `shortcuts_extras` feature" + )] #[test] fn shortcuts_extras() { - let contents = include_bytes!("../tests/sample_data/shortcuts.vdf"); - let shortcuts = parse_shortcuts(contents).unwrap(); - let ideal_ids = vec![0xe89614fe02000000, 0xdb01c79902000000, 0x9d55017302000000]; - for (id, shortcut) in ideal_ids.into_iter().zip(shortcuts.iter()) { - assert_eq!(id, shortcut.steam_id()); + #[cfg(not(feature = "shortcuts_extras"))] + unreachable!(); + #[cfg(feature = "shortcuts_extras")] + { + let contents = include_bytes!("../tests/sample_data/shortcuts.vdf"); + let shortcuts = parse_shortcuts(contents).unwrap(); + let ideal_ids = vec![0xe89614fe02000000, 0xdb01c79902000000, 0x9d55017302000000]; + for (id, shortcut) in ideal_ids.into_iter().zip(shortcuts.iter()) { + assert_eq!(id, shortcut.steam_id()); + } } } } diff --git a/src/tests/test_helpers.rs b/src/tests/helpers.rs similarity index 91% rename from src/tests/test_helpers.rs rename to src/tests/helpers.rs index f7b6daa..b57a7d6 100644 --- a/src/tests/test_helpers.rs +++ b/src/tests/helpers.rs @@ -9,7 +9,7 @@ use std::{ path::{Path, PathBuf}, }; -use crate::InstallDir; +use crate::SteamDir; use serde::Serialize; use tempfile::TempDir; @@ -25,12 +25,12 @@ pub type TestError = Box; pub type TestResult = Result<(), TestError>; // TODO(cosmic): Add in functionality for providing shortcuts too -pub struct TempInstallDir { - steam_dir: crate::InstallDir, +pub struct TempSteamDir { + steam_dir: crate::SteamDir, _tmps: Vec, } -impl TryFrom for TempInstallDir { +impl TryFrom for TempSteamDir { type Error = TestError; fn try_from(app: AppFile) -> Result { @@ -38,7 +38,7 @@ impl TryFrom for TempInstallDir { } } -impl TryFrom for TempInstallDir { +impl TryFrom for TempSteamDir { type Error = TestError; fn try_from(sample_app: SampleApp) -> Result { @@ -46,24 +46,24 @@ impl TryFrom for TempInstallDir { } } -impl TempInstallDir { - pub fn builder() -> TempInstallDirBuilder { - TempInstallDirBuilder::default() +impl TempSteamDir { + pub fn builder() -> TempSteamDirBuilder { + TempSteamDirBuilder::default() } - pub fn steam_dir(&self) -> &InstallDir { + pub fn steam_dir(&self) -> &SteamDir { &self.steam_dir } } #[derive(Default)] #[must_use] -pub struct TempInstallDirBuilder { +pub struct TempSteamDirBuilder { libraries: Vec, apps: Vec, } -impl TempInstallDirBuilder { +impl TempSteamDirBuilder { pub fn app(mut self, app: AppFile) -> Self { self.apps.push(app); self @@ -75,7 +75,7 @@ impl TempInstallDirBuilder { } // Steam dir is also a library, but is laid out slightly differently than a regular library - pub fn finish(self) -> Result { + pub fn finish(self) -> Result { let tmp = test_temp_dir()?; let root_dir = tmp.path().join("test-steam-dir"); let steam_dir = root_dir.join("Steam"); @@ -94,8 +94,8 @@ impl TempInstallDirBuilder { .chain(self.libraries.into_iter().map(|library| library._tmp)) .collect(); - Ok(TempInstallDir { - steam_dir: InstallDir::from_steam_dir(&steam_dir)?, + Ok(TempSteamDir { + steam_dir: SteamDir::from_steam_dir(&steam_dir)?, _tmps: tmps, }) } @@ -290,7 +290,7 @@ impl SampleApp { #[test] fn sanity() -> TestResult { - let tmp_steam_dir = TempInstallDir::try_from(SampleApp::GarrysMod)?; + let tmp_steam_dir = TempSteamDir::try_from(SampleApp::GarrysMod)?; let steam_dir = tmp_steam_dir.steam_dir(); assert!(steam_dir.app(SampleApp::GarrysMod.id()).unwrap().is_some()); diff --git a/src/tests/mod.rs b/src/tests/mod.rs index e25a5f7..5252d8a 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,3 +1,3 @@ -pub mod test_helpers; +pub mod helpers; #[cfg(test)] mod tests; diff --git a/src/tests/tests.rs b/src/tests/tests.rs index 9b43144..1db670e 100644 --- a/src/tests/tests.rs +++ b/src/tests/tests.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use super::test_helpers::{SampleApp, TempInstallDir, TestError, TestResult}; +use super::helpers::{SampleApp, TempSteamDir, TestError, TestResult}; use crate::Error; static GMOD_ID: u32 = SampleApp::GarrysMod.id(); @@ -9,8 +9,8 @@ static GMOD_ID: u32 = SampleApp::GarrysMod.id(); // - Steam must be installed // - At least two library folders must be setup (the steam dir acts as one) // - Garry's Mod along with at least one other steam app must be installed -pub fn legacy_test_env() -> std::result::Result { - TempInstallDir::builder() +pub fn legacy_test_env() -> std::result::Result { + TempSteamDir::builder() .app(SampleApp::GarrysMod.into()) .library(SampleApp::GraveyardKeeper.try_into()?) .finish()