Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: separate komorebi-util crate #195

Merged
merged 11 commits into from
Feb 8, 2025
Prev Previous commit
Next Next commit
feat: add examples
  • Loading branch information
lars-berger committed Feb 7, 2025
commit 015c333dd3432111c5bf2ad360ba887d61bbd0b4
12 changes: 12 additions & 0 deletions crates/komorebi-util/examples/async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use komorebi_util::{Client, KomorebiOutput};

#[tokio::main]
async fn main() -> komorebi_util::Result<()> {
let client = Client::new()?;

while let Some(output) = client.output().await {
println!("Output: {:?}", output);
}

Ok(())
}
11 changes: 11 additions & 0 deletions crates/komorebi-util/examples/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use komorebi_util::{Client, KomorebiOutput};

fn main() -> komorebi_util::Result<()> {
let client = Client::new()?;

while let Some(output) = client.output_blocking() {
println!("Output: {:?}", output);
}

Ok(())
}
6 changes: 3 additions & 3 deletions crates/komorebi-util/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ impl KomorebiClient {
}

/// Returns the latest state from Komorebi.
pub async fn output_blocking(&self) -> crate::Result<KomorebiOutput> {
self.output_rx.recv().map_err(Error::StreamRead)
pub fn output_blocking(&self) -> crate::Result<KomorebiOutput> {
self.output_rx.blocking_recv().map_err(Error::StreamRead)
}

/// Listens for socket messages on a separate thread.
Expand Down Expand Up @@ -87,7 +87,7 @@ impl KomorebiClient {
.map_err(Error::InvalidUtf8)
.and_then(|str| {
serde_json::from_str::<Notification>(&str)
.map_err(Error::NotificationParse)
.map_err(Error::OutputParse)
})
.map(|notification| notification.state.into());

Expand Down
6 changes: 3 additions & 3 deletions crates/komorebi-util/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ pub enum Error {
#[error("Failed to initialize socket: {0}")]
SocketInitialization(#[from] std::io::Error),

#[error("Failed to parse notification: {0}")]
NotificationParse(#[from] serde_json::Error),
#[error("Failed to parse output: {0}")]
OutputParse(#[from] serde_json::Error),

#[error("Failed to read stream.")]
StreamRead,

#[error("Invalid UTF-8 in buffer: {0}")]
InvalidUtf8(#[from] std::string::FromUtf8Error),

#[error("Komorebi client already stopped.")]
#[error("Komorebi client has already been stopped.")]
AlreadyStopped,
}

Expand Down