From ea4fd36f56237d1c8e3f06056347c46d61c0e5bd Mon Sep 17 00:00:00 2001 From: DoumanAsh Date: Tue, 20 Feb 2024 23:40:56 +0900 Subject: [PATCH] Add trait Format over high level format types --- src/formats.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 1 + 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/formats.rs b/src/formats.rs index bb96bac..190aa7e 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -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 BITMAPINFO structure followed by the bitmap bits. @@ -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` @@ -95,6 +123,14 @@ impl Getter> 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` @@ -121,6 +157,14 @@ impl> Setter 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` @@ -150,6 +194,14 @@ impl> 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 @@ -169,6 +221,13 @@ impl> Setter for Bitmap { } } +impl From<&Bitmap> for u32 { + #[inline(always)] + fn from(_: &Bitmap) -> Self { + CF_BITMAP + } +} + #[derive(Copy, Clone)] ///HTML Foramt /// @@ -214,9 +273,11 @@ impl> Setter for Html { } } -impl From 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); diff --git a/src/lib.rs b/src/lib.rs index 1ed9746..1e95f2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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")]