-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix, revision: docs, copypasta (#13)
* fix, revision: docs, copypasta * fix: rustfmt * fix: ci * fix: doc test, clippy
- Loading branch information
Showing
9 changed files
with
116 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"rust-analyzer.cargo.features": [ | ||
"clipboard", | ||
"geolocation", | ||
"utils", | ||
"i18n" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,9 @@ cfg_if::cfg_if! { | |
pub mod i18n; | ||
} | ||
} | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(feature = "clipboard")] { | ||
pub mod clipboard; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters