excel-api 0.1.0

Safe Rust abstractions for native Microsoft Excel XLL add-ins
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
use core::fmt;

use excel_api_sys::{
    XLL_MODIFIER_CLUSTER_SAFE, XLL_MODIFIER_MACRO_SHEET, XLL_MODIFIER_THREAD_SAFE,
    XLL_MODIFIER_VOLATILE, XLL_TYPE_ASYNC_HANDLE, XLL_TYPE_ASYNC_VOID, XLL_TYPE_BOOL,
    XLL_TYPE_DOUBLE, XLL_TYPE_I32, XLL_TYPE_XCHAR_COUNTED, XLL_TYPE_XCHAR_NULL_TERMINATED,
    XLL_TYPE_XLOPER12_REFERENCE, XLL_TYPE_XLOPER12_VALUE,
};

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// One Excel registration type code for a generated function parameter.
pub enum ExcelArgumentType {
    /// `B`: 64-bit floating-point number.
    Number,
    /// `A`: Boolean.
    Boolean,
    /// `J`: 32-bit integer.
    Integer,
    /// `Q`: value-only `XLOPER12` argument.
    GeneralValue,
    /// `U`: reference-preserving `XLOPER12` argument.
    GeneralReference,
    /// `D%`: counted direct UTF-16 argument.
    CountedUtf16,
    /// `C%`: NUL-terminated direct UTF-16 argument.
    NullTerminatedUtf16,
    /// `X`: internal asynchronous completion handle.
    AsyncHandle,
}

impl ExcelArgumentType {
    const fn code(self) -> &'static str {
        match self {
            Self::Number => XLL_TYPE_DOUBLE,
            Self::Boolean => XLL_TYPE_BOOL,
            Self::Integer => XLL_TYPE_I32,
            Self::GeneralValue => XLL_TYPE_XLOPER12_VALUE,
            Self::GeneralReference => XLL_TYPE_XLOPER12_REFERENCE,
            Self::CountedUtf16 => XLL_TYPE_XCHAR_COUNTED,
            Self::NullTerminatedUtf16 => XLL_TYPE_XCHAR_NULL_TERMINATED,
            Self::AsyncHandle => XLL_TYPE_ASYNC_HANDLE,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Excel registration type code for a generated function result.
pub enum ExcelReturnType {
    /// `B`: 64-bit floating-point number.
    Number,
    /// `A`: Boolean.
    Boolean,
    /// `J`: 32-bit integer.
    Integer,
    /// `Q`: an `XLOPER12` return root.
    Xloper12,
    /// `>`: asynchronous void result completed through `xlAsyncReturn`.
    AsyncVoid,
}

impl ExcelReturnType {
    const fn code(self) -> &'static str {
        match self {
            Self::Number => XLL_TYPE_DOUBLE,
            Self::Boolean => XLL_TYPE_BOOL,
            Self::Integer => XLL_TYPE_I32,
            Self::Xloper12 => XLL_TYPE_XLOPER12_VALUE,
            Self::AsyncVoid => XLL_TYPE_ASYNC_VOID,
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Result and argument type metadata used to construct registration type text.
pub struct FunctionSignature {
    /// Excel-visible result type.
    pub result: ExcelReturnType,
    /// Excel-visible and internal argument types in declaration order.
    pub arguments: &'static [ExcelArgumentType],
}

impl FunctionSignature {
    /// Creates a static function signature.
    pub const fn new(result: ExcelReturnType, arguments: &'static [ExcelArgumentType]) -> Self {
        Self { result, arguments }
    }
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
/// Optional Excel registration modifiers for a worksheet function.
pub struct FunctionFlags {
    /// Adds the volatile modifier.
    pub volatile: bool,
    /// Requests Excel's thread-safe registration modifier.
    pub thread_safe: bool,
    /// Marks the function as macro-sheet equivalent.
    pub macro_type: bool,
    /// Requests the cluster-safe modifier when otherwise legal.
    pub cluster_safe: bool,
}

#[derive(Clone, Copy, Debug)]
/// Complete static registration metadata for one worksheet function.
pub struct FunctionRegistration {
    /// Unmangled Rust-export thunk name.
    pub rust_symbol: &'static str,
    /// Excel-visible worksheet-function name.
    pub excel_name: &'static str,
    /// Result and parameter type codes.
    pub signature: FunctionSignature,
    /// Optional Excel category.
    pub category: Option<&'static str>,
    /// Optional Excel-visible function description.
    pub description: Option<&'static str>,
    /// Excel-visible parameter names excluding the internal async handle.
    pub argument_names: &'static [&'static str],
    /// Excel-visible parameter help, parallel to [`Self::argument_names`].
    pub argument_descriptions: &'static [&'static str],
    /// Registration modifiers.
    pub flags: FunctionFlags,
}

/// Registration metadata for an XLL command, which is deliberately distinct
/// from a worksheet-function descriptor.
#[derive(Clone, Copy, Debug)]
pub struct CommandRegistration {
    /// Unmangled Rust-export command thunk name.
    pub rust_symbol: &'static str,
    /// Excel-visible command name.
    pub excel_name: &'static str,
    /// Optional Excel-visible command description.
    pub description: Option<&'static str>,
    /// Optional command shortcut text.
    pub shortcut: Option<&'static str>,
}

impl CommandRegistration {
    /// Creates command metadata with no optional help or shortcut.
    pub const fn new(rust_symbol: &'static str, excel_name: &'static str) -> Self {
        Self {
            rust_symbol,
            excel_name,
            description: None,
            shortcut: None,
        }
    }

    /// Adds Excel-visible command help text.
    pub const fn description(mut self, description: &'static str) -> Self {
        self.description = Some(description);
        self
    }

    /// Adds the Excel shortcut text.
    pub const fn shortcut(mut self, shortcut: &'static str) -> Self {
        self.shortcut = Some(shortcut);
        self
    }

    /// `I` is the documented 16-bit command return ABI; commands have no
    /// Excel-visible arguments and use macro type 2 during registration.
    pub const fn type_text(&self) -> &'static str {
        "I"
    }

    /// Validates command metadata before lifecycle registration.
    pub fn validate(&self) -> Result<(), RegistrationError> {
        if self.rust_symbol.is_empty() {
            return Err(RegistrationError::EmptyRustSymbol);
        }
        if self.excel_name.is_empty() {
            return Err(RegistrationError::EmptyExcelName);
        }
        Ok(())
    }
}

impl FunctionRegistration {
    /// Returns whether this descriptor has the verified async `>`/`X` shape.
    pub const fn is_asynchronous(&self) -> bool {
        matches!(self.signature.result, ExcelReturnType::AsyncVoid)
    }

    /// Creates function metadata with no optional category, help, or modifiers.
    pub const fn new(
        rust_symbol: &'static str,
        excel_name: &'static str,
        signature: FunctionSignature,
    ) -> Self {
        Self {
            rust_symbol,
            excel_name,
            signature,
            category: None,
            description: None,
            argument_names: &[],
            argument_descriptions: &[],
            flags: FunctionFlags {
                volatile: false,
                thread_safe: false,
                macro_type: false,
                cluster_safe: false,
            },
        }
    }

    /// Adds the Excel category.
    pub const fn category(mut self, category: &'static str) -> Self {
        self.category = Some(category);
        self
    }

    /// Adds Excel-visible function help text.
    pub const fn description(mut self, description: &'static str) -> Self {
        self.description = Some(description);
        self
    }

    /// Adds parameter names and matching Excel-visible help text.
    pub const fn arguments(
        mut self,
        names: &'static [&'static str],
        descriptions: &'static [&'static str],
    ) -> Self {
        self.argument_names = names;
        self.argument_descriptions = descriptions;
        self
    }

    /// Sets registration modifiers after validating them with [`Self::validate`].
    pub const fn flags(mut self, flags: FunctionFlags) -> Self {
        self.flags = flags;
        self
    }

    /// Builds canonical Excel registration type text after validation.
    pub fn type_text(&self) -> Result<String, RegistrationError> {
        self.validate()?;
        let mut text = String::from(self.signature.result.code());
        for argument in self.signature.arguments {
            text.push_str(argument.code());
        }
        // Microsoft specifies modifiers after the parameter codes. This
        // canonical order makes descriptors deterministic.
        if self.flags.volatile {
            text.push(XLL_MODIFIER_VOLATILE);
        }
        if self.flags.macro_type {
            text.push(XLL_MODIFIER_MACRO_SHEET);
        }
        if self.flags.thread_safe {
            text.push(XLL_MODIFIER_THREAD_SAFE);
        }
        if self.flags.cluster_safe {
            text.push(XLL_MODIFIER_CLUSTER_SAFE);
        }
        Ok(text)
    }

    /// Validates names, argument metadata, modifiers, and async shape.
    pub fn validate(&self) -> Result<(), RegistrationError> {
        if self.rust_symbol.is_empty() {
            return Err(RegistrationError::EmptyRustSymbol);
        }
        if self.excel_name.is_empty() {
            return Err(RegistrationError::EmptyExcelName);
        }
        let async_handles = self
            .signature
            .arguments
            .iter()
            .filter(|argument| **argument == ExcelArgumentType::AsyncHandle)
            .count();
        let visible_arguments = self.signature.arguments.len() - async_handles;
        if visible_arguments != self.argument_names.len() {
            return Err(RegistrationError::SignatureArgumentLengthMismatch);
        }
        if self.argument_names.len() != self.argument_descriptions.len() {
            return Err(RegistrationError::ArgumentMetadataLengthMismatch);
        }
        if self.flags.macro_type && (self.flags.thread_safe || self.flags.cluster_safe) {
            return Err(RegistrationError::IncompatibleFlags);
        }
        let asynchronous = self.signature.result == ExcelReturnType::AsyncVoid;
        if asynchronous != (async_handles == 1) || (asynchronous && self.flags.cluster_safe) {
            return Err(RegistrationError::InvalidAsyncSignature);
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug)]
/// Static metadata for one XLL and its generated registrations.
pub struct AddInDescriptor {
    /// Excel-visible add-in name.
    pub name: &'static str,
    /// Excel-visible add-in description.
    pub description: &'static str,
    /// Worksheet function registrations.
    pub functions: &'static [FunctionRegistration],
    /// Command registrations.
    pub commands: &'static [CommandRegistration],
}

impl AddInDescriptor {
    /// Creates add-in metadata without commands.
    pub const fn new(
        name: &'static str,
        description: &'static str,
        functions: &'static [FunctionRegistration],
    ) -> Self {
        Self {
            name,
            description,
            functions,
            commands: &[],
        }
    }

    /// Adds static command metadata to this descriptor.
    pub const fn commands(mut self, commands: &'static [CommandRegistration]) -> Self {
        self.commands = commands;
        self
    }

    /// Validates the add-in and every contained registration descriptor.
    pub fn validate(&self) -> Result<(), RegistrationError> {
        if self.name.is_empty() {
            return Err(RegistrationError::EmptyAddInName);
        }
        for function in self.functions {
            function.validate()?;
        }
        for command in self.commands {
            command.validate()?;
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
/// Static registration metadata is inconsistent or exceeds Excel limits.
pub enum RegistrationError {
    /// The add-in name was empty.
    EmptyAddInName,
    /// The exported Rust thunk symbol was empty.
    EmptyRustSymbol,
    /// The Excel-visible name was empty.
    EmptyExcelName,
    /// Visible signature parameters and supplied names have different lengths.
    SignatureArgumentLengthMismatch,
    /// Parameter names and descriptions have different lengths.
    ArgumentMetadataLengthMismatch,
    /// Macro-sheet registration conflicts with thread-safe or cluster-safe flags.
    IncompatibleFlags,
    /// The `>` result and single `X` handle contract was not satisfied.
    InvalidAsyncSignature,
    /// Generated registration text exceeds Excel's counted-string limit.
    StringTooLong,
}

impl fmt::Display for RegistrationError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::EmptyAddInName => "add-in name must not be empty",
            Self::EmptyRustSymbol => "Rust symbol must not be empty",
            Self::EmptyExcelName => "Excel function name must not be empty",
            Self::SignatureArgumentLengthMismatch => {
                "signature and argument metadata lengths differ"
            }
            Self::ArgumentMetadataLengthMismatch => {
                "argument names and descriptions must have the same length"
            }
            Self::IncompatibleFlags => {
                "a function cannot be both thread-safe and macro-sheet equivalent"
            }
            Self::InvalidAsyncSignature => {
                "an asynchronous function requires `>` plus exactly one `X` argument and cannot be cluster-safe"
            }
            Self::StringTooLong => "registration text exceeds Excel's counted-string limit",
        })
    }
}

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

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

    const TWO_NUMBERS: &[ExcelArgumentType] =
        &[ExcelArgumentType::Number, ExcelArgumentType::Number];

    #[test]
    fn exact_type_text_is_generated_from_the_signature() {
        let function = FunctionRegistration::new(
            "rust_add",
            "RUST.ADD",
            FunctionSignature::new(ExcelReturnType::Xloper12, TWO_NUMBERS),
        )
        .arguments(&["x", "y"], &["x", "y"])
        .flags(FunctionFlags {
            volatile: true,
            thread_safe: true,
            macro_type: false,
            cluster_safe: true,
        });
        assert_eq!(function.type_text().as_deref(), Ok("QBB!$&"));
    }

    #[test]
    fn q_and_u_and_direct_wide_strings_remain_distinct() {
        const ARGS: &[ExcelArgumentType] = &[
            ExcelArgumentType::GeneralValue,
            ExcelArgumentType::GeneralReference,
            ExcelArgumentType::CountedUtf16,
            ExcelArgumentType::NullTerminatedUtf16,
        ];
        let function = FunctionRegistration::new(
            "probe",
            "PROBE",
            FunctionSignature::new(ExcelReturnType::Xloper12, ARGS),
        )
        .arguments(&["q", "u", "d", "c"], &["", "", "", ""]);
        assert_eq!(function.type_text().as_deref(), Ok("QQUD%C%"));
    }

    #[test]
    fn incompatible_flags_and_mismatched_metadata_are_rejected() {
        let mismatch = FunctionRegistration::new(
            "add",
            "RUST.ADD",
            FunctionSignature::new(ExcelReturnType::Number, TWO_NUMBERS),
        )
        .arguments(&["x"], &["x"]);
        assert_eq!(
            mismatch.validate(),
            Err(RegistrationError::SignatureArgumentLengthMismatch)
        );

        let incompatible = FunctionRegistration::new(
            "f",
            "F",
            FunctionSignature::new(ExcelReturnType::Number, &[]),
        )
        .flags(FunctionFlags {
            volatile: false,
            thread_safe: true,
            macro_type: true,
            cluster_safe: false,
        });
        assert_eq!(
            incompatible.validate(),
            Err(RegistrationError::IncompatibleFlags)
        );
    }

    #[test]
    fn async_registration_requires_exact_void_handle_pair() {
        let valid = FunctionRegistration::new(
            "async_probe",
            "ASYNC.PROBE",
            FunctionSignature::new(
                ExcelReturnType::AsyncVoid,
                &[ExcelArgumentType::Number, ExcelArgumentType::AsyncHandle],
            ),
        )
        .arguments(&["value"], &["value"])
        .flags(FunctionFlags {
            thread_safe: true,
            ..FunctionFlags::default()
        });
        assert_eq!(valid.type_text().as_deref(), Ok(">BX$"));

        let missing = FunctionRegistration::new(
            "bad",
            "BAD",
            FunctionSignature::new(ExcelReturnType::AsyncVoid, &[ExcelArgumentType::Number]),
        )
        .arguments(&["value"], &["value"]);
        assert_eq!(
            missing.validate(),
            Err(RegistrationError::InvalidAsyncSignature)
        );

        let stray = FunctionRegistration::new(
            "bad",
            "BAD",
            FunctionSignature::new(ExcelReturnType::Number, &[ExcelArgumentType::AsyncHandle]),
        );
        assert_eq!(
            stray.validate(),
            Err(RegistrationError::InvalidAsyncSignature)
        );
    }
}