Seregon/StratoSDK

StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.

Rust/27.3 KB/No license
crates/strato-ui-renderer/src/platform/mac/notification.rs
StratoSDK / crates / strato-ui-renderer / src / platform / mac / notification.rs
1use anyhow::{anyhow, Result};
2use chrono::DateTime;
3use cocoa::base::id;
4use cocoa::foundation::NSUInteger;
5use strato_ui_core::notification::{
6 NotificationResponse, NotificationSendError, RequestPermissionsOutcome,
7};
8 
9use super::utils::nsstring_as_str;
10 
11/// Build a Notification Response from a native notification event
12///
13/// # Safety
14///
15/// The `data` parameter must be a valid pointer to Objective-C string data
16pub unsafe fn response_from_native(
17 seconds_from_epoch: i32,
18 data: id,
19) -> Result<NotificationResponse> {
20 let data = nsstring_as_str(data)?;
21 
22 // Only set the data if it's not an empty string.
23 let data = (!data.is_empty()).then_some(data);
24 
25 let timestamp = DateTime::from_timestamp(seconds_from_epoch as i64, 0)
26 .ok_or_else(|| anyhow!("failed to convert time"))?;
27 Ok(NotificationResponse::new(
28 timestamp.naive_utc(),
29 data.map(Into::into),
30 ))
31}
32 
33/// Build a Notification send error from a native notification event
34///
35/// # Safety
36///
37/// The `error_message` parameter must be a valid pointer to Objective-C string data
38pub unsafe fn send_error_from_native(
39 error_type: NSUInteger,
40 error_message: id,
41) -> Result<NotificationSendError> {
42 let error_message = nsstring_as_str(error_message)?.to_owned();
43 
44 Ok(match error_type {
45 0 => NotificationSendError::PermissionsDenied,
46 1 => NotificationSendError::Other { error_message },
47 _ => NotificationSendError::Other { error_message },
48 })
49}
50 
51/// Build a Notification request permissions outcome from a native notification event
52///
53/// # Safety
54///
55/// The `outcome_message` parameter must be a valid pointer to Objective-C string data
56pub unsafe fn request_permissions_outcome_from_native(
57 outcome_type: NSUInteger,
58 outcome_message: id,
59) -> Result<RequestPermissionsOutcome> {
60 let outcome_message = nsstring_as_str(outcome_message)?.to_owned();
61 
62 Ok(match outcome_type {
63 0 => RequestPermissionsOutcome::Accepted,
64 1 => RequestPermissionsOutcome::PermissionsDenied,
65 2 => RequestPermissionsOutcome::OtherError {
66 error_message: outcome_message,
67 },
68 _ => RequestPermissionsOutcome::OtherError {
69 error_message: outcome_message,
70 },
71 })
72}
73