Skip to content

Commit

Permalink
Add trait Format over high level format types
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Feb 20, 2024
1 parent ba5e776 commit ea4fd36
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
65 changes: 63 additions & 2 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ use crate::types::c_uint;

use core::num::NonZeroU32;

///Format trait
pub trait Format {
///Returns whether format is present on clipboard
fn is_format_avail(&self) -> bool;
}

macro_rules! impl_format {
($($format:ident),+) => {
$(
impl From<$format> for u32 {
#[inline(always)]
fn from(value: $format) -> Self {
(&value).into()
}
}

impl Format for $format {
#[inline(always)]
fn is_format_avail(&self) -> bool {
crate::raw::is_format_avail(self.into())
}
}
)+

};
}

///A handle to a bitmap (HBITMAP).
pub const CF_BITMAP: c_uint = 2;
///A memory object containing a <b>BITMAPINFO</b> structure followed by the bitmap bits.
Expand Down Expand Up @@ -76,6 +103,7 @@ pub const CF_UNICODETEXT: c_uint = 13;
///Represents audio data in one of the standard wave formats.
pub const CF_WAVE: c_uint = 12;

#[derive(Copy, Clone)]
///Format to write/read from clipboard as raw bytes
///
///Has to be initialized with format `id`
Expand All @@ -95,6 +123,14 @@ impl Getter<alloc::vec::Vec<u8>> for RawData {
}
}

impl From<&RawData> for u32 {
#[inline(always)]
fn from(value: &RawData) -> Self {
value.0 as _
}
}

#[derive(Copy, Clone)]
///Format to read/write unicode string.
///
///Refer to `Getter` and `Setter`
Expand All @@ -121,6 +157,14 @@ impl<T: AsRef<str>> Setter<T> for Unicode {
}
}

impl From<&Unicode> for u32 {
#[inline(always)]
fn from(_: &Unicode) -> Self {
CF_UNICODETEXT
}
}

#[derive(Copy, Clone)]
///Format for file lists (generated by drag & drop).
///
///Corresponds to `CF_HDROP`
Expand Down Expand Up @@ -150,6 +194,14 @@ impl<T: AsRef<str>> Setter<[T]> for FileList {
}
}

impl From<&FileList> for u32 {
#[inline(always)]
fn from(_: &FileList) -> Self {
CF_HDROP
}
}

#[derive(Copy, Clone)]
///Format for bitmap images i.e. `CF_BITMAP`.
///
///Both `Getter` and `Setter` expects image as header and rgb payload
Expand All @@ -169,6 +221,13 @@ impl<T: AsRef<[u8]>> Setter<T> for Bitmap {
}
}

impl From<&Bitmap> for u32 {
#[inline(always)]
fn from(_: &Bitmap) -> Self {
CF_BITMAP
}
}

#[derive(Copy, Clone)]
///HTML Foramt
///
Expand Down Expand Up @@ -214,9 +273,11 @@ impl<T: AsRef<str>> Setter<T> for Html {
}
}

impl From<Html> for u32 {
impl From<&Html> for u32 {
#[inline(always)]
fn from(value: Html) -> Self {
fn from(value: &Html) -> Self {
value.code()
}
}

impl_format!(Html, Bitmap, RawData, Unicode, FileList);
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ extern crate alloc;
mod sys;
pub mod types;
pub mod formats;
pub use formats::Format;
mod html;
pub mod raw;
#[cfg(feature = "monitor")]
Expand Down

0 comments on commit ea4fd36

Please sign in to comment.