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
//! Portable contracts for native file and folder dialogs.
//!
//! Native window handles and native dialog objects deliberately do not appear
//! here. A desktop adapter receives these values, associates the dialog with
//! the requesting native window, and returns only paths/document descriptors.

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

/// Stable runtime identity for one file-dialog request. It is unrelated to a
/// native dialog object or operating-system handle.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FileDialogRequestId(u64);

impl FileDialogRequestId {
    #[must_use]
    pub const fn new(value: u64) -> Self {
        Self(value)
    }

    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }
}

/// One file/document path selected by the operating system or delivered by an
/// application activation. The path is preserved exactly; Incular does not
/// canonicalize it or take ownership of document persistence.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct DocumentDescriptor {
    path: PathBuf,
}

impl DocumentDescriptor {
    #[must_use]
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self { path: path.into() }
    }

    #[must_use]
    pub fn path(&self) -> &Path {
        &self.path
    }

    #[must_use]
    pub fn into_path(self) -> PathBuf {
        self.path
    }
}

impl From<PathBuf> for DocumentDescriptor {
    fn from(value: PathBuf) -> Self {
        Self::new(value)
    }
}

impl From<DocumentDescriptor> for PathBuf {
    fn from(value: DocumentDescriptor) -> Self {
        value.into_path()
    }
}

/// Application-level open-document value intentionally shared with the future
/// OS activation pipeline. It transports intent only; Incular does not create a
/// document model, load bytes, or choose a persistence policy.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct DocumentActivation {
    documents: Arc<[DocumentDescriptor]>,
}

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

    #[must_use]
    pub fn from_paths(paths: impl IntoIterator<Item = impl Into<PathBuf>>) -> Self {
        Self::new(
            paths
                .into_iter()
                .map(|path| DocumentDescriptor::new(path.into())),
        )
    }

    #[must_use]
    pub fn documents(&self) -> &[DocumentDescriptor] {
        &self.documents
    }

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

/// Stable semantic identity for a file filter. Native display labels are
/// presentation only and never determine filter identity.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FileDialogFilterId(String);

impl FileDialogFilterId {
    pub fn new(value: impl Into<String>) -> Result<Self, FileDialogValidationError> {
        let value = value.into();
        if !valid_identifier(&value) {
            return Err(FileDialogValidationError::InvalidFilterId(value));
        }
        Ok(Self(value))
    }

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

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

/// Normalized file extension without a leading dot.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FileExtension(String);

impl FileExtension {
    pub fn new(value: impl AsRef<str>) -> Result<Self, FileDialogValidationError> {
        let original = value.as_ref();
        let normalized = original.trim().trim_start_matches('.').to_ascii_lowercase();
        if normalized.is_empty()
            || normalized.chars().any(|character| {
                character.is_control()
                    || character.is_whitespace()
                    || matches!(character, '/' | '\\' | '*' | '?' | '[' | ']')
            })
        {
            return Err(FileDialogValidationError::InvalidExtension(
                original.to_owned(),
            ));
        }
        Ok(Self(normalized))
    }

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

/// Semantic family of a file content-type identifier.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum FileContentTypeKind {
    /// Internet media type such as `image/png`.
    Mime,
    /// Platform-neutral representation of a UTI-like/native type identifier
    /// such as `public.jpeg`.
    TypeIdentifier,
}

/// Semantic content type accepted by a file filter. MIME types are normalized
/// to ASCII lowercase because MIME type/subtype tokens are case-insensitive;
/// UTI-like identifiers are preserved verbatim apart from surrounding space.
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct FileContentType {
    value: String,
    kind: FileContentTypeKind,
}

impl FileContentType {
    pub fn new(value: impl AsRef<str>) -> Result<Self, FileDialogValidationError> {
        let original = value.as_ref();
        let trimmed = original.trim();
        if trimmed.is_empty()
            || trimmed.chars().any(|character| {
                character.is_control() || character.is_whitespace() || character == '\0'
            })
        {
            return Err(FileDialogValidationError::InvalidContentType(
                original.to_owned(),
            ));
        }
        let (normalized, kind) = if trimmed.contains('/') {
            let mut parts = trimmed.split('/');
            let Some(kind) = parts.next() else {
                unreachable!()
            };
            let Some(subtype) = parts.next() else {
                return Err(FileDialogValidationError::InvalidContentType(
                    original.to_owned(),
                ));
            };
            if parts.next().is_some() || kind.is_empty() || subtype.is_empty() {
                return Err(FileDialogValidationError::InvalidContentType(
                    original.to_owned(),
                ));
            }
            (trimmed.to_ascii_lowercase(), FileContentTypeKind::Mime)
        } else {
            (trimmed.to_owned(), FileContentTypeKind::TypeIdentifier)
        };
        Ok(Self {
            value: normalized,
            kind,
        })
    }

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

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

/// One semantic filter with optional native-facing presentation text.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileDialogFilter {
    id: FileDialogFilterId,
    label: Option<String>,
    extensions: Arc<[FileExtension]>,
    content_types: Arc<[FileContentType]>,
}

impl FileDialogFilter {
    pub fn new(
        id: FileDialogFilterId,
        label: Option<String>,
        extensions: impl IntoIterator<Item = FileExtension>,
        content_types: impl IntoIterator<Item = FileContentType>,
    ) -> Result<Self, FileDialogValidationError> {
        if label
            .as_ref()
            .is_some_and(|label| label.trim().is_empty() || label.chars().any(char::is_control))
        {
            return Err(FileDialogValidationError::InvalidFilterLabel);
        }
        let extensions = deduplicate(extensions);
        let content_types = deduplicate(content_types);
        if extensions.is_empty() && content_types.is_empty() {
            return Err(FileDialogValidationError::EmptyFilter(id));
        }
        Ok(Self {
            id,
            label,
            extensions: extensions.into(),
            content_types: content_types.into(),
        })
    }

    pub fn extensions(
        id: impl Into<String>,
        label: impl Into<String>,
        extensions: impl IntoIterator<Item = impl AsRef<str>>,
    ) -> Result<Self, FileDialogValidationError> {
        let extensions = extensions
            .into_iter()
            .map(FileExtension::new)
            .collect::<Result<Vec<_>, _>>()?;
        Self::new(
            FileDialogFilterId::new(id)?,
            Some(label.into()),
            extensions,
            [],
        )
    }

    pub fn content_types(
        id: impl Into<String>,
        label: impl Into<String>,
        content_types: impl IntoIterator<Item = impl AsRef<str>>,
    ) -> Result<Self, FileDialogValidationError> {
        let content_types = content_types
            .into_iter()
            .map(FileContentType::new)
            .collect::<Result<Vec<_>, _>>()?;
        Self::new(
            FileDialogFilterId::new(id)?,
            Some(label.into()),
            [],
            content_types,
        )
    }

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

    #[must_use]
    pub fn label(&self) -> Option<&str> {
        self.label.as_deref()
    }

    #[must_use]
    pub fn extensions_list(&self) -> &[FileExtension] {
        &self.extensions
    }

    #[must_use]
    pub fn content_types_list(&self) -> &[FileContentType] {
        &self.content_types
    }
}

fn deduplicate<T>(values: impl IntoIterator<Item = T>) -> Vec<T>
where
    T: Clone + Eq + std::hash::Hash,
{
    let mut seen = HashSet::new();
    values
        .into_iter()
        .filter(|value| seen.insert(value.clone()))
        .collect()
}

fn valid_identifier(value: &str) -> bool {
    !value.is_empty()
        && value.trim() == value
        && !value
            .chars()
            .any(|character| character.is_control() || character.is_whitespace())
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FileDialogValidationError {
    InvalidFilterId(String),
    InvalidFilterLabel,
    InvalidExtension(String),
    InvalidContentType(String),
    EmptyFilter(FileDialogFilterId),
    DuplicateFilterId(FileDialogFilterId),
    InvalidSuggestedFileName,
    OptionNotApplicable(FileDialogOption),
}

impl fmt::Display for FileDialogValidationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidFilterId(value) => {
                write!(formatter, "invalid file-dialog filter id {value:?}")
            }
            Self::InvalidFilterLabel => {
                formatter.write_str("file-dialog filter label must be non-empty printable text")
            }
            Self::InvalidExtension(value) => write!(formatter, "invalid file extension {value:?}"),
            Self::InvalidContentType(value) => {
                write!(formatter, "invalid file content type {value:?}")
            }
            Self::EmptyFilter(id) => write!(
                formatter,
                "file-dialog filter {id} has no extensions or content types"
            ),
            Self::DuplicateFilterId(id) => {
                write!(formatter, "duplicate file-dialog filter id {id}")
            }
            Self::InvalidSuggestedFileName => {
                formatter.write_str("suggested file name must be a single non-empty file name")
            }
            Self::OptionNotApplicable(option) => write!(
                formatter,
                "file-dialog option {option:?} does not apply to this dialog kind"
            ),
        }
    }
}

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

/// Native file-dialog operation requested by application code.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FileDialogKind {
    OpenFile,
    OpenFiles,
    SaveFile,
    SelectFolder,
}

/// Semantically important optional behavior a backend may support or reject.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FileDialogOption {
    ExtensionFilters,
    MimeTypeFilters,
    TypeIdentifierFilters,
    SuggestedFileName,
    SuggestedDirectory,
    ParentWindow,
}

/// Shared options for native file-dialog operations.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FileDialogOptions {
    title: Option<String>,
    filters: Arc<[FileDialogFilter]>,
    suggested_file_name: Option<String>,
    suggested_directory: Option<PathBuf>,
}

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

    #[must_use]
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn filters(
        mut self,
        filters: impl IntoIterator<Item = FileDialogFilter>,
    ) -> Result<Self, FileDialogValidationError> {
        let filters = filters.into_iter().collect::<Vec<_>>();
        let mut ids = HashSet::with_capacity(filters.len());
        for filter in &filters {
            if !ids.insert(filter.id.clone()) {
                return Err(FileDialogValidationError::DuplicateFilterId(
                    filter.id.clone(),
                ));
            }
        }
        self.filters = filters.into();
        Ok(self)
    }

    pub fn suggested_file_name(
        mut self,
        name: impl Into<String>,
    ) -> Result<Self, FileDialogValidationError> {
        let name = name.into();
        if name.trim().is_empty()
            || name.chars().any(char::is_control)
            || name.contains(['/', '\\'])
            || matches!(name.as_str(), "." | "..")
        {
            return Err(FileDialogValidationError::InvalidSuggestedFileName);
        }
        self.suggested_file_name = Some(name);
        Ok(self)
    }

    #[must_use]
    pub fn suggested_directory(mut self, directory: impl Into<PathBuf>) -> Self {
        self.suggested_directory = Some(directory.into());
        self
    }

    #[must_use]
    pub fn title_value(&self) -> Option<&str> {
        self.title.as_deref()
    }

    #[must_use]
    pub fn filters_value(&self) -> &[FileDialogFilter] {
        &self.filters
    }

    #[must_use]
    pub fn suggested_file_name_value(&self) -> Option<&str> {
        self.suggested_file_name.as_deref()
    }

    #[must_use]
    pub fn suggested_directory_value(&self) -> Option<&Path> {
        self.suggested_directory.as_deref()
    }
}

/// Fully validated, native-handle-free dialog request.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileDialogRequest {
    pub kind: FileDialogKind,
    pub options: FileDialogOptions,
}

impl FileDialogRequest {
    pub fn new(
        kind: FileDialogKind,
        options: FileDialogOptions,
    ) -> Result<Self, FileDialogValidationError> {
        if kind == FileDialogKind::SelectFolder {
            for filter in options.filters.iter() {
                if !filter.extensions.is_empty() {
                    return Err(FileDialogValidationError::OptionNotApplicable(
                        FileDialogOption::ExtensionFilters,
                    ));
                }
                if let Some(content_type) = filter.content_types.first() {
                    let option = match content_type.kind() {
                        FileContentTypeKind::Mime => FileDialogOption::MimeTypeFilters,
                        FileContentTypeKind::TypeIdentifier => {
                            FileDialogOption::TypeIdentifierFilters
                        }
                    };
                    return Err(FileDialogValidationError::OptionNotApplicable(option));
                }
            }
        }
        if kind != FileDialogKind::SaveFile && options.suggested_file_name.is_some() {
            return Err(FileDialogValidationError::OptionNotApplicable(
                FileDialogOption::SuggestedFileName,
            ));
        }
        Ok(Self { kind, options })
    }
}

/// Portable result payload from a native file dialog.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FileDialogSelection {
    Documents(DocumentActivation),
    Folder(PathBuf),
}

/// User-visible completion of a native file dialog. User cancellation is a
/// normal outcome rather than a backend error.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FileDialogOutcome {
    Selected(FileDialogSelection),
    Cancelled,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FileDialogError {
    InvalidRequest(FileDialogValidationError),
    ParentClosed,
    ApplicationStopped,
    UnsupportedOperation(FileDialogKind),
    UnsupportedOption(FileDialogOption),
    InvalidResult,
    Backend(String),
}

impl fmt::Display for FileDialogError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidRequest(error) => {
                write!(formatter, "invalid file-dialog request: {error}")
            }
            Self::ParentClosed => formatter.write_str("file-dialog parent window closed"),
            Self::ApplicationStopped => {
                formatter.write_str("file-dialog application service stopped")
            }
            Self::UnsupportedOperation(kind) => {
                write!(formatter, "file-dialog operation {kind:?} is unsupported")
            }
            Self::UnsupportedOption(option) => {
                write!(formatter, "file-dialog option {option:?} is unsupported")
            }
            Self::InvalidResult => formatter
                .write_str("file-dialog backend returned a result incompatible with its request"),
            Self::Backend(message) => write!(formatter, "file-dialog backend failed: {message}"),
        }
    }
}

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

impl From<FileDialogValidationError> for FileDialogError {
    fn from(value: FileDialogValidationError) -> Self {
        Self::InvalidRequest(value)
    }
}

/// Detailed native-dialog capabilities. Keeping options independent prevents a
/// backend from claiming full semantic support merely because it can show a
/// basic file picker.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct FileDialogCapabilities {
    pub open_file: CapabilitySupport,
    pub open_files: CapabilitySupport,
    pub save_file: CapabilitySupport,
    pub select_folder: CapabilitySupport,
    pub extension_filters: CapabilitySupport,
    pub mime_type_filters: CapabilitySupport,
    pub type_identifier_filters: CapabilitySupport,
    pub suggested_file_name: CapabilitySupport,
    pub suggested_directory: CapabilitySupport,
    pub parent_window: CapabilitySupport,
}

impl FileDialogCapabilities {
    #[must_use]
    pub const fn all(support: CapabilitySupport) -> Self {
        Self {
            open_file: support,
            open_files: support,
            save_file: support,
            select_folder: support,
            extension_filters: support,
            mime_type_filters: support,
            type_identifier_filters: support,
            suggested_file_name: support,
            suggested_directory: support,
            parent_window: support,
        }
    }

    #[must_use]
    pub const fn operation(self, kind: FileDialogKind) -> CapabilitySupport {
        match kind {
            FileDialogKind::OpenFile => self.open_file,
            FileDialogKind::OpenFiles => self.open_files,
            FileDialogKind::SaveFile => self.save_file,
            FileDialogKind::SelectFolder => self.select_folder,
        }
    }

    pub fn validate_request(&self, request: &FileDialogRequest) -> Result<(), FileDialogError> {
        if self.operation(request.kind) != CapabilitySupport::Supported {
            return Err(FileDialogError::UnsupportedOperation(request.kind));
        }
        if self.parent_window != CapabilitySupport::Supported {
            return Err(FileDialogError::UnsupportedOption(
                FileDialogOption::ParentWindow,
            ));
        }
        if request
            .options
            .filters
            .iter()
            .any(|filter| !filter.extensions.is_empty())
            && self.extension_filters != CapabilitySupport::Supported
        {
            return Err(FileDialogError::UnsupportedOption(
                FileDialogOption::ExtensionFilters,
            ));
        }
        for content_type in request
            .options
            .filters
            .iter()
            .flat_map(|filter| filter.content_types.iter())
        {
            let (support, option) = match content_type.kind() {
                FileContentTypeKind::Mime => {
                    (self.mime_type_filters, FileDialogOption::MimeTypeFilters)
                }
                FileContentTypeKind::TypeIdentifier => (
                    self.type_identifier_filters,
                    FileDialogOption::TypeIdentifierFilters,
                ),
            };
            if support != CapabilitySupport::Supported {
                return Err(FileDialogError::UnsupportedOption(option));
            }
        }
        if request.options.suggested_file_name.is_some()
            && self.suggested_file_name != CapabilitySupport::Supported
        {
            return Err(FileDialogError::UnsupportedOption(
                FileDialogOption::SuggestedFileName,
            ));
        }
        if request.options.suggested_directory.is_some()
            && self.suggested_directory != CapabilitySupport::Supported
        {
            return Err(FileDialogError::UnsupportedOption(
                FileDialogOption::SuggestedDirectory,
            ));
        }
        Ok(())
    }
}