From 5b4f7fbbcde4bc73cc08cdc4ba45217066b3d9f1 Mon Sep 17 00:00:00 2001 From: pacholoamit Date: Thu, 16 May 2024 08:31:16 +0800 Subject: [PATCH] feat(types): implement ts-rs --- src-tauri/Cargo.lock | 52 ++++++++++++ src-tauri/Cargo.toml | 4 +- src-tauri/src/models.rs | 57 +++++++------ .../components/disks/disk.area-chart.tsx | 2 +- .../processes.kill-verification.tsx | 13 +-- .../processes/components/processes.table.tsx | 2 +- .../processes/hooks/useKillProcess.tsx | 13 +-- .../processes/pages/processes.page.tsx | 2 +- src/lib/bindings/Cpu.ts | 4 + src/lib/bindings/Disk.ts | 4 + src/lib/bindings/GlobalCpu.ts | 4 + src/lib/bindings/Memory.ts | 4 + src/lib/bindings/Network.ts | 4 + src/lib/bindings/Process.ts | 3 + src/lib/bindings/Swap.ts | 4 + src/lib/bindings/SysInfo.ts | 4 + src/lib/bindings/Timestamp.ts | 3 + src/lib/index.ts | 1 + src/lib/types.ts | 79 +++---------------- 19 files changed, 142 insertions(+), 117 deletions(-) create mode 100644 src/lib/bindings/Cpu.ts create mode 100644 src/lib/bindings/Disk.ts create mode 100644 src/lib/bindings/GlobalCpu.ts create mode 100644 src/lib/bindings/Memory.ts create mode 100644 src/lib/bindings/Network.ts create mode 100644 src/lib/bindings/Process.ts create mode 100644 src/lib/bindings/Swap.ts create mode 100644 src/lib/bindings/SysInfo.ts create mode 100644 src/lib/bindings/Timestamp.ts diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 584e2a8d..ded2dbc8 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" + [[package]] name = "addr2line" version = "0.21.0" @@ -87,6 +93,8 @@ dependencies = [ "tauri-plugin-autostart", "tauri-plugin-log", "tauri-plugin-store", + "tokio", + "ts-rs", ] [[package]] @@ -4287,6 +4295,15 @@ dependencies = [ "utf-8", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "thin-slice" version = "0.1.1" @@ -4395,9 +4412,21 @@ dependencies = [ "num_cpus", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys 0.48.0", ] +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.63", +] + [[package]] name = "tokio-native-tls" version = "0.3.1" @@ -4587,6 +4616,29 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "ts-rs" +version = "8.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d3fa4606cdab1e9b668cc65ce2545941d01f52bc27536a195c66c55b91cb84" +dependencies = [ + "thiserror", + "ts-rs-macros", +] + +[[package]] +name = "ts-rs-macros" +version = "8.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f86ae36cbb2d58b86677ad413054feeb0712e382e822131cf9a4a1e580c419b5" +dependencies = [ + "Inflector", + "proc-macro2", + "quote", + "syn 2.0.63", + "termcolor", +] + [[package]] name = "typenum" version = "1.17.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 2b9c28c2..d090ed01 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -25,7 +25,9 @@ tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", br tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } tauri-plugin-autostart = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" } log = "^0.4" - +tokio = { version = "1.37.0", features = ["macros", "rt-multi-thread"] } +ts-rs = "8.1" +# surrealdb = { version = "1.5.0" } diff --git a/src-tauri/src/models.rs b/src-tauri/src/models.rs index 6a2d52e5..c2443ac7 100644 --- a/src-tauri/src/models.rs +++ b/src-tauri/src/models.rs @@ -1,15 +1,22 @@ use serde::{Deserialize, Serialize}; use std::path::PathBuf; +use ts_rs::TS; -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Clone, Debug, Serialize, Deserialize, TS)] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct Timestamp(pub i64); -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct Memory { + #[ts(type = "number")] pub free: u64, + #[ts(type = "number")] pub total: u64, + #[ts(type = "number")] pub used: u64, + #[ts(type = "number")] pub used_percentage: f64, pub timestamp: Timestamp, } @@ -18,8 +25,9 @@ pub trait MemoryTrait { fn get_memory(&mut self) -> Memory; } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct GlobalCpu { pub usage: f32, pub brand: String, @@ -33,8 +41,9 @@ pub trait GlobalCpuTrait { fn get_global_cpu(&mut self) -> GlobalCpu; } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct Cpu { pub name: String, pub usage: f64, @@ -45,12 +54,17 @@ pub trait CpuTrait { fn get_cpus(&mut self) -> Vec; } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct Swap { + #[ts(type = "number")] pub free: u64, + #[ts(type = "number")] pub total: u64, + #[ts(type = "number")] pub used: u64, + #[ts(type = "number")] pub used_percentage: f64, pub timestamp: Timestamp, } @@ -59,8 +73,9 @@ pub trait SwapTrait { fn get_swap(&mut self) -> Swap; } -#[derive(Clone, Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct SysInfo { pub kernel_version: String, pub os_version: String, @@ -73,11 +88,14 @@ pub trait SystemInformationTrait { fn get_system_information(&mut self) -> SysInfo; } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct Network { pub name: String, + #[ts(type = "number")] pub received: u64, + #[ts(type = "number")] pub transmitted: u64, pub timestamp: Timestamp, } @@ -86,12 +104,16 @@ pub trait NetworkTrait { fn get_networks(&mut self) -> Vec; } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct Disk { pub name: String, + #[ts(type = "number")] pub free: u64, + #[ts(type = "number")] pub total: u64, + #[ts(type = "number")] pub used: u64, pub mount_point: PathBuf, pub file_system: String, @@ -104,12 +126,15 @@ pub trait DisksTrait { fn get_disks(&mut self) -> Vec; } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize, TS)] #[serde(rename_all = "camelCase")] +#[ts(export, export_to = "../../src/lib/bindings/")] pub struct Process { pub name: String, pub pid: String, + #[ts(type = "number")] pub cpu_usage: f32, + #[ts(type = "number")] pub memory_usage: u64, pub status: String, } @@ -118,17 +143,3 @@ pub trait ProcessesTrait { fn get_processes(&mut self) -> Vec; fn kill_process(&mut self, pid: &str) -> bool; } - -#[derive(Debug, Serialize, Deserialize, Default)] -pub struct Config { - pub user: User, -} - -#[derive(Debug, Serialize, Deserialize, Default)] -pub struct User { - pub email: String, - pub first_name: String, - pub last_name: String, - pub skipped_setup: bool, - pub user_hash: String, -} diff --git a/src/features/metrics/components/disks/disk.area-chart.tsx b/src/features/metrics/components/disks/disk.area-chart.tsx index 1c9e3435..b549137d 100644 --- a/src/features/metrics/components/disks/disk.area-chart.tsx +++ b/src/features/metrics/components/disks/disk.area-chart.tsx @@ -1,6 +1,6 @@ import formatBytes from "@/features/metrics/utils/format-bytes"; import { Enumerable } from "@/hooks/useServerEventsEnumerableStore"; -import { Disk } from "@/lib/types"; +import { Disk } from "@/lib"; import AreaChart, { useAreaChartState } from "@/components/area-chart"; import { useEffect } from "react"; diff --git a/src/features/processes/components/processes.kill-verification.tsx b/src/features/processes/components/processes.kill-verification.tsx index 601a20d4..ac2e05bf 100644 --- a/src/features/processes/components/processes.kill-verification.tsx +++ b/src/features/processes/components/processes.kill-verification.tsx @@ -1,4 +1,4 @@ -import { Process } from "@/lib/types"; +import { Process } from "@/lib"; import { Button, Modal, Text, Group, Space } from "@mantine/core"; import useKillProcess from "@/features/processes/hooks/useKillProcess"; @@ -14,9 +14,7 @@ const KillProcessVerification: React.FC = ({ }) => { const [kill] = useKillProcess({ onKill() { - setRecords((records) => - records.filter((r) => r.pid !== selectedProcess?.pid) - ); + setRecords((records) => records.filter((r) => r.pid !== selectedProcess?.pid)); }, }); @@ -29,12 +27,7 @@ const KillProcessVerification: React.FC = ({ }; return ( - + Are you sure you want to kill {selectedProcess?.name}? diff --git a/src/features/processes/components/processes.table.tsx b/src/features/processes/components/processes.table.tsx index 1361763f..428ac48f 100644 --- a/src/features/processes/components/processes.table.tsx +++ b/src/features/processes/components/processes.table.tsx @@ -1,5 +1,5 @@ import { DataTable, DataTableSortStatus } from "mantine-datatable"; -import { Process } from "@/lib/types"; +import { Process } from "@/lib"; import { memo } from "react"; import { Button } from "@mantine/core"; diff --git a/src/features/processes/hooks/useKillProcess.tsx b/src/features/processes/hooks/useKillProcess.tsx index 709fc2a8..59a16887 100644 --- a/src/features/processes/hooks/useKillProcess.tsx +++ b/src/features/processes/hooks/useKillProcess.tsx @@ -1,5 +1,5 @@ import { Command, invoke } from "@/lib"; -import { KillProcessOpts, Process } from "@/lib/types"; +import { KillProcessOpts, Process } from "@/lib"; import notification from "@/utils/notification"; import { useCallback } from "react"; @@ -8,11 +8,7 @@ interface UseKillProcessOpts { onFailure?: () => void; } -const killProcess = async ( - process: Process, - onKill?: () => void, - onFailure?: () => void -) => { +const killProcess = async (process: Process, onKill?: () => void, onFailure?: () => void) => { const cmd = Command.KillProcess; const isKilled = await invoke(cmd, { pid: process.pid, @@ -36,10 +32,7 @@ const killProcess = async ( const useKillProcess = (opts: UseKillProcessOpts) => { const { onKill, onFailure } = opts; - const kill = useCallback( - (process: Process) => killProcess(process, onKill, onFailure), - [onKill, onFailure] - ); + const kill = useCallback((process: Process) => killProcess(process, onKill, onFailure), [onKill, onFailure]); return [kill] as const; }; diff --git a/src/features/processes/pages/processes.page.tsx b/src/features/processes/pages/processes.page.tsx index 25e8b3bb..c6fccedb 100644 --- a/src/features/processes/pages/processes.page.tsx +++ b/src/features/processes/pages/processes.page.tsx @@ -1,7 +1,7 @@ import { DataTableSortStatus } from "mantine-datatable"; import { TextInput } from "@mantine/core"; import { IconSearch } from "@tabler/icons-react"; -import { Process } from "@/lib/types"; +import { Process } from "@/lib"; import { useState, useEffect, useMemo } from "react"; import sortBy from "lodash.sortby"; diff --git a/src/lib/bindings/Cpu.ts b/src/lib/bindings/Cpu.ts new file mode 100644 index 00000000..24186306 --- /dev/null +++ b/src/lib/bindings/Cpu.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Timestamp } from "./Timestamp"; + +export type Cpu = { name: string, usage: number, timestamp: Timestamp, }; \ No newline at end of file diff --git a/src/lib/bindings/Disk.ts b/src/lib/bindings/Disk.ts new file mode 100644 index 00000000..930c7c58 --- /dev/null +++ b/src/lib/bindings/Disk.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Timestamp } from "./Timestamp"; + +export type Disk = { name: string, free: number, total: number, used: number, mountPoint: string, fileSystem: string, diskType: string, isRemovable: boolean, timestamp: Timestamp, }; \ No newline at end of file diff --git a/src/lib/bindings/GlobalCpu.ts b/src/lib/bindings/GlobalCpu.ts new file mode 100644 index 00000000..9d41fd56 --- /dev/null +++ b/src/lib/bindings/GlobalCpu.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Timestamp } from "./Timestamp"; + +export type GlobalCpu = { usage: number, brand: string, frequency: bigint, name: string, vendor: string, timestamp: Timestamp, }; \ No newline at end of file diff --git a/src/lib/bindings/Memory.ts b/src/lib/bindings/Memory.ts new file mode 100644 index 00000000..a3608826 --- /dev/null +++ b/src/lib/bindings/Memory.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Timestamp } from "./Timestamp"; + +export type Memory = { free: number, total: number, used: number, usedPercentage: number, timestamp: Timestamp, }; \ No newline at end of file diff --git a/src/lib/bindings/Network.ts b/src/lib/bindings/Network.ts new file mode 100644 index 00000000..878e92d7 --- /dev/null +++ b/src/lib/bindings/Network.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Timestamp } from "./Timestamp"; + +export type Network = { name: string, received: number, transmitted: number, timestamp: Timestamp, }; \ No newline at end of file diff --git a/src/lib/bindings/Process.ts b/src/lib/bindings/Process.ts new file mode 100644 index 00000000..64ca4957 --- /dev/null +++ b/src/lib/bindings/Process.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type Process = { name: string, pid: string, cpuUsage: number, memoryUsage: number, status: string, }; \ No newline at end of file diff --git a/src/lib/bindings/Swap.ts b/src/lib/bindings/Swap.ts new file mode 100644 index 00000000..5da0014b --- /dev/null +++ b/src/lib/bindings/Swap.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Timestamp } from "./Timestamp"; + +export type Swap = { free: number, total: number, used: number, usedPercentage: number, timestamp: Timestamp, }; \ No newline at end of file diff --git a/src/lib/bindings/SysInfo.ts b/src/lib/bindings/SysInfo.ts new file mode 100644 index 00000000..541719c7 --- /dev/null +++ b/src/lib/bindings/SysInfo.ts @@ -0,0 +1,4 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Timestamp } from "./Timestamp"; + +export type SysInfo = { kernelVersion: string, osVersion: string, hostname: string, coreCount: string, timestamp: Timestamp, }; \ No newline at end of file diff --git a/src/lib/bindings/Timestamp.ts b/src/lib/bindings/Timestamp.ts new file mode 100644 index 00000000..5c1d0930 --- /dev/null +++ b/src/lib/bindings/Timestamp.ts @@ -0,0 +1,3 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. + +export type Timestamp = bigint; \ No newline at end of file diff --git a/src/lib/index.ts b/src/lib/index.ts index f98516eb..e07b9d7b 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -3,3 +3,4 @@ export * from "@/lib/types"; export * from "@/lib/store"; export * from "@/lib/autostart"; export * from "@/lib/commands"; + diff --git a/src/lib/types.ts b/src/lib/types.ts index 28e486fc..71680130 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,4 +1,13 @@ import { InvokeArgs } from "@tauri-apps/api/tauri"; +export * from "@/lib/bindings/Cpu"; +export * from "@/lib/bindings/Disk"; +export * from "@/lib/bindings/GlobalCpu"; +export * from "@/lib/bindings/Memory"; +export * from "@/lib/bindings/Network"; +export * from "@/lib/bindings/Process"; +export * from "@/lib/bindings/Swap"; +export * from "@/lib/bindings/SysInfo"; +export * from "@/lib/bindings/Timestamp"; export enum Command { KillProcess = "kill_process", @@ -25,73 +34,3 @@ export interface KillProcessOpts extends InvokeArgs { } export type KillProcessResult = boolean; - -// typescript interfaces/types from models.rs - -type Timestamp = number; - -export interface SysInfo { - hostname: string; - kernelVersion: string; - osVersion: string; - coreCount: string; - timestamp: Timestamp; -} - -export interface Memory { - free: number; - total: number; - used: number; - usedPercentage: number; - timestamp: Timestamp; -} - -export interface GlobalCpu { - usage: number; - brand: string; - frequency: number; - name: string; - vendor: string; - timestamp: Timestamp; -} - -export interface Cpu { - name: string; - usage: number; - timestamp: Timestamp; -} - -export interface Swap { - free: number; - total: number; - used: number; - usedPercentage: number; - timestamp: Timestamp; -} - -export interface Network { - name: string; - received: number; - transmitted: number; - timestamp: Timestamp; -} - -export interface Disk { - name: string; - free: number; - total: number; - used: number; - mountPoint: string; - fileSystem: string; - diskType: string; - isRemovable: boolean; - timestamp: Timestamp; -} - -export interface Process { - name: string; - pid: string; - cpuUsage: number; - memoryUsage: number; - status: string; -}