starship-battery-0.8.2/.cargo_vcs_info.json0000644000000001360000000000100143500ustar { "git": { "sha1": "5a06bf47669f5b4f960088d7dd4f500682e711d7" }, "path_in_vcs": "" }starship-battery-0.8.2/.editorconfig000064400000000000000000000002531046102023000156150ustar 00000000000000root = true [*] end_of_line = lf insert_final_newline = true charset = utf-8 indent_style = space indent_size = 4 trim_trailing_whitespace = true [*.yml] indent_size = 2 starship-battery-0.8.2/.github/dependabot.yml000064400000000000000000000005031046102023000173260ustar 00000000000000version: 2 updates: - package-ecosystem: cargo directory: "/" schedule: interval: daily open-pull-requests-limit: 10 labels: - "\U0001F4E6 dependencies" - package-ecosystem: github-actions directory: "/" schedule: interval: daily open-pull-requests-limit: 10 labels: - "\U0001F4E6 dependencies" starship-battery-0.8.2/.github/workflows/ci.yml000064400000000000000000000066601046102023000176630ustar 00000000000000on: [push, pull_request] name: Continuous integration env: CARGO_INCREMENTAL: 0 CARGO_NET_RETRY: 10 RUST_BACKTRACE: short RUSTUP_MAX_RETRIES: 10 jobs: test: name: Test strategy: matrix: toolchain: - stable - 1.63.0 # MSRV target: - i686-unknown-linux-gnu - x86_64-unknown-linux-gnu - x86_64-unknown-linux-musl - i686-pc-windows-msvc - x86_64-pc-windows-msvc - x86_64-apple-darwin include: - target: i686-unknown-linux-gnu os: ubuntu-latest - target: x86_64-unknown-linux-gnu os: ubuntu-latest - target: x86_64-unknown-linux-musl os: ubuntu-latest - target: i686-pc-windows-msvc os: windows-latest - target: x86_64-pc-windows-msvc os: windows-latest - target: x86_64-apple-darwin os: macos-latest runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3.5.3 - uses: actions-rs/toolchain@v1.0.7 with: toolchain: ${{ matrix.toolchain }} target: ${{ matrix.target }} override: true - uses: Swatinem/rust-cache@v2.6.0 with: key: ${{ matrix.target }} - name: Build uses: actions-rs/cargo@v1.0.3 with: use-cross: true command: build args: --target ${{ matrix.target }} --all-features --all-targets - name: Run tests uses: actions-rs/cargo@v1.0.3 with: use-cross: true command: test args: --target ${{ matrix.target }} --all-features --all-targets - name: Run clippy if: matrix.toolchain == 'stable' uses: actions-rs/cargo@v1.0.3 with: use-cross: true command: clippy args: --target ${{ matrix.target }} -- -D clippy::all build_only: name: Test [Build only] strategy: matrix: toolchain: - stable - 1.63.0 # MSRV target: - x86_64-unknown-freebsd - aarch64-unknown-linux-gnu - aarch64-unknown-linux-musl - aarch64-pc-windows-msvc - aarch64-apple-darwin include: - target: x86_64-unknown-freebsd os: ubuntu-latest - target: aarch64-unknown-linux-gnu os: ubuntu-latest - target: aarch64-unknown-linux-musl os: ubuntu-latest - target: aarch64-pc-windows-msvc os: windows-latest - target: aarch64-apple-darwin os: macos-latest runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v3.5.3 # FreeBSD requires latest cross - run: cargo install cross --git https://github.com/cross-rs/cross - uses: actions-rs/toolchain@v1.0.7 with: toolchain: ${{ matrix.toolchain }} target: ${{ matrix.target }} override: true - uses: Swatinem/rust-cache@v2.6.0 with: key: ${{ matrix.target }} - name: Build uses: actions-rs/cargo@v1.0.3 with: use-cross: true command: build args: --target ${{ matrix.target }} - name: Run clippy if: matrix.toolchain == 'stable' uses: actions-rs/cargo@v1.0.3 with: use-cross: true command: clippy args: --target ${{ matrix.target }} -- -D clippy::all starship-battery-0.8.2/.github/workflows/deploy.yml000064400000000000000000000022571046102023000205620ustar 00000000000000name: Deploy on: push: branches: - main env: CARGO_INCREMENTAL: 0 CARGO_NET_RETRY: 10 RUST_BACKTRACE: short RUSTUP_MAX_RETRIES: 10 jobs: # Update release PR release_please: name: Release Please runs-on: ubuntu-latest if: github.repository == 'starship/rust-battery' outputs: release_created: ${{ steps.release.outputs.release_created }} steps: - uses: google-github-actions/release-please-action@v3 id: release with: token: ${{ secrets.GITHUB_TOKEN }} release-type: rust bump-minor-pre-major: true # Publish starship-battery to Crates.io cargo_publish: name: Publish Cargo Package runs-on: ubuntu-latest needs: release_please if: ${{ needs.release_please.outputs.release_created == 'true' }} steps: - name: Setup | Checkout uses: actions/checkout@v3.5.3 - name: Setup | Rust uses: actions-rs/toolchain@v1.0.7 with: toolchain: stable profile: minimal override: true - name: Build | Publish run: cargo publish env: CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} starship-battery-0.8.2/.github/workflows/merge-dependabot.yml000064400000000000000000000010041046102023000224550ustar 00000000000000name: Auto-merge Dependabot PRs on: schedule: - cron: "0 * * * *" jobs: auto_merge: if: (github.event_name == 'schedule' && github.repository == 'starship/rust-battery') || (github.event_name != 'schedule') name: Auto-merge Dependabot PRs runs-on: ubuntu-latest steps: - uses: akheron/dependabot-cron-action@7c8c20dc33be141eb135aaa6280129be3e1ec4cc with: token: ${{ secrets.DEPENDABOT_GITHUB_API_TOKEN }} auto-merge: "minor" merge-method: "squash" starship-battery-0.8.2/.github/workflows/security-audit.yml000064400000000000000000000013731046102023000222370ustar 00000000000000name: Security audit on: pull_request: paths: - "**/Cargo.toml" - "**/Cargo.lock" push: paths: - "**/Cargo.toml" - "**/Cargo.lock" schedule: - cron: "0 0 * * *" jobs: security_audit: if: (github.event_name == 'schedule' && github.repository == 'starship/rust-battery') || (github.event_name != 'schedule') runs-on: ubuntu-latest steps: - name: Setup | Checkout uses: actions/checkout@v3.5.3 - name: Setup | Rust uses: actions-rs/toolchain@v1.0.7 with: toolchain: stable override: true profile: minimal - name: Test | Security Audit uses: actions-rs/audit-check@v1.2.0 with: token: ${{ secrets.GITHUB_TOKEN }} starship-battery-0.8.2/.gitignore000064400000000000000000000000771046102023000151340ustar 00000000000000/target **/target **/*.rs.bk Cargo.lock *.orig local_build.sh starship-battery-0.8.2/CHANGELOG.md000064400000000000000000000135211046102023000147530ustar 00000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [0.8.2](https://github.com/starship/rust-battery/compare/v0.8.1...v0.8.2) (2023-08-05) ### Bug Fixes * **darwin:** allow fallback to non-raw key & permit errors ([#41](https://github.com/starship/rust-battery/issues/41)) ([19f12f0](https://github.com/starship/rust-battery/commit/19f12f04c1194515131b6edd7a1f155e3c954573)) ## [0.8.1](https://github.com/starship/rust-battery/compare/v0.8.0...v0.8.1) (2023-06-08) ### Bug Fixes * use correct data source for querying max capacity on arm64-macOS ([#33](https://github.com/starship/rust-battery/issues/33)) ([fadcee6](https://github.com/starship/rust-battery/commit/fadcee6c5e052ba58e92ea0290e1d582b2609e4f)) ## [0.8.0](https://github.com/starship/rust-battery/compare/v0.7.9...v0.8.0) (2023-04-12) ### ⚠ BREAKING CHANGES * rework ci & code cleanup * rework ci & code cleanup ### Features * derive `serde` and `schemars` traits for `State` enum ([#2](https://github.com/starship/rust-battery/issues/2)) ([487ebc2](https://github.com/starship/rust-battery/commit/487ebc2f7fbd30c346c13f82a1f721fa0256d43a)) ### Bug Fixes * bump msrv ([26bea66](https://github.com/starship/rust-battery/commit/26bea66aea58ccfd8df6005a54c403bf89554b6d)) * bump msrv ([bfcf16d](https://github.com/starship/rust-battery/commit/bfcf16db3297b05565b853e2dc19cdfd03c32986)) * bump msrv further ([#27](https://github.com/starship/rust-battery/issues/27)) ([79f1359](https://github.com/starship/rust-battery/commit/79f1359b3d4ca2f3247f04b6f72a0ca2a6db5811)) * replace `mach` with `mach2` fork ([#28](https://github.com/starship/rust-battery/issues/28)) ([ada2f1b](https://github.com/starship/rust-battery/commit/ada2f1b2ab0fd8c8c6bea7ff47e623d5fa0c94ea)) ### Code Refactoring * rework ci & code cleanup ([2330404](https://github.com/starship/rust-battery/commit/2330404d7b0f57be47f733905c735f96e866e401)) * rework ci & code cleanup ([b507347](https://github.com/starship/rust-battery/commit/b507347036b237405e292c23f7ce50dfb4ab8e58)) ## [0.7.9] - 2021-11-09 - Update `nix` dependency version - Allow empty `FullyCharged` and `DesignCapacity` ## [0.7.8] - 2020-11-01 ### Fixed - Add `nix` dependency back for FreeBSD build (#76) ## [0.7.7] - 2020-10-19 ### Fixed - Update dependencies versions to fix `nightly` toolchain compilation ## [0.7.6] - 2020-08-24 - MSRV changed to Rust `1.36.0` ### Fixed - Zero energy rate is not considered as an error for Windows [#63](https://github.com/svartalf/rust-battery/issues/63) ## [0.7.5] - 2019-11-26 ### Fixed - Handling missing `energy_full_design` source files for Linux [#40](https://github.com/svartalf/rust-battery/issues/40) ## [0.7.4] - 2019-06-03 ### Fixed - `Manager::refresh` method in Linux implementation checks if battery folder is exists [#29](https://github.com/svartalf/rust-battery/issues/29) - `Battery::energy_full_design` is not falling into a infinite recursion anymore [#30](https://github.com/svartalf/rust-battery/issues/30) ## [0.7.3] - 2019-05-30 ### Fixed - `ENODEV` errors for Linux are now handled the same as `ENOENT` [#28](https://github.com/svartalf/rust-battery/issues/28) ### Changed - Relaxing `uom` dependency version to `^0.23` for `battery` crate - Relaxing `libc` dependency version to `^0.2` for `battery-ffi` crate ## [0.7.2] - 2019-05-21 ### Fixed - `Battery::state_of_health` and `Battery::state_of_charge` are always returning values in `0.0 ≤ x ≤ 1.0` interval ## [0.7.1] - 2019-03-31 ### Changed - `uom`, `core-foundation` and `libc` dependencies were updated to latest versions - Zero cycles count is considered as non-existing value for Linux [#23](https://github.com/svartalf/rust-battery/issues/23) ### Removed - `battery-cli` crate was yanked and replaced with `battop` crate (https://crates.io/crates/battop) ## [0.7.0] - 2019-03-10 ### Changed - Propagate all errors happened from `battery` and `battery-ffi` crates to the caller - Return SI measurement units from `uom` crate for almost all public `Battery` methods - Re-export used `uom` quantities and measurement units in public `battery::units` module - Rename `Battery::percentage` method into `Battery::state_of_charge` - Rename `Battery::capacity` method into `Battery::state_of_health` - Mark `battery::State` and `battery::Technology` enums as a non-exhaustive - Support multiple devices for FreeBSD and DragonFlyBSD [#17](https://github.com/svartalf/rust-battery/issues/17) - Ignore devices with `scope` attributes different from `System` for Linux [#18](https://github.com/svartalf/rust-battery/issues/18) - Update outdated `mach` dependency for Mac OS ## [0.6.2] - 2019-02-28 ### Changed - Replace looks-to-be-abandoned `CoreFoundation-sys` and `IOKit-sys` dependencies [#2](https://github.com/svartalf/rust-battery/issues/2) ### Fixed - Free hanging mach port used for communication with Mac OS IOKit ## [0.6.1] - 2019-02-27 ### Fixed - Fix energy and remaining time calculations for MacOS [#8](https://github.com/svartalf/rust-battery/issues/8), [#11](https://github.com/svartalf/rust-battery/pull/11) - Fix multiplication overflow while calculating battery percentage in Mac OS by [@mindriot101](https://github.com/mindriot101) [#10](https://github.com/svartalf/rust-battery/pull/10) - Fix wrong units for consumption graph in `battery-cli`, should be `W` instead of `Wh` [#9](https://github.com/svartalf/rust-battery/issues/9) - Fix non-uniform path import that was breaking compilation for Rust<1.32 [#6](https://github.com/svartalf/rust-battery/issues/6) - Fix `time_to_empty` and `time_to_full` calculations for Linux when charger is unplugged but driver still reports zero `energy_rate` by [@kerhong](https://github.com/kerhong) [#5](https://github.com/svartalf/rust-battery/pull/5) starship-battery-0.8.2/Cargo.lock0000644000000262260000000000100123330ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "approx" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" dependencies = [ "num-traits", ] [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "630be753d4e58660abd17930c71b647fe46c27ea6b63cc59e1e3851406972e42" [[package]] name = "cc" version = "1.0.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6c6b2562119bf28c3439f7f02db99faf0aa1a8cdfe5772a2ee155d32227239f0" dependencies = [ "libc", ] [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "core-foundation" version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" dependencies = [ "core-foundation-sys", "libc", ] [[package]] name = "core-foundation-sys" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" [[package]] name = "dyn-clone" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "304e6508efa593091e97a9abbc10f90aa7ca635b6d2784feff3c89d41dd12272" [[package]] name = "errno" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f" dependencies = [ "errno-dragonfly", "libc", "windows-sys", ] [[package]] name = "errno-dragonfly" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" dependencies = [ "cc", "libc", ] [[package]] name = "fastrand" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" [[package]] name = "itoa" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "lazycell" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "linux-raw-sys" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57bcfdad1b858c2db7c38303a6d2ad4dfaf5eb53dfeb0910128b2c26d6158503" [[package]] name = "mach2" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d0d1830bcd151a6fc4aea1369af235b36c1528fe976b8ff678683c9995eade8" dependencies = [ "libc", ] [[package]] name = "nix" version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfdda3d196821d6af13126e40375cdf7da646a96114af134d5f417a9a1dc8e1a" dependencies = [ "bitflags 1.3.2", "cfg-if", "libc", "static_assertions", ] [[package]] name = "num-traits" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" dependencies = [ "autocfg", ] [[package]] name = "proc-macro2" version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" dependencies = [ "proc-macro2", ] [[package]] name = "redox_syscall" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" dependencies = [ "bitflags 1.3.2", ] [[package]] name = "rustix" version = "0.38.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ee020b1716f0a80e2ace9b03441a749e402e86712f15f16fe8a8f75afac732f" dependencies = [ "bitflags 2.3.3", "errno", "libc", "linux-raw-sys", "windows-sys", ] [[package]] name = "ryu" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] name = "schemars" version = "0.8.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "02c613288622e5f0c3fdc5dbd4db1c5fbe752746b1d1a56a0630b78fd00de44f" dependencies = [ "dyn-clone", "schemars_derive", "serde", "serde_json", ] [[package]] name = "schemars_derive" version = "0.8.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "109da1e6b197438deb6db99952990c7f959572794b80ff93707d55a232545e7c" dependencies = [ "proc-macro2", "quote", "serde_derive_internals", "syn 1.0.109", ] [[package]] name = "serde" version = "1.0.181" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d3e73c93c3240c0bda063c239298e633114c69a888c3e37ca8bb33f343e9890" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.181" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be02f6cb0cd3a5ec20bbcfbcbd749f57daddb1a0882dc2e46a6c236c90b977ed" dependencies = [ "proc-macro2", "quote", "syn 2.0.28", ] [[package]] name = "serde_derive_internals" version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "85bf8229e7920a9f636479437026331ce11aa132b4dde37d121944a44d6e5f3c" dependencies = [ "proc-macro2", "quote", "syn 1.0.109", ] [[package]] name = "serde_json" version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c" dependencies = [ "itoa", "ryu", "serde", ] [[package]] name = "starship-battery" version = "0.8.2" dependencies = [ "approx", "cfg-if", "core-foundation", "lazycell", "libc", "mach2", "nix", "num-traits", "schemars", "serde", "tempfile", "uom", "winapi", ] [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "syn" version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "syn" version = "2.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "tempfile" version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5486094ee78b2e5038a6382ed7645bc084dc2ec433426ca4c3cb61e2007b8998" dependencies = [ "cfg-if", "fastrand", "redox_syscall", "rustix", "windows-sys", ] [[package]] name = "typenum" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" [[package]] name = "unicode-ident" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" [[package]] name = "uom" version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8362194c7a9845a7a7f3562173d6e1da3f24f7132018cb78fe77a5b4474187b2" dependencies = [ "num-traits", "typenum", ] [[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" dependencies = [ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", ] [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.48.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05d4b17490f70499f20b9e791dcf6a299785ce8af4d709018206dc5b4953e95f" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc" [[package]] name = "windows_aarch64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3" [[package]] name = "windows_i686_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241" [[package]] name = "windows_i686_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00" [[package]] name = "windows_x86_64_gnu" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1" [[package]] name = "windows_x86_64_gnullvm" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953" [[package]] name = "windows_x86_64_msvc" version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" starship-battery-0.8.2/Cargo.toml0000644000000043610000000000100123520ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" rust-version = "1.63" name = "starship-battery" version = "0.8.2" authors = ["Starship Contributors"] build = "build.rs" description = "Cross-platform information about the notebook batteries" readme = "README.md" keywords = [ "battery", "linux", "macos", "windows", "freebsd", ] categories = ["os"] license = "ISC" repository = "https://github.com/starship/rust-battery" [dependencies.cfg-if] version = "1.0" [dependencies.num-traits] version = "0.2" default_features = false [dependencies.schemars] version = "0.8.12" optional = true [dependencies.serde] version = "1.0.160" features = ["derive"] optional = true [dependencies.uom] version = "0.35" features = [ "autoconvert", "f32", "si", ] [dev-dependencies.approx] version = "0.5.1" [dev-dependencies.tempfile] version = "^3.0" [features] config-schema = [ "schemars", "serde", ] [target."cfg(any(target_os = \"dragonfly\", target_os = \"freebsd\"))".dependencies.libc] version = "~0.2" [target."cfg(any(target_os = \"dragonfly\", target_os = \"freebsd\"))".dependencies.nix] version = "~0.26" features = ["ioctl"] default-features = false [target."cfg(any(target_os = \"macos\", target_os = \"ios\"))".dependencies.core-foundation] version = "~0.9" [target."cfg(any(target_os = \"macos\", target_os = \"ios\"))".dependencies.libc] version = "^0.2" [target."cfg(any(target_os = \"macos\", target_os = \"ios\"))".dependencies.mach] version = "^0.4" package = "mach2" [target."cfg(target_os = \"linux\")".dependencies.lazycell] version = "~1.3" [target."cfg(target_os = \"windows\")".dependencies.winapi] version = "~0.3" features = [ "impl-default", "devguid", "winbase", "ioapiset", "ntdef", "setupapi", "handleapi", "errhandlingapi", "winerror", ] starship-battery-0.8.2/Cargo.toml.orig000064400000000000000000000026211046102023000160300ustar 00000000000000[package] name = "starship-battery" version = "0.8.2" # remember to update html_root_url authors = ["Starship Contributors"] edition = "2018" description = "Cross-platform information about the notebook batteries" repository = "https://github.com/starship/rust-battery" readme = "README.md" categories = ["os"] keywords = ["battery", "linux", "macos", "windows", "freebsd"] license = "ISC" build = "build.rs" rust-version = "1.63" [features] config-schema = ["schemars", "serde"] [dependencies] cfg-if = "1.0" num-traits = { version = "0.2", default_features = false } uom = { version = "0.35", features = ["autoconvert", "f32", "si"] } serde = { version = "1.0.160", features = ["derive"], optional = true } schemars = { version = "0.8.12", optional = true } [target.'cfg(target_os = "linux")'.dependencies] lazycell = "~1.3" [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] libc = "^0.2" mach = { version = "^0.4", package = "mach2" } core-foundation = "~0.9" [target.'cfg(target_os = "windows")'.dependencies] winapi = { version ="~0.3", features = ["impl-default", "devguid", "winbase", "ioapiset", "ntdef", "setupapi", "handleapi", "errhandlingapi", "winerror"] } [target.'cfg(any(target_os = "dragonfly", target_os = "freebsd"))'.dependencies] libc = "~0.2" nix = { version = "~0.26", default-features = false, features = ["ioctl"] } [dev-dependencies] tempfile = "^3.0" approx = "0.5.1" starship-battery-0.8.2/Cross.toml000064400000000000000000000001041046102023000151210ustar 00000000000000[build.env] passthrough = [ "RUST_BACKTRACE", "RUST_LOG", ] starship-battery-0.8.2/LICENSE000064400000000000000000000013661046102023000141530ustar 00000000000000ISC License Copyright (c) 2019-2021, Starship Contributors Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. starship-battery-0.8.2/README.md000064400000000000000000000062101046102023000144160ustar 00000000000000# battery [![Latest Version](https://img.shields.io/crates/v/starship-battery.svg)](https://crates.io/crates/starship-battery) [![Latest Version](https://docs.rs/starship-battery/badge.svg)](https://docs.rs/starship-battery) [![Build Status](https://github.com/starship/rust-battery/workflows/Continuous%20integration/badge.svg)](https://github.com/starship/rust-battery/actions?workflow=Continuous+integration) ![Minimum rustc version](https://img.shields.io/badge/rustc-1.63+-yellow.svg) ![ISC licensed](https://img.shields.io/badge/license-ISC-blue.svg) > Rust crate providing cross-platform information about the notebook batteries. ## Table of contents * [Overview](#overview) * [Supported platforms](#supported-platforms) * [Install](#install) * [Examples](#examples) * [FFI bindings](#ffi-bindings) * [Users](#users) * [License](#license) * [Donations](#donations) * [Contributors](#contributors) * [Backers](#backers) * [Sponsors](#sponsors) ## Overview `battery` provides a cross-platform unified API to a notebook batteries state. Its main goal is to wrap the OS-specific interfaces, cover all the hacks and legacy cases and get the batteries information (such as state of charge, energy rate, voltage and temperature) as a typed values, recalculated as necessary to be returned as a [SI measurement units](https://www.bipm.org/en/measurement-units/). ## Supported platforms * Linux 2.6.39+ * MacOS 10.10+ * iOS * Windows 7+ * FreeBSD * DragonFlyBSD Do note that iOS implementation uses IOKit bindings, your application might be automatically rejected by Apple based on that fact. Use it on your own risk. ## Install As a prerequisite, `battery` crate requires at least Rustc version **1.63** or greater. Add the following line into a `Cargo.toml`: ```toml [dependencies] battery = "0.7.8" ``` ## Examples ```rust fn main() -> Result<(), battery::Error> { let manager = battery::Manager::new()?; for (idx, maybe_battery) in manager.batteries()?.enumerate() { let battery = maybe_battery?; println!("Battery #{}:", idx); println!("Vendor: {:?}", battery.vendor()); println!("Model: {:?}", battery.model()); println!("State: {:?}", battery.state()); println!("Time to full charge: {:?}", battery.time_to_full()); println!(""); } Ok(()) } ``` See the `battery/examples/` folder in the [repository](https://github.com/starship/rust-battery/blob/main/battery/examples/simple.rs) for additional examples. ## Users This an incomplete list of the `battery` crate users. If you are using it too, send me a message and I'll add your project here! ### starship [`starship`](https://github.com/starship/starship) is a Rust port of the minimalistic, powerful, and extremely customizable prompt Spaceship ZSH.\ It is using the `battery` crate to show the the current battery level and status in a shell prompt. Here is what [@matchai](https://github.com/matchai) says: > I really appreciate how easily we were able to get your library up and running! > Battery APIs were a headache for us in predecessors of this project 😅 And there is [this tweet](https://twitter.com/matchai/status/1135906726392283136) also! starship-battery-0.8.2/build.rs000064400000000000000000000003431046102023000146050ustar 00000000000000use std::env; fn main() { match env::var("CARGO_CFG_TARGET_OS").as_ref().map(|x| &**x) { Ok("macos") | Ok("ios") => { println!("cargo:rustc-link-lib=framework=IOKit"); } _ => {} } } starship-battery-0.8.2/examples/simple.rs000064400000000000000000000013011046102023000166100ustar 00000000000000extern crate starship_battery as battery; use std::io; use std::thread; use std::time::Duration; fn main() -> battery::Result<()> { let manager = battery::Manager::new()?; let mut battery = match manager.batteries()?.next() { Some(Ok(battery)) => battery, Some(Err(e)) => { eprintln!("Unable to access battery information"); return Err(e); } None => { eprintln!("Unable to find any batteries"); return Err(io::Error::from(io::ErrorKind::NotFound).into()); } }; loop { println!("{:?}", battery); thread::sleep(Duration::from_secs(1)); manager.refresh(&mut battery)?; } } starship-battery-0.8.2/rustfmt.toml000064400000000000000000000002461046102023000155430ustar 00000000000000edition = "2018" version = "Two" wrap_comments = true comment_width = 120 max_width = 120 merge_imports = false newline_style = "Unix" struct_lit_single_line = false starship-battery-0.8.2/src/errors.rs000064400000000000000000000042141046102023000156120ustar 00000000000000//! Errors handling use std::borrow::Cow; use std::error::Error as StdError; use std::fmt; use std::io; use std::result; pub type Result = result::Result; /// Battery routines error. /// /// Since all operations are basically I/O of some kind, /// this is a thin wrapper around `::std::io::Error` with option /// to store custom description for debugging purposes. #[derive(Debug)] pub struct Error { source: io::Error, description: Option>, } impl Error { #[allow(unused)] pub(crate) fn new(e: io::Error, description: T) -> Error where T: Into>, { Error { source: e, description: Some(description.into()), } } #[allow(unused)] pub(crate) fn not_found(description: T) -> Error where T: Into>, { Error { source: io::Error::from(io::ErrorKind::NotFound), description: Some(description.into()), } } #[allow(unused)] pub(crate) fn invalid_data(description: T) -> Error where T: Into>, { Error { source: io::Error::from(io::ErrorKind::InvalidData), description: Some(description.into()), } } } impl StdError for Error { fn source(&self) -> Option<&(dyn StdError + 'static)> { Some(&self.source) } } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match &self.description { Some(desc) => write!(f, "{}", desc), None => self.source.fmt(f), } } } impl From for Error { fn from(e: io::Error) -> Self { Error { source: e, description: None, } } } #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] mod nix_impl { use std::io; use super::Error; impl From for Error { fn from(errno: nix::Error) -> Self { Error { source: io::Error::from_raw_os_error(errno as i32), description: Some(errno.desc().into()), } } } } starship-battery-0.8.2/src/lib.rs000064400000000000000000000024361046102023000150500ustar 00000000000000//! This crate provides cross-platform information about batteries. //! //! Gives access to a system independent battery state, capacity, charge and voltage values //! recalculated as necessary to be returned in [SI measurement units](https://www.bipm.org/en/measurement-units/). //! //! ## Supported platforms //! //! * Linux 2.6.39+ //! * MacOS 10.10+ //! * Windows 7+ //! * FreeBSD //! * DragonFlyBSD //! //! ## Examples //! //! For a quick example see the [Manager](struct.Manager.html) type documentation //! or [`simple.rs`](https://github.com/starship/rust-battery/blob/main/battery/examples/simple.rs) //! file in the `examples/` folder. //! //! [battop](https://crates.io/crates/battop) crate is using this library as a knowledge source, //! so check it out too for a real-life example. #![deny(unused)] #![deny(unstable_features)] #![deny(bare_trait_objects)] #![doc(html_root_url = "https://docs.rs/starship-battery/0.7.9")] #[macro_use] extern crate cfg_if; #[cfg(target_os = "windows")] #[macro_use] extern crate winapi; #[cfg(any(target_os = "dragonfly", target_os = "freebsd"))] #[macro_use] extern crate nix; mod types; #[macro_use] pub mod units; pub mod errors; mod platform; pub use self::errors::{Error, Result}; pub use self::types::{Batteries, Battery, Manager, State, Technology}; starship-battery-0.8.2/src/platform/darwin/device.rs000064400000000000000000000055701046102023000206530ustar 00000000000000// For keys reference see: https://developer.apple.com/documentation/kernel/iopmpowersource?language=objc // Additional keys worth to implement later: // * "ChargerData" -> // - ChargingVoltage // - ChargingCurrent // - NotChargingReason (?) use num_traits::identities::Zero; use std::boxed::Box; use std::fmt; use std::str; use super::traits::DataSource; use crate::platform::traits::BatteryDevice; use crate::types::{State, Technology}; use crate::units::{ElectricPotential, Energy, Power, ThermodynamicTemperature, Time}; use crate::Result; pub struct IoKitDevice { source: Box, } impl IoKitDevice { pub fn get_mut_ref(&mut self) -> &mut dyn DataSource { &mut self.source } pub fn refresh(&mut self) -> Result<()> { self.source.refresh() } } impl BatteryDevice for IoKitDevice { fn energy(&self) -> Energy { self.source.current_capacity() * self.source.voltage() } fn energy_full(&self) -> Energy { self.source.max_capacity() * self.source.voltage() } fn energy_full_design(&self) -> Energy { self.source.design_capacity() * self.source.voltage() } fn energy_rate(&self) -> Power { self.source.amperage() * self.source.voltage() } fn state(&self) -> State { match () { _ if !self.source.external_connected() => State::Discharging, _ if self.source.is_charging() => State::Charging, _ if self.source.current_capacity().is_zero() => State::Empty, _ if self.source.fully_charged() => State::Full, _ => State::Unknown, } } fn voltage(&self) -> ElectricPotential { self.source.voltage() } fn temperature(&self) -> Option { self.source.temperature() } fn vendor(&self) -> Option<&str> { self.source.manufacturer() } fn model(&self) -> Option<&str> { self.source.device_name() } fn serial_number(&self) -> Option<&str> { self.source.serial_number() } fn technology(&self) -> Technology { Technology::Unknown } fn cycle_count(&self) -> Option { self.source.cycle_count() } fn time_to_full(&self) -> Option