nsh-core 0.6.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
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
//! Modules: functions a UI set exports to JS without a component
//! (`AlertDialog.show(...)`, `Keyboard.dismiss()`), written in Rust with
//! `#[module]` and generated into the set's JS module like its components.
//! The signature decides how a call runs: a function without a context
//! runs in the JS call and returns its value (as a class's method does);
//! one taking `cx: &mut ModuleCx` is an op, ordered with the props set
//! before it (a `Promise` when it returns a value, `Result<(), E>`
//! included; `undefined` when it returns nothing: nothing answers it); an
//! `async fn` runs as a task of the core (a `Promise`, of `void` too). In
//! every kind the arguments are converted in the JS call, and the op
//! carries a closure holding them (`super::call`).

use std::{future::Future, pin::Pin};

use super::{
    DialogAnswer, TsType, TsTypes,
    call::{JsResult, OpCall},
    class::{AsyncCall, Thrown},
    js::{JsHost, JsRaw},
    task::{self, Action},
};
use crate::{Command, Dialog, LayoutAnimation, ROOT_ID, StatusBarSetting};

/// A JS object of functions (`#[module]` implements it).
pub trait Module {
    /// Its JS name: the type's own (`#[module] impl Keyboard`: `Keyboard`), which
    /// `#[module]` writes; JS calls its functions by it (`Keyboard.dismiss`).
    const NAME: &'static str;
    /// Documentation (the `impl` block's doc comment).
    const DOC: &'static str;
    /// Its functions.
    const FUNCTIONS: &'static [Function];
    /// Its functions are exported one by one
    /// rather than as an object (`#[module(flat)]`).
    const FLAT: bool = false;
    /// Export the same flat module object as `default`.
    const DEFAULT: bool = false;
    /// Associated primitive constants exported with the functions.
    const CONSTANTS: &'static [Constant] = &[];
}

/// An owned primitive constant, shared by native exports and declarations.
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub enum ConstantValue {
    /// No value; the default descriptor's value.
    #[default]
    Undefined,
    /// A static UTF-8 string.
    String(&'static str),
    /// A JavaScript number.
    Number(f64),
    /// A JavaScript boolean.
    Bool(bool),
}

impl ConstantValue {
    /// The TypeScript primitive corresponding to this value.
    pub const fn ts_type(self) -> &'static str {
        match self {
            Self::Undefined => "undefined",
            Self::String(_) => "string",
            Self::Number(_) => "number",
            Self::Bool(_) => "boolean",
        }
    }
}

/// One typed associated constant exported by a module.
#[derive(Debug, Default, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct Constant {
    /// Its JS name (preserved, including uppercase names).
    pub name: &'static str,
    /// Documentation.
    pub doc: &'static str,
    /// Its primitive value and declaration type.
    pub value: ConstantValue,
}

impl Constant {
    /// Builds a constant descriptor, including from generated downstream code.
    pub const fn new(name: &'static str, doc: &'static str, value: ConstantValue) -> Self {
        Self { name, doc, value }
    }
}

/// A module function's parameter: its JS name, TypeScript type, and
/// whether it may be left out.
pub type ModuleParam = (&'static str, TsType, bool);

/// The future of an `async` function or member: its result, or the
/// message the `Promise` rejects with.
pub type ModuleFuture = Pin<Box<dyn Future<Output = JsResult>>>;

/// A function that runs in the JS call: it converts the call's values
/// directly; its result is an owned JS value.
pub type SyncCall = fn(&dyn JsHost) -> Result<JsRaw, Thrown>;

/// How a module function runs.
#[derive(Clone, Copy)]
pub enum FunctionCall {
    /// In the JS call (no context, not `async`): its value, or what it
    /// throws.
    Sync(SyncCall),
    /// As an op, with a [`ModuleCx`], in order with what JS set before it
    /// (its arguments converted in the JS call); an error rejects the
    /// `Promise` (a wrong argument of one returning nothing throws).
    Op(fn(&dyn JsHost) -> Result<OpCall, Thrown>),
    /// As a task started in order with the UI's ops (its arguments
    /// converted in the JS call): it reaches the platform with the same free
    /// functions views use (`dialog`, `clipboard_string`, ...).
    Async(AsyncCall),
    /// In the JS call, returning a task's start ([`super::Later`]): JS gets
    /// a promise of the task, rejected with what the call refuses (a
    /// function returning a promise never throws).
    Start(AsyncCall),
}

impl std::fmt::Debug for FunctionCall {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.kind())
    }
}

impl FunctionCall {
    /// What it is, for the glue and the declarations.
    pub fn kind(&self) -> FunctionKind {
        match self {
            FunctionCall::Sync(_) => FunctionKind::Sync,
            FunctionCall::Op(_) => FunctionKind::Op,
            FunctionCall::Async(_) | FunctionCall::Start(_) => FunctionKind::Async,
        }
    }
}

/// How a module function runs ([`FunctionCall`]), for the glue and the
/// declarations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FunctionKind {
    /// In the JS call: TS gets the value.
    Sync,
    /// An op: TS gets a promise of the value; without one (`()`), nothing
    /// (`void`): the op runs in order with the UI, and no reply comes back.
    Op,
    /// A task: TS gets a promise of the value (`Promise<void>` without
    /// one, settled when the task ends).
    Async,
}

/// A function of a module (`#[module]` makes them).
pub struct Function {
    /// Its JS name (`alert`).
    pub name: &'static str,
    /// Documentation.
    pub doc: &'static str,
    /// Its parameters.
    pub params: &'static [ModuleParam],
    /// The TypeScript type of its result; `None`: nothing (`void`; a
    /// promise of it for a task).
    pub returns: Option<TsType>,
    /// The classes its parameters and result name.
    pub refs: super::ClassRefs,
    /// How it runs.
    pub call: FunctionCall,
    /// A getter of the module's object (`#[getter]`: `process.env`), read
    /// as a property and never a named export of a flat module.
    pub getter: bool,
    /// The signatures TypeScript sees instead of the one above, when the
    /// result's type depends on the arguments (`#[overload(..)]`).
    pub overloads: &'static [Overload],
}

/// One TypeScript signature of a function with several
/// (`#[overload(fn(path: String) -> Vec<u8>)]`): its parameters and the
/// type of its result (a promise of it when the function's is one).
#[derive(Debug, Clone, Copy)]
pub struct Overload {
    /// Its parameters.
    pub params: &'static [ModuleParam],
    /// The TypeScript type of its result.
    pub returns: Option<TsType>,
}

/// A signature of [`FunctionSpec::overloads`]: its parameters (JS name,
/// TypeScript type, optional) and its result's type.
pub type OverloadSpec = (Vec<(&'static str, String, bool)>, Option<String>);

/// One function, for the glue and its declarations.
#[derive(Debug, Clone)]
pub struct FunctionSpec {
    /// Its JS name.
    pub name: &'static str,
    /// Documentation.
    pub doc: &'static str,
    /// Its parameters: JS name, TypeScript type, optional.
    pub params: Vec<(&'static str, String, bool)>,
    /// The TypeScript type of its result.
    pub returns: Option<String>,
    /// How it runs.
    pub kind: FunctionKind,
    /// The call.
    pub call: FunctionCall,
    /// A getter of the module's object ([`Function::getter`]).
    pub getter: bool,
    /// The signatures TypeScript sees instead ([`Function::overloads`]).
    pub overloads: Vec<OverloadSpec>,
}

impl FunctionSpec {
    /// Whether JS gets a `Promise` of the result: a task always, an op
    /// when it returns a value (`Result<(), E>` too, rejected with an
    /// `Err`). An op returning `()` gives `undefined` (`void`): it runs in
    /// order with the UI and nothing answers it, and a wrong argument
    /// throws in the call.
    pub fn promises(&self) -> bool {
        match self.kind {
            FunctionKind::Sync => false,
            FunctionKind::Op => self.returns.is_some(),
            FunctionKind::Async => true,
        }
    }
}

/// What a module is to JS.
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct ModuleSpec {
    /// Its JS name.
    pub name: &'static str,
    /// Documentation.
    pub doc: &'static str,
    /// Its functions.
    pub functions: Vec<FunctionSpec>,
    /// Exported one by one ([`Module::FLAT`]).
    pub flat: bool,
    /// A default export of the same flat module object.
    pub default: bool,
    /// Primitive constants, including their declaration types.
    pub constants: Vec<Constant>,
    /// The named TypeScript types they use.
    pub types: TsTypes,
}

impl ModuleSpec {
    pub(crate) fn of<M: Module>() -> Self {
        let mut types = TsTypes::default();
        let functions = M::FUNCTIONS
            .iter()
            .map(|f| FunctionSpec {
                name: f.name,
                doc: f.doc,
                params: f
                    .params
                    .iter()
                    .map(|(n, ts, optional)| (*n, ts(&mut types), *optional))
                    .collect(),
                returns: f.returns.map(|ts| ts(&mut types)),
                kind: f.call.kind(),
                call: f.call,
                getter: f.getter,
                overloads: f
                    .overloads
                    .iter()
                    .map(|o| {
                        let params = o
                            .params
                            .iter()
                            .map(|(n, ts, optional)| (*n, ts(&mut types), *optional))
                            .collect();
                        (params, o.returns.map(|ts| ts(&mut types)))
                    })
                    .collect(),
            })
            .collect();
        Self {
            name: M::NAME,
            doc: M::DOC,
            functions,
            flat: M::FLAT,
            default: M::DEFAULT,
            constants: M::CONSTANTS.to_vec(),
            types,
        }
    }
}

/// `future`, with a panic in it as its error (a promise JS waits for
/// rejects with the message).
pub(crate) async fn catching(future: ModuleFuture) -> JsResult {
    struct Caught(ModuleFuture);
    impl Future for Caught {
        type Output = JsResult;

        fn poll(
            mut self: Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Self::Output> {
            let future = &mut self.0;
            std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| future.as_mut().poll(cx)))
                .unwrap_or_else(|payload| {
                    std::task::Poll::Ready(Err(super::component::panic_message(payload).into()))
                })
        }
    }
    Caught(future).await
}

/// The context of a module function that is not `async`: the platform's
/// services, applied in order right after the call (an `async` one uses the
/// free functions, `dialog(..)`, ...).
#[derive(Default)]
pub struct ModuleCx {
    pub(crate) actions: Vec<Action>,
}

impl ModuleCx {
    /// Shows `dialog`; await the answer in an `async` function.
    pub fn dialog(&mut self, dialog: Dialog) -> DialogAnswer {
        let (answer, slot) = DialogAnswer::new();
        self.actions.push(Action::Dialog(dialog, slot));
        answer
    }

    /// Draws the views' bars with the built-in bar `source` makes of
    /// `theme`, or with the platform's indicator
    /// ([`set_builtin_scrollbars`](super::set_builtin_scrollbars)).
    pub fn set_builtin_scrollbars(
        &mut self,
        source: super::ScrollbarSource,
        theme: Option<super::AnyValue>,
    ) {
        self.actions.push(Action::BuiltInScrollbars(source, theme));
    }

    /// Runs `f` on the platform's main thread after the call, as
    /// [`run_on_main_thread`](super::run_on_main_thread) does: `spawn` a task
    /// that awaits the result, or drop it.
    pub fn run_on_main_thread<R: Send + 'static>(
        &mut self,
        f: impl FnOnce() -> R + Send + 'static,
    ) -> crate::main_thread::OnMainThread<R> {
        let (task, result) = crate::main_thread::task(f);
        self.actions.push(Action::MainThread(task));
        result
    }

    /// Unfocuses the focused input, hiding the software keyboard.
    pub fn dismiss_keyboard(&mut self) {
        self.actions.push(Action::Command(ROOT_ID, Command::Blur));
    }

    /// Changes what functions set for the status bar (the fields `patch`
    /// sets), under the settings components render.
    pub fn set_status_bar(&mut self, patch: StatusBarSetting) {
        self.actions.push(task::status_bar(patch));
    }

    /// Animates the layout changes of the next commit.
    pub fn animate_layout(&mut self, animation: LayoutAnimation) {
        self.actions.push(Action::Command(
            ROOT_ID,
            Command::LayoutAnimation(animation),
        ));
    }

    /// The screen reader reads `message` (TS:
    /// `announce`).
    pub fn announce_for_accessibility(&mut self, message: impl Into<String>) {
        self.actions.push(super::services::announce(message.into()));
    }

    /// The device plays `haptic` (TS: `Haptics` of `nsh/mobile`).
    pub fn haptic(&mut self, haptic: crate::Haptic) {
        self.actions.push(super::services::haptic(haptic));
    }

    /// Puts `text` on the system clipboard (TS: `Clipboard.writeText`).
    pub fn set_clipboard(&mut self, text: impl Into<String>) {
        self.actions
            .push(super::services::set_clipboard(text.into()));
    }

    /// Puts `item`, its text and its HTML, on the system clipboard (TS:
    /// `Clipboard.write`).
    pub fn set_clipboard_item(&mut self, item: super::ClipboardItem) {
        self.actions.push(super::services::set_clipboard_item(item));
    }

    /// Puts the image of a `data:` URI on the system clipboard (TS:
    /// `Clipboard.writeImage`).
    pub fn set_clipboard_image(&mut self, uri: impl Into<String>) {
        self.actions
            .push(super::services::set_clipboard_image(uri.into()));
    }

    /// Shows the surface in `appearance` whatever the system's (TS:
    /// `setColorScheme`): [`crate::prelude::dark`] (the theme's mode),
    /// `dark:` classes and the platform's controls follow it;
    /// [`crate::platform::Appearance::System`] follows the system again.
    pub fn set_color_scheme(&mut self, appearance: crate::platform::Appearance) {
        self.actions.push(Action::Appearance(appearance));
    }

    /// Shows the app in `locale` whatever the user's language (`None`: the
    /// user's again), as [`crate::i18n::set_locale`] does.
    pub fn set_locale(&mut self, locale: Option<crate::platform::Locale>) {
        self.actions.push(Action::Locale(locale));
    }

    /// Gives the core the app's translations (`nsh-js`: the bundle's).
    #[doc(hidden)]
    pub fn set_translations(&mut self, translations: crate::i18n::Translations) {
        self.actions.push(Action::Translations(translations));
    }

    /// Asks the platform for `action` (the runtime's own modules).
    pub(crate) fn act(&mut self, action: Action) {
        self.actions.push(action);
    }
}

#[cfg(test)]
mod descriptor_tests {
    use super::*;

    #[nsh_macros::module]
    struct Metadata;

    #[nsh_macros::module(flat, default)]
    impl Metadata {
        /// An arbitrary string constant; no name-specific inference.
        pub(crate) const LABEL: &'static str = "native";
        pub(crate) const COUNT: i32 = -7;
        pub(crate) const ENABLED: bool = true;
        #[cfg(any())]
        pub(crate) const OMITTED: bool = false;

        pub(crate) fn version() -> u32 {
            1
        }
    }

    #[test]
    fn descriptors_preserve_constants_default_and_value_derived_types() {
        let module = ModuleSpec::of::<Metadata>();
        assert!(module.flat && module.default);
        assert_eq!(module.functions[0].name, "version");
        assert_eq!(module.constants.len(), 3);
        assert_eq!(module.constants[0].name, "LABEL");
        assert_eq!(module.constants[0].value, ConstantValue::String("native"));
        assert_eq!(module.constants[1].value, ConstantValue::Number(-7.0));
        assert_eq!(module.constants[2].value, ConstantValue::Bool(true));
        assert_eq!(
            module
                .constants
                .iter()
                .map(|c| c.value.ts_type())
                .collect::<Vec<_>>(),
            ["string", "number", "boolean"]
        );
    }

    #[test]
    fn descriptors_have_forward_compatible_defaults() {
        let constant = Constant::default();
        assert_eq!(constant.value, ConstantValue::Undefined);
        assert_eq!(constant.value.ts_type(), "undefined");
        let module = ModuleSpec::default();
        assert!(!module.flat && !module.default);
        assert!(module.functions.is_empty() && module.constants.is_empty());
    }
}