incular-platform 0.1.0

Shared platform abstractions for Incular
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
//! Shared clipboard and external data-transfer model.
//!
//! A transfer item may advertise several representations of the same logical
//! value (for example plain text and HTML). Payloads are reference counted and
//! lazy providers are materialized at most once, so large data is never cloned
//! merely because a transfer crosses the platform/runtime/widget boundaries.

use crate::CapabilitySupport;
use incular_core::Offset;
use std::{
    collections::HashSet,
    fmt,
    path::PathBuf,
    sync::{Arc, OnceLock},
};

/// Stable identifier for an application-defined transfer representation.
///
/// MIME types (`application/json`) and UTI-like identifiers
/// (`com.example.document`) are both accepted. Incular deliberately does not
/// reinterpret or normalize application-defined identifiers.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MediaType(String);

impl MediaType {
    pub fn new(value: impl Into<String>) -> Result<Self, TransferDataError> {
        let value = value.into();
        if value.is_empty()
            || value.trim() != value
            || value.chars().any(|character| {
                character.is_control() || character.is_whitespace() || character == '\0'
            })
        {
            return Err(TransferDataError::InvalidMediaType(value));
        }
        Ok(Self(value))
    }

    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for MediaType {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.0)
    }
}

/// Portable representation kinds shared by clipboard and external drag/drop.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum TransferFormat {
    PlainText,
    Html,
    UriList,
    Files,
    Rgba8Image,
    Custom(MediaType),
}

impl TransferFormat {
    #[must_use]
    pub fn media_type(&self) -> &str {
        match self {
            Self::PlainText => "text/plain;charset=utf-8",
            Self::Html => "text/html",
            Self::UriList => "text/uri-list",
            Self::Files => "application/x-incular-file-list",
            Self::Rgba8Image => "application/x-incular-rgba8",
            Self::Custom(media_type) => media_type.as_str(),
        }
    }
}

/// Owned, unpremultiplied RGBA8 image transfer.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TransferImage {
    width: u32,
    height: u32,
    rgba: Arc<[u8]>,
}

impl TransferImage {
    pub fn new(
        width: u32,
        height: u32,
        rgba: impl Into<Arc<[u8]>>,
    ) -> Result<Self, TransferDataError> {
        let rgba = rgba.into();
        let expected = u64::from(width)
            .checked_mul(u64::from(height))
            .and_then(|pixels| pixels.checked_mul(4))
            .and_then(|bytes| usize::try_from(bytes).ok())
            .ok_or(TransferDataError::InvalidImageDimensions { width, height })?;
        if rgba.len() != expected {
            return Err(TransferDataError::InvalidImageLength {
                width,
                height,
                actual: rgba.len(),
                expected,
            });
        }
        Ok(Self {
            width,
            height,
            rgba,
        })
    }

    #[must_use]
    pub const fn width(&self) -> u32 {
        self.width
    }

    #[must_use]
    pub const fn height(&self) -> u32 {
        self.height
    }

    #[must_use]
    pub fn rgba(&self) -> &[u8] {
        &self.rgba
    }

    #[must_use]
    pub fn shared_rgba(&self) -> Arc<[u8]> {
        self.rgba.clone()
    }
}

/// Materialized transfer data. Large payload variants retain shared storage.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TransferData {
    Text(Arc<str>),
    UriList(Arc<[String]>),
    Files(Arc<[PathBuf]>),
    Rgba8Image(TransferImage),
    Bytes(Arc<[u8]>),
}

impl TransferData {
    #[must_use]
    pub fn text(value: impl Into<String>) -> Self {
        Self::Text(Arc::<str>::from(value.into()))
    }

    #[must_use]
    pub fn uri_list(values: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self::UriList(
            values
                .into_iter()
                .map(Into::into)
                .collect::<Vec<_>>()
                .into(),
        )
    }

    #[must_use]
    pub fn files(values: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
        Self::Files(
            values
                .into_iter()
                .map(Into::into)
                .collect::<Vec<_>>()
                .into(),
        )
    }

    #[must_use]
    pub fn bytes(value: impl Into<Arc<[u8]>>) -> Self {
        Self::Bytes(value.into())
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TransferDataError {
    InvalidMediaType(String),
    InvalidImageDimensions {
        width: u32,
        height: u32,
    },
    InvalidImageLength {
        width: u32,
        height: u32,
        actual: usize,
        expected: usize,
    },
    DuplicateFormat(TransferFormat),
    TypeMismatch {
        format: TransferFormat,
        data: &'static str,
    },
    ProviderFailed(String),
}

impl fmt::Display for TransferDataError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidMediaType(value) => {
                write!(formatter, "invalid transfer media type {value:?}")
            }
            Self::InvalidImageDimensions { width, height } => {
                write!(
                    formatter,
                    "RGBA8 image dimensions overflow: {width}x{height}"
                )
            }
            Self::InvalidImageLength {
                width,
                height,
                actual,
                expected,
            } => write!(
                formatter,
                "RGBA8 image {width}x{height} requires {expected} bytes, got {actual}"
            ),
            Self::DuplicateFormat(format) => {
                write!(formatter, "duplicate transfer representation {format:?}")
            }
            Self::TypeMismatch { format, data } => write!(
                formatter,
                "transfer representation {format:?} cannot materialize {data} data"
            ),
            Self::ProviderFailed(message) => {
                write!(formatter, "transfer provider failed: {message}")
            }
        }
    }
}

impl std::error::Error for TransferDataError {}

struct LazyTransferData {
    provider: Arc<dyn Fn() -> Result<TransferData, TransferDataError> + Send + Sync>,
    value: OnceLock<Result<TransferData, TransferDataError>>,
}

impl LazyTransferData {
    fn materialize(&self) -> Result<TransferData, TransferDataError> {
        self.value.get_or_init(|| (self.provider)()).clone()
    }
}

#[derive(Clone)]
enum TransferPayload {
    Immediate(TransferData),
    Lazy(Arc<LazyTransferData>),
}

impl TransferPayload {
    fn materialize(&self) -> Result<TransferData, TransferDataError> {
        match self {
            Self::Immediate(data) => Ok(data.clone()),
            Self::Lazy(data) => data.materialize(),
        }
    }
}

impl fmt::Debug for TransferPayload {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Immediate(data) => formatter.debug_tuple("Immediate").field(data).finish(),
            Self::Lazy(_) => formatter.write_str("Lazy(<provider>)"),
        }
    }
}

impl PartialEq for TransferPayload {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Immediate(left), Self::Immediate(right)) => left == right,
            (Self::Lazy(left), Self::Lazy(right)) => Arc::ptr_eq(left, right),
            _ => false,
        }
    }
}

impl Eq for TransferPayload {}

/// One advertised representation of a logical transfer item.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TransferRepresentation {
    format: TransferFormat,
    payload: TransferPayload,
}

impl TransferRepresentation {
    fn immediate(format: TransferFormat, data: TransferData) -> Self {
        debug_assert!(data_matches_format(&format, &data));
        Self {
            format,
            payload: TransferPayload::Immediate(data),
        }
    }

    #[must_use]
    pub fn plain_text(value: impl Into<String>) -> Self {
        Self::immediate(TransferFormat::PlainText, TransferData::text(value))
    }

    #[must_use]
    pub fn html(value: impl Into<String>) -> Self {
        Self::immediate(TransferFormat::Html, TransferData::text(value))
    }

    #[must_use]
    pub fn uri_list(values: impl IntoIterator<Item = impl Into<String>>) -> Self {
        Self::immediate(TransferFormat::UriList, TransferData::uri_list(values))
    }

    #[must_use]
    pub fn files(values: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
        Self::immediate(TransferFormat::Files, TransferData::files(values))
    }

    #[must_use]
    pub fn rgba8_image(image: TransferImage) -> Self {
        Self::immediate(TransferFormat::Rgba8Image, TransferData::Rgba8Image(image))
    }

    #[must_use]
    pub fn custom(media_type: MediaType, bytes: impl Into<Arc<[u8]>>) -> Self {
        Self::immediate(
            TransferFormat::Custom(media_type),
            TransferData::bytes(bytes),
        )
    }

    /// Creates a delayed representation. The provider is invoked on first
    /// materialization and its result (including failure) is cached and shared
    /// by every clone of this representation.
    #[must_use]
    pub fn lazy(
        format: TransferFormat,
        provider: impl Fn() -> Result<TransferData, TransferDataError> + Send + Sync + 'static,
    ) -> Self {
        Self {
            format,
            payload: TransferPayload::Lazy(Arc::new(LazyTransferData {
                provider: Arc::new(provider),
                value: OnceLock::new(),
            })),
        }
    }

    #[must_use]
    pub const fn format(&self) -> &TransferFormat {
        &self.format
    }

    pub fn materialize(&self) -> Result<TransferData, TransferDataError> {
        let data = self.payload.materialize()?;
        if data_matches_format(&self.format, &data) {
            Ok(data)
        } else {
            Err(TransferDataError::TypeMismatch {
                format: self.format.clone(),
                data: transfer_data_name(&data),
            })
        }
    }
}

fn data_matches_format(format: &TransferFormat, data: &TransferData) -> bool {
    matches!(
        (format, data),
        (
            TransferFormat::PlainText | TransferFormat::Html,
            TransferData::Text(_)
        ) | (TransferFormat::UriList, TransferData::UriList(_))
            | (TransferFormat::Files, TransferData::Files(_))
            | (TransferFormat::Rgba8Image, TransferData::Rgba8Image(_))
            | (TransferFormat::Custom(_), TransferData::Bytes(_))
    )
}

fn transfer_data_name(data: &TransferData) -> &'static str {
    match data {
        TransferData::Text(_) => "text",
        TransferData::UriList(_) => "URI list",
        TransferData::Files(_) => "file list",
        TransferData::Rgba8Image(_) => "RGBA8 image",
        TransferData::Bytes(_) => "bytes",
    }
}

/// Multiple representations of one logical clipboard/drag item.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TransferItem {
    representations: Arc<[TransferRepresentation]>,
}

impl TransferItem {
    pub fn new(
        representations: impl IntoIterator<Item = TransferRepresentation>,
    ) -> Result<Self, TransferDataError> {
        let representations = representations.into_iter().collect::<Vec<_>>();
        let mut formats = HashSet::with_capacity(representations.len());
        for representation in &representations {
            if !formats.insert(representation.format.clone()) {
                return Err(TransferDataError::DuplicateFormat(
                    representation.format.clone(),
                ));
            }
        }
        Ok(Self {
            representations: representations.into(),
        })
    }

    #[must_use]
    pub fn single(representation: TransferRepresentation) -> Self {
        Self {
            representations: vec![representation].into(),
        }
    }

    #[must_use]
    pub fn representations(&self) -> &[TransferRepresentation] {
        &self.representations
    }

    #[must_use]
    pub fn representation(&self, format: &TransferFormat) -> Option<&TransferRepresentation> {
        self.representations
            .iter()
            .find(|representation| representation.format() == format)
    }

    fn filtered(&self, request: &TransferReadRequest) -> Option<Self> {
        let representations = self
            .representations
            .iter()
            .filter(|representation| request.includes(representation.format()))
            .cloned()
            .collect::<Vec<_>>();
        (!representations.is_empty()).then(|| Self {
            representations: representations.into(),
        })
    }
}

/// Ordered collection of clipboard or external drag items.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DataTransfer {
    items: Arc<[TransferItem]>,
}

impl DataTransfer {
    #[must_use]
    pub fn new(items: impl IntoIterator<Item = TransferItem>) -> Self {
        Self {
            items: items.into_iter().collect::<Vec<_>>().into(),
        }
    }

    #[must_use]
    pub fn single(item: TransferItem) -> Self {
        Self {
            items: vec![item].into(),
        }
    }

    #[must_use]
    pub fn plain_text(value: impl Into<String>) -> Self {
        Self::single(TransferItem::single(TransferRepresentation::plain_text(
            value,
        )))
    }

    #[must_use]
    pub fn files(paths: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
        Self::single(TransferItem::single(TransferRepresentation::files(paths)))
    }

    #[must_use]
    pub fn items(&self) -> &[TransferItem] {
        &self.items
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.items.is_empty()
    }

    #[must_use]
    pub fn representation(&self, format: &TransferFormat) -> Option<&TransferRepresentation> {
        self.items
            .iter()
            .find_map(|item| item.representation(format))
    }

    pub fn materialize(
        &self,
        format: &TransferFormat,
    ) -> Result<Option<TransferData>, TransferDataError> {
        self.representation(format)
            .map(TransferRepresentation::materialize)
            .transpose()
    }

    pub fn plain_text_value(&self) -> Result<Option<Arc<str>>, TransferDataError> {
        match self.materialize(&TransferFormat::PlainText)? {
            Some(TransferData::Text(text)) => Ok(Some(text)),
            Some(data) => Err(TransferDataError::TypeMismatch {
                format: TransferFormat::PlainText,
                data: transfer_data_name(&data),
            }),
            None => Ok(None),
        }
    }

    #[must_use]
    pub fn formats(&self) -> Vec<TransferFormat> {
        let mut formats = Vec::new();
        for item in self.items.iter() {
            for representation in item.representations.iter() {
                if !formats.contains(&representation.format) {
                    formats.push(representation.format.clone());
                }
            }
        }
        formats
    }

    #[must_use]
    pub fn filtered(&self, request: &TransferReadRequest) -> Self {
        Self::new(self.items.iter().filter_map(|item| item.filtered(request)))
    }
}

/// Requested clipboard representations. Empty means every representation the
/// backend can enumerate without inventing conversions.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TransferReadRequest {
    formats: Arc<[TransferFormat]>,
}

impl TransferReadRequest {
    #[must_use]
    pub fn all() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn format(format: TransferFormat) -> Self {
        Self {
            formats: vec![format].into(),
        }
    }

    #[must_use]
    pub fn formats(formats: impl IntoIterator<Item = TransferFormat>) -> Self {
        let mut unique = Vec::new();
        for format in formats {
            if !unique.contains(&format) {
                unique.push(format);
            }
        }
        Self {
            formats: unique.into(),
        }
    }

    #[must_use]
    pub fn requested_formats(&self) -> &[TransferFormat] {
        &self.formats
    }

    #[must_use]
    pub fn includes(&self, format: &TransferFormat) -> bool {
        self.formats.is_empty() || self.formats.iter().any(|requested| requested == format)
    }
}

/// Per-format support for one direction of clipboard access.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct TransferFormatCapabilities {
    pub plain_text: CapabilitySupport,
    pub html: CapabilitySupport,
    pub uri_list: CapabilitySupport,
    pub files: CapabilitySupport,
    pub rgba8_image: CapabilitySupport,
    pub custom: CapabilitySupport,
}

impl TransferFormatCapabilities {
    #[must_use]
    pub const fn support(self, format: &TransferFormat) -> CapabilitySupport {
        match format {
            TransferFormat::PlainText => self.plain_text,
            TransferFormat::Html => self.html,
            TransferFormat::UriList => self.uri_list,
            TransferFormat::Files => self.files,
            TransferFormat::Rgba8Image => self.rgba8_image,
            TransferFormat::Custom(_) => self.custom,
        }
    }

    #[must_use]
    pub const fn all(support: CapabilitySupport) -> Self {
        Self {
            plain_text: support,
            html: support,
            uri_list: support,
            files: support,
            rgba8_image: support,
            custom: support,
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ClipboardCapabilities {
    pub read: TransferFormatCapabilities,
    pub write: TransferFormatCapabilities,
    /// Whether the backend can retain a provider without materializing its data
    /// during the `write` call.
    pub lazy_write: CapabilitySupport,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClipboardWriteReport {
    pub written: Vec<TransferFormat>,
    pub skipped: Vec<TransferFormat>,
}

impl ClipboardWriteReport {
    #[must_use]
    pub fn all_written(formats: Vec<TransferFormat>) -> Self {
        Self {
            written: formats,
            skipped: Vec::new(),
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ClipboardError {
    Unavailable,
    UnsupportedFormat(TransferFormat),
    UnsupportedCombination,
    Transfer(TransferDataError),
    Backend(String),
}

impl fmt::Display for ClipboardError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unavailable => formatter.write_str("clipboard content is unavailable"),
            Self::UnsupportedFormat(format) => {
                write!(formatter, "clipboard format {format:?} is unsupported")
            }
            Self::UnsupportedCombination => {
                formatter.write_str("clipboard cannot represent this combination atomically")
            }
            Self::Transfer(error) => error.fmt(formatter),
            Self::Backend(message) => write!(formatter, "clipboard backend failed: {message}"),
        }
    }
}

impl std::error::Error for ClipboardError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Transfer(error) => Some(error),
            _ => None,
        }
    }
}

impl From<TransferDataError> for ClipboardError {
    fn from(value: TransferDataError) -> Self {
        Self::Transfer(value)
    }
}

/// Clipboard boundary shared by memory, desktop, and custom hosts.
pub trait Clipboard {
    fn capabilities(&self) -> ClipboardCapabilities;

    fn read(&mut self, request: TransferReadRequest) -> Result<DataTransfer, ClipboardError>;

    fn write(&mut self, transfer: DataTransfer) -> Result<ClipboardWriteReport, ClipboardError>;

    /// Plain-text convenience layered over [`Self::read`].
    fn get_text(&mut self) -> Option<String> {
        let transfer = self
            .read(TransferReadRequest::format(TransferFormat::PlainText))
            .ok()?;
        transfer
            .plain_text_value()
            .ok()?
            .map(|text| text.to_string())
    }

    /// Plain-text convenience layered over [`Self::write`].
    fn set_text(&mut self, text: String) {
        let _ = self.write(DataTransfer::plain_text(text));
    }
}

#[derive(Default)]
pub struct MemoryClipboard {
    transfer: Option<DataTransfer>,
}

impl Clipboard for MemoryClipboard {
    fn capabilities(&self) -> ClipboardCapabilities {
        ClipboardCapabilities {
            read: TransferFormatCapabilities::all(CapabilitySupport::Supported),
            write: TransferFormatCapabilities::all(CapabilitySupport::Supported),
            lazy_write: CapabilitySupport::Supported,
        }
    }

    fn read(&mut self, request: TransferReadRequest) -> Result<DataTransfer, ClipboardError> {
        let transfer = self.transfer.as_ref().ok_or(ClipboardError::Unavailable)?;
        let filtered = transfer.filtered(&request);
        if filtered.is_empty() {
            Err(ClipboardError::Unavailable)
        } else {
            Ok(filtered)
        }
    }

    fn write(&mut self, transfer: DataTransfer) -> Result<ClipboardWriteReport, ClipboardError> {
        let formats = transfer.formats();
        self.transfer = Some(transfer);
        Ok(ClipboardWriteReport::all_written(formats))
    }
}

/// One operation an external drag source permits a target to request.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TransferOperation {
    Copy,
    Move,
    Link,
}

/// Compact operation set used for drag/drop negotiation.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct TransferOperations(u8);

impl TransferOperations {
    pub const COPY: Self = Self(1 << 0);
    pub const MOVE: Self = Self(1 << 1);
    pub const LINK: Self = Self(1 << 2);

    #[must_use]
    pub const fn empty() -> Self {
        Self(0)
    }

    #[must_use]
    pub const fn contains(self, operation: TransferOperation) -> bool {
        let bit = match operation {
            TransferOperation::Copy => Self::COPY.0,
            TransferOperation::Move => Self::MOVE.0,
            TransferOperation::Link => Self::LINK.0,
        };
        self.0 & bit != 0
    }

    #[must_use]
    pub const fn union(self, other: Self) -> Self {
        Self(self.0 | other.0)
    }

    #[must_use]
    pub const fn preferred(self) -> Option<TransferOperation> {
        if self.contains(TransferOperation::Copy) {
            Some(TransferOperation::Copy)
        } else if self.contains(TransferOperation::Move) {
            Some(TransferOperation::Move)
        } else if self.contains(TransferOperation::Link) {
            Some(TransferOperation::Link)
        } else {
            None
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ExternalDragPhase {
    Enter,
    Over,
    Leave,
    Cancel,
    Drop,
}

/// Normalized incoming drag sample. `transfer` is the same shared transfer
/// model used by the clipboard; backends never reinterpret a file as text or
/// fabricate unsupported rich representations.
#[derive(Clone, Debug, PartialEq)]
pub struct ExternalDragEvent {
    pub phase: ExternalDragPhase,
    pub transfer: DataTransfer,
    pub position: Offset,
    pub allowed_operations: TransferOperations,
}

impl ExternalDragEvent {
    #[must_use]
    pub fn files(
        phase: ExternalDragPhase,
        paths: impl IntoIterator<Item = impl Into<PathBuf>>,
        position: Offset,
    ) -> Self {
        Self {
            phase,
            transfer: DataTransfer::files(paths),
            position,
            // Winit's file-drop abstraction has no protocol for promising a
            // source-side move/delete. Copy is therefore the only truthful
            // operation until a richer native adapter says otherwise.
            allowed_operations: TransferOperations::COPY,
        }
    }
}

/// Target negotiation result returned to a native adapter when it can honor a
/// requested drag operation.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ExternalDragResponse {
    pub requested_operation: Option<TransferOperation>,
}