Skip to content

Commit

Permalink
fix, revision: docs, copypasta (#13)
Browse files Browse the repository at this point in the history
* fix, revision: docs, copypasta

* fix: rustfmt

* fix: ci

* fix: doc test, clippy
  • Loading branch information
DogeDark authored Jul 17, 2023
1 parent 095efca commit 0bfb5c3
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 107 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rust-analyzer.cargo.features": [
"clipboard",
"geolocation",
"utils",
"i18n"
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ categories = ["multimedia","os", "wasm"]
#members = ["./examples/*"]

[features]
clipboard = ["dep:clipboard"]
clipboard = ["dep:copypasta"]
notifications = ["dep:notify-rust"]
geolocation = ["dep:windows", "dep:futures-util", "dep:futures", "dep:web-sys", "dep:wasm-bindgen", "utils"]
color_scheme = ["dep:web-sys", "dep:wasm-bindgen", "dep:wasm-bindgen-futures"]
Expand All @@ -36,7 +36,7 @@ cfg-if = "1.0.0"
uuid = { version = "1.3.2", features = ["v4"], optional = true }
async-broadcast = { version = "0.5.1", optional = true }
# clipboard
clipboard = { version = "0.5.0", optional = true }
copypasta = { version = "0.8.2", optional = true }
# notifications
notify-rust = { version = "4.8.0", optional = true }
# geolocation
Expand All @@ -59,5 +59,7 @@ js-sys = "0.3.62"
uuid = { version = "1.3.2", features = ["v4", "js"]}

[package.metadata.docs.rs]
all-features = true
default-target = "x86_64-pc-windows-msvc"
no-default-features = true
features = ["desktop-testing"]

98 changes: 0 additions & 98 deletions src/clipboard/clipboard.rs

This file was deleted.

101 changes: 100 additions & 1 deletion src/clipboard/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,100 @@
pub mod clipboard;
//! Provides a clipboard abstraction to access the target system's clipboard.

use copypasta::{ClipboardContext, ClipboardProvider};
use std::fmt;

/// Contains the context for interacting with the clipboard.
///
/// # Examples
///
/// ```
/// use dioxus_std;
///
/// // Access the clipboard abstraction
/// let mut clipboard = dioxus_std::clipboard::Clipboard::new().unwrap();
///
/// // Get clipboard content
/// if let Ok(content) = clipboard.get_content() {
/// println!("{}", content);
/// }
///
/// // Set clipboard content
/// clipboard.set_content("Hello, Dioxus!".to_string());;
///
/// ```
pub struct Clipboard {
ctx: ClipboardContext,
}

impl Clipboard {
/// Creates a new struct to utilize the clipboard abstraction.
pub fn new() -> Result<Self, ClipboardError> {
let ctx = match ClipboardContext::new() {
Ok(ctx) => ctx,
Err(e) => return Err(ClipboardError::FailedToInit(e.to_string())),
};

Ok(Self { ctx })
}

/// Provides a [`String`] of the target system's current clipboard content.
pub fn get_content(&mut self) -> Result<String, ClipboardError> {
match self.ctx.get_contents() {
Ok(content) => Ok(content),
Err(e) => Err(ClipboardError::FailedToFetchContent(e.to_string())),
}
}

/// Set the clipboard's content to the provided [`String`]
pub fn set_content(&mut self, value: String) -> Result<(), ClipboardError> {
match self.ctx.set_contents(value) {
Ok(()) => Ok(()),
Err(e) => Err(ClipboardError::FailedToSetContent(e.to_string())),
}
}
}

/// Represents errors when utilizing the clipboard abstraction.
#[derive(Debug)]
pub enum ClipboardError {
/// Failure when initializing the clipboard.
FailedToInit(String),
/// Failure to retrieve clipboard content.
FailedToFetchContent(String),
/// Failure to set clipboard content.
FailedToSetContent(String),
}

impl std::error::Error for ClipboardError {}
impl fmt::Display for ClipboardError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ClipboardError::FailedToInit(s) => write!(f, "{}", s),
ClipboardError::FailedToFetchContent(s) => write!(f, "{}", s),
ClipboardError::FailedToSetContent(s) => write!(f, "{}", s),
}
}
}

// Tests
// This doesn't work in CI.
/*#[test]
fn test_clipboard() {
let mut clipboard = Clipboard::new().unwrap();
// Preserve user's clipboard contents when testing
let initial_content = clipboard.get_content().unwrap();
// Set the content
let new_content = String::from("Hello, Dioxus!");
clipboard.set_content(new_content.clone()).unwrap();
// Get the new content
let content = clipboard.get_content().unwrap();
// Return previous content - For some reason this only works if the test panics..?
clipboard.set_content(initial_content).unwrap();
// Check if the abstraction worked
assert_eq!(new_content, content);
}*/
2 changes: 1 addition & 1 deletion src/geolocation/platform/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub async fn get_coordinates(geolocator: &Geolocator) -> Result<Geocoordinates,
)
.map_err(|e| Error::DeviceError(format!("{:?}", e)))?;

while let Some(msg) = receiver.next().await {
if let Some(msg) = receiver.next().await {
receiver.close();
return msg;
}
Expand Down
3 changes: 1 addition & 2 deletions src/geolocation/use_geolocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ pub fn use_geolocation(cx: &ScopeState) -> Result<Geocoordinates, Error> {
});

// Get the result and return a clone
let result = coords.read().map_err(|_| Error::Poisoned)?.clone();
result
coords.read().map_err(|_| Error::Poisoned)?.clone()
}

/// Must be called before any use of the geolocation abstraction.
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ cfg_if::cfg_if! {
pub mod i18n;
}
}

cfg_if::cfg_if! {
if #[cfg(feature = "clipboard")] {
pub mod clipboard;
}
}
2 changes: 1 addition & 1 deletion src/utils/channel/use_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<MessageType: Clone> UseChannel<MessageType> {
}

/// Create a receiver for the channel.
/// You probably want to use [`use_listen_channel`].
/// You probably want to use [`super::use_listen_channel()`].
pub fn receiver(&mut self) -> Receiver<MessageType> {
self.inactive_receiver.clone().activate()
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/rw/use_rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<T> Clone for UseRw<T> {

impl<T> UseRw<T> {
pub fn read(&self) -> Result<RwLockReadGuard<'_, T>, UseRwError> {
Ok(self.value.read().map_err(|_| UseRwError::Poisoned)?)
self.value.read().map_err(|_| UseRwError::Poisoned)
}

pub fn write(&self, new_value: T) -> Result<(), UseRwError> {
Expand Down

0 comments on commit 0bfb5c3

Please sign in to comment.