windows_snapshot/
lib.rs

1//! # windows-snapshot
2//!
3//! The aim is to provide a snapshot of Windows system asynchronously. It achieves this by using `WMI`.
4//!
5//! Our global state is `windows_snapshot::state::Windows` which stores the states of Windows machine at any given instance.
6//! Each state member can be updated synchronously or asynchronously, alone or alongside other states.
7//!
8//! Example:
9//!
10//! ```rust
11//! use windows_snapshot::COMLibrary;
12//!
13//! #[tokio::main]
14//! async fn main() {
15//!     let _com_con = COMLibrary::new().unwrap(); // initialise security context
16//!
17//!     let mut k = windows_snapshot::state::Windows::default();
18//!
19//!     k.async_update().await;
20//!     //k.update(); // for synchronous update
21//!
22//!     println!("{k:#?}");
23//! }
24//! ```
25
26pub mod operating_system;
27pub mod state;
28
29pub use wmi::COMLibrary;
30
31/// Macro to automatically make `update` and `async_update` for a given state field
32#[macro_export]
33macro_rules! update {
34    ($struct_name: ident, $struct_field: ident) => {
35        impl $struct_name {
36            /// Update fields synchronously
37            pub fn update(&mut self) {
38                let com_con = unsafe { COMLibrary::assume_initialized() };
39
40                let wmi_con = WMIConnection::new(com_con).unwrap();
41
42                self.last_updated = SystemTime::now();
43                self.$struct_field = wmi_con.query().unwrap();
44            }
45
46            /// Update fields asynchronously
47            pub async fn async_update(&mut self) {
48                let com_con = unsafe { COMLibrary::assume_initialized() };
49
50                let wmi_con = WMIConnection::new(com_con).unwrap();
51
52                self.last_updated = SystemTime::now();
53                self.$struct_field = wmi_con.async_query().await.unwrap();
54            }
55        }
56
57        impl Default for $struct_name {
58            /// `last_updated` defaults to the the current `SystemTime`
59            fn default() -> Self {
60                $struct_name {
61                    $struct_field: Default::default(),
62                    last_updated: SystemTime::now(),
63                }
64            }
65        }
66    };
67}