Skip to content

Commit

Permalink
feat: ipc error
Browse files Browse the repository at this point in the history
  • Loading branch information
LIMPIX31 committed Nov 19, 2023
1 parent 30dad28 commit afc1d3e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/root/mod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { invoke } from '@tauri-apps/api/primitives'
export function Root() {
useEffect(() => {
void (async () => {
console.log(await invoke('lookup_versions', { path: '/home/limpix/workspaces/launcher/minecraft' }))
await invoke('lookup_versions', { path: '~/workspaces/launcher/minecraft' }).then(console.log, console.log)
})()
})

Expand Down
1 change: 1 addition & 0 deletions crates/ipc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ edition = "2021"
[dependencies]
tauri = "2.0.0-alpha"
lookup = { path = "../lookup" }
serde = { version = "1.0.192", features = ["derive"] }
72 changes: 72 additions & 0 deletions crates/ipc/src/error/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use {
serde::{
ser::SerializeStruct,
Serialize,
Serializer,
},
std::io::ErrorKind,
};

#[derive(Debug)]
pub struct Io {
kind: ErrorKind,
code: Option<i32>,
msg: String,
}

impl Serialize for Io {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("Io", 3)?;
state.serialize_field("kind", &self.kind.to_string().replace(' ', "_"))?;
state.serialize_field("code", &self.code)?;
state.serialize_field("msg", &self.msg)?;
state.end()
}
}

#[derive(Debug, Serialize, Default)]
pub struct Unknown {
msg: String,
}

impl<T: std::error::Error> From<T> for Unknown {
fn from(value: T) -> Self {
Self {
msg: value.to_string(),
}
}
}

impl From<std::io::Error> for Io {
fn from(value: std::io::Error) -> Self {
let code = value.raw_os_error();

Self {
kind: value.kind(),
msg: value.to_string(),
code,
}
}
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum IpcError {
Io(Box<Io>),
Unknown(Box<Unknown>),
}

impl<T: std::error::Error> From<T> for IpcError {
default fn from(value: T) -> Self {
Self::Unknown(Box::new(value.into()))
}
}

impl From<std::io::Error> for IpcError {
fn from(value: std::io::Error) -> Self {
Self::Io(Box::new(value.into()))
}
}
3 changes: 3 additions & 0 deletions crates/ipc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![feature(specialization)]

mod lookup;
pub(crate) mod error;

pub use lookup::*;
9 changes: 6 additions & 3 deletions crates/ipc/src/lookup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::path::Path;
use {
crate::error::IpcError,
std::path::Path,
};

#[tauri::command]
pub async fn lookup_versions(path: &Path) -> Result<Vec<lookup::VersionOverview>, String> {
lookup::lookup_versions(path).await.map_err(|err| err.to_string())
pub async fn lookup_versions(path: &Path) -> Result<Vec<lookup::VersionOverview>, IpcError> {
Ok(lookup::lookup_versions(path).await?)
}
2 changes: 1 addition & 1 deletion crates/profile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2021"

[dependencies]
serde = { version = "1.0.192", features = ["derive"] }
tokio = { version = "1.34.0", features = ["fs"] }
tokio = { version = "1.34.0", features = ["full"] }
serde_json = "1.0.108"
16 changes: 14 additions & 2 deletions crates/profile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::io::ErrorKind;
use {
serde::{
Deserialize,
Expand All @@ -7,7 +8,10 @@ use {
collections::HashMap,
path::Path,
},
tokio::fs,
tokio::{
fs,
io::AsyncWriteExt,
},
};

#[derive(Debug, Deserialize, Serialize)]
Expand All @@ -17,7 +21,7 @@ pub struct ProfileEntry {
pub icon: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Default)]
pub struct Profile {
pub profiles: HashMap<String, ProfileEntry>,
}
Expand All @@ -27,3 +31,11 @@ pub async fn read_profile(root: &Path) -> Result<Profile, std::io::Error> {

Ok(serde_json::from_str(contents.as_str())?)
}

pub async fn create_empty_profile(root: &Path) -> Result<(), std::io::Error> {
let mut file = fs::File::create(root).await?;

file.write_all(br#"{"profiles":{}}"#).await?;

Ok(())
}

0 comments on commit afc1d3e

Please sign in to comment.