feat(os): add plugin (#346)

Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de>
This commit is contained in:
Lucas Fernandes Nogueira
2023-05-08 09:39:54 -07:00
committed by GitHub
parent bcb42b7343
commit 61e7d6ede5
19 changed files with 809 additions and 154 deletions
+26
View File
@@ -0,0 +1,26 @@
use std::path::PathBuf;
#[tauri::command]
pub fn platform() -> &'static str {
crate::platform()
}
#[tauri::command]
pub fn version() -> String {
crate::version().to_string()
}
#[tauri::command]
pub fn kind() -> String {
crate::kind().to_string()
}
#[tauri::command]
pub fn arch() -> &'static str {
crate::arch()
}
#[tauri::command]
pub fn tempdir() -> PathBuf {
crate::tempdir()
}
+13
View File
@@ -0,0 +1,13 @@
use serde::{Serialize, Serializer};
#[derive(Debug, thiserror::Error)]
pub enum Error {}
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.to_string().as_ref())
}
}
+77
View File
@@ -0,0 +1,77 @@
use std::{fmt::Display, path::PathBuf};
pub use os_info::Version;
use tauri::{
plugin::{Builder, TauriPlugin},
Runtime,
};
mod commands;
mod error;
pub use error::Error;
pub enum Kind {
Linux,
Windows,
Darwin,
IOS,
Android,
}
impl Display for Kind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Linux => write!(f, "Linux"),
Self::Windows => write!(f, "Linux_NT"),
Self::Darwin => write!(f, "Darwin"),
Self::IOS => write!(f, "iOS"),
Self::Android => write!(f, "Android"),
}
}
}
pub fn platform() -> &'static str {
match std::env::consts::OS {
"windows" => "win32",
"macos" => "darwin",
_ => std::env::consts::OS,
}
}
pub fn version() -> Version {
os_info::get().version().clone()
}
pub fn kind() -> Kind {
#[cfg(target_os = "linux")]
return Kind::Linux;
#[cfg(target_os = "windows")]
return Kind::Windows;
#[cfg(target_os = "macos")]
return Kind::Darwin;
#[cfg(target_os = "ios")]
return Kind::IOS;
#[cfg(target_os = "android")]
return Kind::Android;
}
pub fn arch() -> &'static str {
std::env::consts::ARCH
}
pub fn tempdir() -> PathBuf {
std::env::temp_dir()
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("os")
.invoke_handler(tauri::generate_handler![
commands::platform,
commands::version,
commands::kind,
commands::arch,
commands::tempdir
])
.build()
}