Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Change the all from trait impls to tryfrom impls #8

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src-tauri/src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ fn cast_rgbau8_to_u32(rgba: &Rgba<u8>) -> u32 {
}

/// use komorebi_client::Window::hwnd for get hwnd
impl From<HWND> for super::WindowForSend {
fn from(value: HWND) -> Self {
let icon = self::hwnd::get_icon_from_hwnd(value.into()).unwrap();
super::WindowForSend {
impl TryFrom<HWND> for super::WindowForSend {
type Error = anyhow::Error;

fn try_from(value: HWND) -> Result<Self, Self::Error> {
let icon = self::hwnd::get_icon_from_hwnd(value.into())?;
Ok(super::WindowForSend {
icon: super::Icon {
base64_icon: (img::convert_img_base64(&icon)),
base64_icon: (img::convert_img_base64(&icon)?),
},
accent_color: cast_rgbau8_to_u32(&img::find_most_used_color(&icon).unwrap()),
}
})
}
}
7 changes: 3 additions & 4 deletions src-tauri/src/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ use image::{DynamicImage, Rgba};

/// pngをbase64_pngに変換する
/// `format!("data:image/png;base64,{}", resp_base64)`
pub fn convert_img_base64(image_from: &DynamicImage) -> String {
pub fn convert_img_base64(image_from: &DynamicImage) -> Result<String> {
let mut image_data: Vec<u8> = Vec::new();
image_from
.write_to(
&mut std::io::Cursor::new(&mut image_data),
image::ImageFormat::Png,
)
.unwrap();
base64::Engine::encode(&base64::engine::general_purpose::STANDARD, image_data)
).context("Unable to convert an icon to base64 string")?;
Ok(base64::Engine::encode(&base64::engine::general_purpose::STANDARD, image_data))
}

/// 一番多く使われている色を`u32`で返す
Expand Down
64 changes: 36 additions & 28 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
mod icon;
mod img;

use core::panic;

use anyhow::{Context as _, Result};
use anyhow::{Context as _, Result, anyhow};
use komorebi_client::{send_query, Layout, SocketMessage, State};
use komorebi_client::{Monitor, Window, Workspace};
use tauri::Manager as _;
Expand Down Expand Up @@ -134,46 +132,52 @@ fn fetch_komorebi_state() -> Result<State> {
// HACK: クレートを分割するとimplできないという制約を乗り越えるための方法。醜い
include!("./structs.rs");

impl From<&Monitor> for AsayakeMonitorState {
fn from(value: &Monitor) -> Self {
impl TryFrom<&Monitor> for AsayakeMonitorState {
type Error = anyhow::Error;

fn try_from(value: &Monitor) -> Result<Self, Self::Error> {
let workspaces = value.workspaces();
let focusing_workspace = value.focused_workspace_idx();

let mut workspaces_for_send: Vec<WorkspaceForSend> = vec![];

for woi in workspaces {
workspaces_for_send.push(woi.into());
workspaces_for_send.push(woi.try_into()?);
}

AsayakeMonitorState {
Ok(AsayakeMonitorState {
monitor_id: value.id(),
focusing_workspace: focusing_workspace,
workspaces: workspaces_for_send,
}
})
}
}

impl From<&Workspace> for WorkspaceForSend {
fn from(value: &Workspace) -> Self {
impl TryFrom<&Workspace> for WorkspaceForSend {
type Error = anyhow::Error;

fn try_from(value: &Workspace) -> Result<Self, Self::Error> {
let mut container_for_send: Vec<ContainerForSend> = vec![];

let containers = value.containers();

for coni in containers {
container_for_send.push(coni.into());
container_for_send.push(coni.try_into()?);
}

WorkspaceForSend {
Ok(WorkspaceForSend {
items: container_for_send,
layout: value.layout().into(),
}
layout: value.layout().try_into()?,
})
}
}

impl From<&Layout> for LayoutForSend {
fn from(value: &Layout) -> Self {
impl TryFrom<&Layout> for LayoutForSend {
type Error = anyhow::Error;

fn try_from(value: &Layout) -> Result<Self, Self::Error> {
if let Layout::Default(default_layout_kind) = value {
LayoutForSend::Default(match default_layout_kind {
Ok(LayoutForSend::Default(match default_layout_kind {
komorebi_client::DefaultLayout::BSP => DefaultLayout::BSP,
komorebi_client::DefaultLayout::Columns => DefaultLayout::Columns,
komorebi_client::DefaultLayout::Rows => DefaultLayout::Rows,
Expand All @@ -186,32 +190,36 @@ impl From<&Layout> for LayoutForSend {
komorebi_client::DefaultLayout::RightMainVerticalStack => {
DefaultLayout::RightMainVerticalStack
}
})
}))
} else {
panic!("Unable to parse custom layout, asayake still doesn't have compatibility for custom layout");
Err(anyhow!("Unable to parse custom layout, asayake still doesn't have compatibility for custom layout"))
}
}
}

impl From<&komorebi_client::Container> for ContainerForSend {
fn from(value: &komorebi_client::Container) -> Self {
impl TryFrom<&komorebi_client::Container> for ContainerForSend {
type Error = anyhow::Error;

fn try_from(value: &komorebi_client::Container) -> Result<Self, Self::Error> {
let mut window_for_send: Vec<WindowForSend> = vec![];

let windows = value.windows();

for wini in windows {
window_for_send.push(wini.into());
window_for_send.push(wini.try_into()?);
}

ContainerForSend {
Ok(ContainerForSend {
windows: window_for_send,
}
})
}
}

impl From<&Window> for WindowForSend {
fn from(value: &Window) -> Self {
value.hwnd().into()
impl TryFrom<&Window> for WindowForSend {
type Error = anyhow::Error;

fn try_from(value: &Window) -> Result<Self, Self::Error> {
Ok(value.hwnd().try_into()?)
}
}

Expand All @@ -224,5 +232,5 @@ fn fetch_asayake_window_state(window_num: usize) -> AsayakeMonitorState {
let komorebi_state = fetch_komorebi_state().unwrap();
let monitor = komorebi_state.monitors.elements().get(window_num).unwrap();

monitor.into()
monitor.try_into().unwrap()
}