Skip to content

Commit

Permalink
Make rust modules public
Browse files Browse the repository at this point in the history
  • Loading branch information
HuakunShen committed Oct 17, 2024
1 parent b36962d commit 2ce52e9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tauri-plugin-system-info"
version = "2.0.7"
version = "2.0.8"
authors = ["Huakun"]
edition = "2021"
rust-version = "1.70"
Expand All @@ -10,6 +10,10 @@ description = "A tauri plugin for retrieving system info"
license = "MIT"
repository = "https://github.com/HuakunShen/tauri-plugin-system-info"

[package.metadata.docs.rs]
rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
tauri = { version = "2.0.1" }
serde = "1.0"
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Tauri Plugin system-info

Documentation Website: https://huakunshen.github.io/tauri-plugin-system-info
TypeScript Documentation: https://huakunshen.github.io/tauri-plugin-system-info
Rust Documentation: https://docs.rs/crate/tauri-plugin-system-info

> This is a Tauri plugin for reading system information.
Expand Down Expand Up @@ -95,10 +96,24 @@ console.log(Batteries.parse(await batteries()));

The API functions in Rust are all exported, so that you can also build your own commands.

Here is a simple example:

```rust
use tauri_plugin_system_info::utils::{SysInfo, SysInfoState};
use tauri_plugin_system_info::commands;
use tauri_plugin_system_info::model::Cpu;

#[tauri::command]
fn cpu_count() -> Result<usize, String> {
let state = SysInfoState::default();
let sysinfo = state.sysinfo.lock().unwrap();
let cpu_count = sysinfo.cpu_count();
Ok(cpu_count)
}
```

See https://docs.rs/crate/tauri-plugin-system-info/ for full rust documentation.

`SysInfo` is the API struct that can be used to access all information. It's like a wrapper for `sysinfo` APIs and other crates. The reason for doing this is, some structs in third party libraries cannot be cloned or serialized, and thus cannot be sent to the frontend.

I aggregate all the APIs, do structure conversion and serilization with custom code.
Expand Down
14 changes: 14 additions & 0 deletions examples/sveltekit/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
#[cfg_attr(mobile, tauri::mobile_entry_point)]
use tauri::Manager;
use tauri_plugin_system_info::utils::{SysInfo, SysInfoState};
use tauri_plugin_system_info::commands;
use tauri_plugin_system_info::model::Cpu;

#[tauri::command]
fn cpu_count() -> Result<usize, String> {
let state = SysInfoState::default();
let sysinfo = state.sysinfo.lock().unwrap();
let cpu_count = sysinfo.cpu_count();
Ok(cpu_count)
}

pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_system_info::init())
.invoke_handler(tauri::generate_handler![
cpu_count
])
.setup(|app| {
#[cfg(debug_assertions)] // only include this code on debug builds
{
Expand Down
50 changes: 34 additions & 16 deletions examples/sveltekit/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import { onMount } from "svelte"
import { invoke } from "@tauri-apps/api/core";
import { onMount } from "svelte";
import {
allSysInfo,
AllSystemInfo,
Expand All @@ -20,6 +21,7 @@
networks,
osVersion,
processes,
Process,
refreshAll,
refreshCpu,
refreshMemory,
Expand All @@ -29,34 +31,50 @@
totalMemory,
totalSwap,
usedMemory,
usedSwap
} from "tauri-plugin-system-info-api"
import * as v from "valibot"
usedSwap,
} from "tauri-plugin-system-info-api";
import * as v from "valibot";
onMount(async () => {
reload();
invoke("cpu_count").then((res) => {
console.log(res);
});
});
async function reload() {
refreshAll();
// ! used to debug if there is any parse error. There shouldn't.
// let ret = AllSystemInfo.safeParse(await allSysInfo());
// if (!ret.success) {
// console.log(ret.error);
// }
console.log("All System Info", v.parse(AllSystemInfo, await allSysInfo()))
console.log("Memory Info", v.parse(MemoryInfo, await memoryInfo()))
console.log("Static Info", v.parse(StaticInfo, await staticInfo()))
console.log("CPU Info", v.parse(CpuInfo, await cpuInfo()))
console.log("Battery Info", v.parse(Batteries, await batteries()))
})
console.log("All System Info", v.parse(AllSystemInfo, await allSysInfo()));
console.log("Memory Info", v.parse(MemoryInfo, await memoryInfo()));
console.log("Static Info", v.parse(StaticInfo, await staticInfo()));
console.log("CPU Info", v.parse(CpuInfo, await cpuInfo()));
console.log("Battery Info", v.parse(Batteries, await batteries()));
const processesInfo = await processes();
let data: string = ""
let error: string = ""
const allPs = v.parse(
v.array(Process),
processesInfo.sort((a, b) => b.memory - a.memory)
);
console.log(allPs.filter((ps) => ps.name.toLowerCase().includes("rust")));
}
let data: string = "";
let error: string = "";
</script>

<div class="alert alert-warning flex flex-col">
<span
>Make sure you are in Tauri desktop app with development mode. Right click and inspect elements,
check console for system info logged. There is too much data to display on screen.</span
>Make sure you are in Tauri desktop app with development mode. Right click
and inspect elements, check console for system info logged. There is too
much data to display on screen.</span
>
<span
>If there is error in console, this library may not support your OS/computer, you can notify the
author.</span
>If there is error in console, this library may not support your
OS/computer, you can notify the author.</span
>
<button class="btn" on:click={reload}>Reload</button>
</div>
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use tauri::{

use std::{collections::HashMap, sync::Mutex};

mod commands;
pub mod commands;
#[cfg(desktop)]
mod desktop;
mod error;
#[cfg(mobile)]
mod mobile;
mod model;
mod utils;
pub mod model;
pub mod utils;

pub use error::{Error, Result};

Expand Down

0 comments on commit 2ce52e9

Please sign in to comment.