Skip to content

Commit

Permalink
Merge pull request #205 from thrombe/no_panic
Browse files Browse the repository at this point in the history
don't panic in call_hyprctl_data_cmd_async and call_hyprctl_data_cmd
  • Loading branch information
yavko authored Apr 8, 2024
2 parents 7a60845 + 49824ab commit 9a1a835
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/data/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ macro_rules! impl_on {
#[async_trait]
impl HyprData for $name {
fn get() -> $crate::Result<Self> {
let data = call_hyprctl_data_cmd(DataCommands::$name);
let data = call_hyprctl_data_cmd(DataCommands::$name)?;
let deserialized: $name = serde_json::from_str(&data)?;
Ok(deserialized)
}
async fn get_async() -> $crate::Result<Self> {
let data = call_hyprctl_data_cmd_async(DataCommands::$name).await;
let data = call_hyprctl_data_cmd_async(DataCommands::$name).await?;
let deserialized: $name = serde_json::from_str(&data)?;
Ok(deserialized)
}
Expand Down Expand Up @@ -148,12 +148,12 @@ macro_rules! create_data_struct {
#[async_trait]
impl HyprData for $name {
fn get() -> $crate::Result<Self> {
let data = call_hyprctl_data_cmd($cmd_kind);
let data = call_hyprctl_data_cmd($cmd_kind)?;
let deserialized: Vec<$holding_type> = serde_json::from_str(&data)?;
Ok(Self(deserialized))
}
async fn get_async() -> $crate::Result<Self> {
let data = call_hyprctl_data_cmd_async($cmd_kind).await;
let data = call_hyprctl_data_cmd_async($cmd_kind).await?;
let deserialized: Vec<$holding_type> = serde_json::from_str(&data)?;
Ok(Self(deserialized))
}
Expand Down Expand Up @@ -189,13 +189,13 @@ macro_rules! create_data_struct {
#[async_trait]
impl HyprData for $name {
fn get() -> $crate::Result<Self> {
let data = call_hyprctl_data_cmd($cmd_kind);
let data = call_hyprctl_data_cmd($cmd_kind)?;
let deserialized: HashMap<$key, $value> = serde_json::from_str(&data)?;
Ok(Self(deserialized))
}

async fn get_async() -> $crate::Result<Self> {
let data = call_hyprctl_data_cmd_async($cmd_kind).await;
let data = call_hyprctl_data_cmd_async($cmd_kind).await?;
let deserialized: HashMap<$key, $value> = serde_json::from_str(&data)?;
Ok(Self(deserialized))
}
Expand Down
26 changes: 10 additions & 16 deletions src/data/regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,26 @@ use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};

/// This private function is to call socket commands
async fn call_hyprctl_data_cmd_async(cmd: DataCommands) -> String {
async fn call_hyprctl_data_cmd_async(cmd: DataCommands) -> crate::Result<String> {
let socket_path = get_socket_path(SocketType::Command);

let command = CommandContent {
flag: CommandFlag::JSON,
data: cmd.to_string(),
};

match write_to_socket(socket_path, command).await {
Ok(data) => data,
Err(e) => panic!("A error occured while parsing the output from the hypr socket: {e:?}"),
}
write_to_socket(socket_path, command).await
}

fn call_hyprctl_data_cmd(cmd: DataCommands) -> String {
fn call_hyprctl_data_cmd(cmd: DataCommands) -> crate::Result<String> {
let socket_path = get_socket_path(SocketType::Command);

let command = CommandContent {
flag: CommandFlag::JSON,
data: cmd.to_string(),
};

match write_to_socket_sync(socket_path, command) {
Ok(data) => data,
Err(e) => panic!("A error occured while parsing the output from the hypr socket: {e:?}"),
}
write_to_socket_sync(socket_path, command)
}

/// This pub(crate) enum holds every socket command that returns data
Expand Down Expand Up @@ -186,12 +180,12 @@ pub struct Workspace {
#[async_trait]
impl HyprDataActive for Workspace {
fn get_active() -> crate::Result<Self> {
let data = call_hyprctl_data_cmd(DataCommands::ActiveWorkspace);
let data = call_hyprctl_data_cmd(DataCommands::ActiveWorkspace)?;
let deserialized: Workspace = serde_json::from_str(&data)?;
Ok(deserialized)
}
async fn get_active_async() -> crate::Result<Self> {
let data = call_hyprctl_data_cmd_async(DataCommands::ActiveWorkspace).await;
let data = call_hyprctl_data_cmd_async(DataCommands::ActiveWorkspace).await?;
let deserialized: Workspace = serde_json::from_str(&data)?;
Ok(deserialized)
}
Expand Down Expand Up @@ -259,7 +253,7 @@ struct Empty {}
#[async_trait]
impl HyprDataActiveOptional for Client {
fn get_active() -> crate::Result<Option<Self>> {
let data = call_hyprctl_data_cmd(DataCommands::ActiveWindow);
let data = call_hyprctl_data_cmd(DataCommands::ActiveWindow)?;
let res = serde_json::from_str::<Empty>(&data);
if res.is_err() {
let t = serde_json::from_str::<Client>(&data)?;
Expand All @@ -269,7 +263,7 @@ impl HyprDataActiveOptional for Client {
}
}
async fn get_active_async() -> crate::Result<Option<Self>> {
let data = call_hyprctl_data_cmd_async(DataCommands::ActiveWindow).await;
let data = call_hyprctl_data_cmd_async(DataCommands::ActiveWindow).await?;
let res = serde_json::from_str::<Empty>(&data);
if res.is_err() {
let t = serde_json::from_str::<Client>(&data)?;
Expand Down Expand Up @@ -612,7 +606,7 @@ impl HyprData for Animations {
where
Self: Sized,
{
let out = call_hyprctl_data_cmd(DataCommands::Animations);
let out = call_hyprctl_data_cmd(DataCommands::Animations)?;
let des: AnimationsRaw = serde_json::from_str(&out)?;
let AnimationsRaw(anims, beziers) = des;
let new_anims: Vec<Animation> = anims
Expand All @@ -633,7 +627,7 @@ impl HyprData for Animations {
where
Self: Sized,
{
let out = call_hyprctl_data_cmd_async(DataCommands::Animations).await;
let out = call_hyprctl_data_cmd_async(DataCommands::Animations).await?;
let des: AnimationsRaw = serde_json::from_str(&out)?;
let AnimationsRaw(anims, beziers) = des;
let new_anims: Vec<Animation> = anims
Expand Down

0 comments on commit 9a1a835

Please sign in to comment.