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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
use enum_dispatch::enum_dispatch;
use nsi_sys::*;
use std::{ffi::CString, marker::PhantomData};

// Needed for docs.
#[allow(unused_imports)]
use crate::*;

#[inline]
pub(crate) fn get_c_param_vec(args: &ArgSlice) -> (i32, *const NSIParam_t, Vec<NSIParam_t>) {
    let args = args
        .iter()
        .map(|arg| NSIParam_t {
            name: arg.name.as_ptr(),
            data: arg.data.as_c_ptr(),
            type_: arg.data.type_() as _,
            arraylength: arg.array_length as _,
            count: (arg.data.len() / arg.array_length) as _,
            flags: arg.flags as _,
        })
        .collect::<Vec<_>>();
    (args.len() as _, args.as_ptr(), args)
}

/// A slice of (optional) arguments passed to a method of
/// [`Context`](context::Context).
pub type ArgSlice<'a, 'b> = [Arg<'a, 'b>];

/// A vector of (optional) arguments passed to a method of
/// [`Context`](context::Context).
pub type ArgVec<'a, 'b> = Vec<Arg<'a, 'b>>;

/// An (optional) argument passed to a method of
/// [`Context`](context::Context).
#[derive(Debug)]
pub struct Arg<'a, 'b> {
    pub(crate) name: CString,
    pub(crate) data: ArgData<'a, 'b>,
    // length of each element if an array type
    pub(crate) array_length: usize,
    // number of elements
    pub(crate) flags: u32,
}

impl<'a, 'b> Arg<'a, 'b> {
    #[inline]
    pub fn new(name: &str, data: ArgData<'a, 'b>) -> Self {
        Arg {
            name: CString::new(name).unwrap(),
            data,
            array_length: 1,
            flags: 0,
        }
    }

    /// Sets the length of the argument for each element.
    #[inline]
    pub fn array_len(mut self, length: usize) -> Self {
        self.array_length = length;
        self.flags |= NSIParamIsArray;
        self
    }

    /// Marks this argument as having per-face granularity.
    #[inline]
    pub fn per_face(mut self) -> Self {
        self.flags |= NSIParamPerFace;
        self
    }

    /// Marks this argument as having per-vertex granularity.
    #[inline]
    pub fn per_vertex(mut self) -> Self {
        self.flags |= NSIParamPerVertex;
        self
    }

    /// Marks this argument as to be interpolated linearly.
    #[inline]
    pub fn linear_interpolation(mut self) -> Self {
        self.flags |= NSIParamInterpolateLinear;
        self
    }
}

#[enum_dispatch(ArgData)]
pub(crate) trait ArgDataMethods {
    //const TYPE: Type;
    fn type_(&self) -> Type;
    fn len(&self) -> usize;
    fn as_c_ptr(&self) -> *const core::ffi::c_void;
}

/// A variant describing data passed to the renderer.
///
/// # Lifetimes
/// Lifetime `'a` is for any tuple or array type as these are
/// passed as references and only need to live as long as the
/// function call where they get passed.
///
/// Lifetime `'b` is for the arbitrary reference type. This is
/// pegged to the lifetime of the [`Context`](crate::context::Context).
/// Use this to pass arbitray Rust data through the FFI boundary.
#[enum_dispatch]
#[derive(Debug)]
pub enum ArgData<'a, 'b> {
    /// Single [`f32`) value.
    Float,
    Floats(Floats<'a>),
    /// Single [`f64`] value.
    Double,
    Doubles(Doubles<'a>),
    /// Single [`i32`] value.
    Integer,
    /// An [[`i32`]] array.
    Integers(Integers<'a>),
    /// A [`String`].
    String(String),
    /// A [[`String`]] array.
    Strings(Strings),
    /// Color in linear space, given as a red, green, blue triplet
    /// of [`f32`] values; usually in the range `0..1`.
    Color(Color<'a>),
    /// An arry of colors.
    Colors(Colors<'a>),
    /// Point, given as three [`f32`] values.
    Point(Point<'a>),
    Points(Points<'a>),
    /// Vector, given as three [`f32`] values.
    Vector(Vector<'a>),
    Vectors(Vectors<'a>),
    /// Normal vector, given as three [`f32`] values.
    /// values.
    Normal(Normal<'a>),
    Normasl(Normals<'a>),
    /// Transformation matrix, given as 16 [`f32`] floating
    /// point datas.
    Matrix(Matrix<'a>),
    Matrices(Matrices<'a>),
    /// Transformation matrix, given as 16 [`f64`] floating
    /// point datas.
    DoubleMatrix(DoubleMatrix<'a>),
    DoubleMatrices(DoubleMatrices<'a>),
    /// Reference to arbitrary data.
    Reference(Reference<'b>),
    References(References<'b>),
    /// Callback.
    Callback(Callback<'b>),
    /// Raw (`*const T`) pointer.
    Pointer,
    Pointers(Pointers<'a>),
}

macro_rules! nsi_data_def {
    ($type: ty, $name: ident, $nsi_type: expr) => {
        #[derive(Debug)]
        pub struct $name {
            data: $type,
        }

        impl $name {
            pub fn new(data: $type) -> Self {
                Self { data }
            }
        }

        impl ArgDataMethods for $name {
            fn type_(&self) -> Type {
                $nsi_type
            }

            fn len(&self) -> usize {
                1
            }

            fn as_c_ptr(&self) -> *const core::ffi::c_void {
                &self.data as *const $type as _
            }
        }
    };
}

macro_rules! nsi_data_array_def {
    ($type: ty, $name: ident, $nsi_type: expr) => {
        #[derive(Debug)]
        pub struct $name<'a> {
            data: &'a [$type],
        }

        impl<'a> $name<'a> {
            pub fn new(data: &'a [$type]) -> Self {
                debug_assert!(data.len() % $nsi_type.element_size() == 0);
                Self { data }
            }
        }

        impl<'a> ArgDataMethods for $name<'a> {
            fn type_(&self) -> Type {
                $nsi_type
            }

            fn len(&self) -> usize {
                self.data.len() / $nsi_type.element_size()
            }

            fn as_c_ptr(&self) -> *const core::ffi::c_void {
                self.data.as_ptr() as _
            }
        }
    };
}

macro_rules! nsi_tuple_data_def {
    ($type: tt, $len: expr, $name: ident, $nsi_type: expr) => {
        #[derive(Debug)]
        pub struct $name<'a> {
            data: &'a [$type; $len],
        }

        impl<'a> $name<'a> {
            pub fn new(data: &'a [$type; $len]) -> Self {
                Self { data }
            }
        }

        impl<'a> ArgDataMethods for $name<'a> {
            fn type_(&self) -> Type {
                $nsi_type
            }

            fn len(&self) -> usize {
                1
            }

            fn as_c_ptr(&self) -> *const core::ffi::c_void {
                self.data.as_ptr() as _
            }
        }
    };
}

nsi_data_def!(f32, Float, Type::Float);
nsi_data_def!(f64, Double, Type::Double);
nsi_data_def!(i32, Integer, Type::Integer);

/// Reference type *with* lifetime guaratees.
///
/// Prefer this over using a raw [`Pointer`]
/// as it allows the compiler to check that
/// the data you reference outlives the
/// [`Context`](context::Context) you eventually
/// send it to.
///
/// This gets converted to a raw pointer when passed
/// through the FFI boundary.
/// ```
/// struct Payload {
///     some_data: u32,
/// }
///
/// let ctx = nsi::Context::new(&[]).unwrap();
///
/// // Lots of scene setup omitted ...
///
/// // Setup a custom output driver and send
/// // a payload to it through the FFI boundary
/// ctx.create("driver", nsi::NodeType::OutputDriver, &[]);
/// ctx.connect("driver", "", "beauty", "outputdrivers", &[]);
/// let payload = Payload { some_data: 42 };
/// ctx.set_attribute(
///     "driver",
///     &[
///         nsi::string!("drivername", "custom_driver"),
///         // Payload gets sent as raw pointer through
///         // the FFI boundary.
///         nsi::reference!("payload", Some(&payload)),
///     ],
/// );
///
/// // We need to explicitly call drop here as
/// // ctx's lifetime is pegged to that of payload.
/// drop(ctx);
/// ```
#[derive(Debug)]
pub struct Reference<'a> {
    data: *const core::ffi::c_void,
    _marker: PhantomData<&'a ()>,
}

impl<'a> Reference<'a> {
    pub fn new<T: Sized>(data: Option<&'a T>) -> Self {
        Self {
            data: data
                .map(|p| p as *const _ as _)
                .unwrap_or(core::ptr::null()),
            _marker: PhantomData,
        }
    }
}

impl<'a> ArgDataMethods for Reference<'a> {
    fn type_(&self) -> Type {
        Type::Pointer
    }

    fn len(&self) -> usize {
        1
    }

    fn as_c_ptr(&self) -> *const core::ffi::c_void {
        self.data
    }
}

pub trait CallbackPtr {
    #[doc(hidden)]
    #[allow(clippy::wrong_self_convention)]
    fn to_ptr(self) -> *const core::ffi::c_void;
}

#[derive(Debug)]
pub struct Callback<'a> {
    data: *const core::ffi::c_void,
    _marker: PhantomData<&'a mut ()>,
}

impl<'a> Callback<'a> {
    pub fn new<T: CallbackPtr>(data: T) -> Self {
        Self {
            data: data.to_ptr(),
            _marker: PhantomData,
        }
    }
}

impl<'a> ArgDataMethods for Callback<'a> {
    fn type_(&self) -> Type {
        Type::Pointer
    }

    fn len(&self) -> usize {
        1
    }

    fn as_c_ptr(&self) -> *const core::ffi::c_void {
        self.data
    }
}

/// Raw pointer type *without* lifetime guaratees.
///
/// This can't guarantee that the data this points to
/// outlives the [`Context`](context::Context) you
/// eventually send this to. This is your responsibility.
///
/// If you need to send pointers a better alternative
/// is the [`Reference`] type that allows the compiler
/// to check that the the referenced data outlives the
/// [`Context`](context::Context).
#[derive(Debug)]
pub struct Pointer {
    data: *const core::ffi::c_void,
}

impl Pointer {
    /// # Safety
    /// This is marked unsafe because the responsibility
    /// to ensure the pointer can be safely dereferenced
    /// after the function has returned lies with the user.
    ///
    /// [`Reference`] is a *safe* alternative.
    pub unsafe fn new(data: *const core::ffi::c_void) -> Self {
        Self { data }
    }
}

impl ArgDataMethods for Pointer {
    fn type_(&self) -> Type {
        Type::Pointer
    }

    fn len(&self) -> usize {
        1
    }

    fn as_c_ptr(&self) -> *const core::ffi::c_void {
        self.data
    }
}

#[derive(Debug)]
pub struct String {
    #[allow(dead_code)]
    data: CString,
    // The FFI API needs a pointer to a C string
    pointer: *const core::ffi::c_void,
}

impl String {
    pub fn new<T: Into<Vec<u8>>>(data: T) -> Self {
        let data = CString::new(data).unwrap();
        let pointer = data.as_ptr() as _;

        String { data, pointer }
    }
}

impl ArgDataMethods for String {
    fn type_(&self) -> Type {
        Type::String
    }

    fn len(&self) -> usize {
        1
    }

    fn as_c_ptr(&self) -> *const core::ffi::c_void {
        unsafe { core::mem::transmute(&self.pointer) }
    }
}

nsi_data_array_def!(f32, Floats, Type::Float);
nsi_data_array_def!(f64, Doubles, Type::Double);
nsi_data_array_def!(i32, Integers, Type::Integer);
nsi_data_array_def!(f32, Colors, Type::Color);
nsi_data_array_def!(f32, Points, Type::Point);
nsi_data_array_def!(f32, Vectors, Type::Vector);
nsi_data_array_def!(f32, Normals, Type::Normal);
nsi_data_array_def!(f32, Matrices, Type::Matrix);
nsi_data_array_def!(f64, DoubleMatrices, Type::DoubleMatrix);

/// Reference array type *with* lifetime guarantees.
///
/// Prefer this over using a raw [`Pointers`]
/// as it allows the compiler to check that
/// the data you reference outlives the
/// [`Context`](context::Context) you eventually send
/// it to.
///
/// This gets converted to an array of raw pointers when
/// passed through the FFI boundary.
#[derive(Debug)]
pub struct References<'a> {
    data: Vec<*const core::ffi::c_void>,
    _marker: PhantomData<&'a ()>,
}

impl<'a> References<'a> {
    pub fn new<T>(data: &'a [Option<&'a T>]) -> Self {
        debug_assert!(data.len() % Type::Pointer.element_size() == 0);

        let mut c_data = Vec::<*const core::ffi::c_void>::with_capacity(data.len());

        for e in data {
            c_data.push(
                e.map(|p| p as *const _ as *const core::ffi::c_void)
                    .unwrap_or(core::ptr::null()),
            );
        }

        Self {
            data: c_data,
            _marker: PhantomData,
        }
    }
}

impl<'a> ArgDataMethods for References<'a> {
    fn type_(&self) -> Type {
        Type::Pointer
    }

    fn len(&self) -> usize {
        self.data.len() / Type::Pointer.element_size()
    }

    fn as_c_ptr(&self) -> *const core::ffi::c_void {
        self.data.as_ptr() as _
    }
}

/// Raw pointer array type *without* lifetime guaratees.
///
/// This can't guarantee that the data this points to
/// outlives the [`Context`](context::Context) you
/// eventually send this to. This is your responsibility.
///
/// If you need to send pointers a better alternative
/// is the [`References`] type that allows the compiler
/// to check that the the referenced data outlives the
/// [`Context`](context::Context).
#[derive(Debug)]
pub struct Pointers<'a> {
    data: &'a [*const core::ffi::c_void],
}

impl<'a> Pointers<'a> {
    /// # Safety
    /// This is marked unsafe because the responsibility
    /// to ensure the pointer can be safely dereferenced
    /// after the function has returned lies with the user.
    ///
    /// [`References`] is a *safe* alternative.
    pub unsafe fn new(data: &'a [*const core::ffi::c_void]) -> Self {
        Self { data }
    }
}

impl<'a> ArgDataMethods for Pointers<'a> {
    fn type_(&self) -> Type {
        Type::Pointer
    }

    fn len(&self) -> usize {
        self.data.len() / Type::Pointer.element_size()
    }

    fn as_c_ptr(&self) -> *const core::ffi::c_void {
        self.data.as_ptr() as _
    }
}

#[derive(Debug)]
pub struct Strings {
    #[allow(dead_code)]
    data: Vec<CString>,
    pointer: Vec<*const core::ffi::c_void>,
}

impl Strings {
    pub fn new<T: Into<Vec<u8>> + Copy>(data: &[T]) -> Self {
        let data = data
            .iter()
            .map(|s| CString::new(*s).unwrap())
            .collect::<Vec<_>>();
        let pointer = data.iter().map(|s| s.as_ptr() as _).collect();

        Strings { data, pointer }
    }
}

impl ArgDataMethods for Strings {
    fn type_(&self) -> Type {
        Type::String
    }

    fn len(&self) -> usize {
        self.pointer.len()
    }

    fn as_c_ptr(&self) -> *const core::ffi::c_void {
        self.pointer.as_ptr() as _
    }
}

nsi_tuple_data_def!(f32, 3, Color, Type::Color);
nsi_tuple_data_def!(f32, 3, Point, Type::Point);
nsi_tuple_data_def!(f32, 3, Vector, Type::Vector);
nsi_tuple_data_def!(f32, 3, Normal, Type::Normal);
nsi_tuple_data_def!(f32, 16, Matrix, Type::Matrix);
nsi_tuple_data_def!(f64, 16, DoubleMatrix, Type::DoubleMatrix);

/// Identifies an [`Arg`]’s data type.
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(i32)]
pub(crate) enum Type {
    /// A single 32-bit ([`f32`]) floating point value.
    Float = NSIType_t_NSITypeFloat as _,
    /// A single 64-bit ([`f64`]) floating point value.
    Double = NSIType_t_NSITypeDouble as _,
    /// Single 32-bit ([`i32`]) integer data.
    Integer = NSIType_t_NSITypeInteger as _,
    /// A [`String`].
    String = NSIType_t_NSITypeString as _,
    /// Color, given as three 32-bit ([`i32`]) floating point datas,
    /// usually in the range `0..1`. Red would e.g. be `[1.0, 0.0,
    /// 0.0]`
    Color = NSIType_t_NSITypeColor as _,
    /// Point, given as three 32-bit ([`f32`])floating point datas.
    Point = NSIType_t_NSITypePoint as _,
    /// Vector, given as three 32-bit ([`f32`]) floating point datas.
    Vector = NSIType_t_NSITypeVector as _,
    /// Normal vector, given as three 32-bit ([`f32`]) floating point
    /// datas.
    Normal = NSIType_t_NSITypeNormal as _,
    /// Transformation matrix, given as 16 32-bit ([`f32`]) floating
    /// point datas.
    Matrix = NSIType_t_NSITypeMatrix as _,
    /// Transformation matrix, given as 16 64-bit ([`f64`]) floating
    /// point datas.
    DoubleMatrix = NSIType_t_NSITypeDoubleMatrix as _,
    /// Raw (`*const T`) pointer.
    Pointer = NSIType_t_NSITypePointer as _,
}

impl Type {
    /// Returns the number of components of the resp. type.
    #[inline]
    pub(crate) fn element_size(&self) -> usize {
        match self {
            Type::Float => 1,
            Type::Double => 1,
            Type::Integer => 1,
            Type::String => 1,
            Type::Color => 3,
            Type::Point => 3,
            Type::Vector => 3,
            Type::Normal => 3,
            Type::Matrix => 16,
            Type::DoubleMatrix => 16,
            Type::Pointer => 1,
        }
    }
}

/// Create a [`Float`] argument.
#[macro_export]
macro_rules! float {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Float::new($value)))
    };
}

/// Create a [`Float`] array argument.
#[macro_export]
macro_rules! floats {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Floats::new($value)))
    };
}

/// Create a [`Double`] precision argument.
#[macro_export]
macro_rules! double {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Double::new($value)))
    };
}

/// Create a [`Double`] precision array argument.
#[macro_export]
macro_rules! doubles {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Doubles::new($value)))
    };
}

/// Create a [`Integer`] argument.
#[macro_export]
macro_rules! integer {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Integer::new($value)))
    };
}

/// Create a [`Integer`] array argument.
#[macro_export]
macro_rules! integers {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Integers::new($value)))
    };
}

/// Create a [`Color`] argument.
#[macro_export]
macro_rules! color {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Color::new($value)))
    };
}

/// Create a [`Color`] array argument.
#[macro_export]
macro_rules! colors {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Colors::new($value)))
    };
}

/// Create a [`Point`] argument.
#[macro_export]
macro_rules! point {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Point::new($value)))
    };
}

/// Create a [`Point`] array argument.
#[macro_export]
macro_rules! points {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Points::new($value)))
    };
}

/// Create a [`Vector`] argument.
#[macro_export]
macro_rules! vector {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Vector::new($value)))
    };
}

/// Create a [`Vector`] array argument.
#[macro_export]
macro_rules! vectors {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Vectors::new($value)))
    };
}

/// Create a [`Normal`] argument.
#[macro_export]
macro_rules! normal {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Normal::new($value)))
    };
}

/// Create a [`Normal`] array argument.
#[macro_export]
macro_rules! normals {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Normals::new($value)))
    };
}

/// Create a [`Matrices`] single precision 4×4 matrix argument.
#[macro_export]
macro_rules! matrix {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Matrix::new($value)))
    };
}

/// Create a [`Matrices`] single precision 4×4 matrix array
/// argument.
#[macro_export]
macro_rules! matrices {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Matrices::new($value)))
    };
}

/// Create a [`DoubleMatrix`] double precision 4×4 matrix argument.
/// # Example
/// ```
/// # let ctx = nsi::Context::new(&[]).unwrap();
/// // Setup a transform node.
/// ctx.create("xform", nsi::NodeType::Transform, &[]);
/// ctx.connect("xform", "", ".root", "objects", &[]);
///
/// // Translate 5 units along z-axis.
/// ctx.set_attribute(
///     "xform",
///     &[nsi::double_matrix!(
///         "transformationmatrix",
///         &[1., 0., 0., 0., 0., 1., 0., 0., 0., 0., 1., 0., 0., 0., 5., 1.,]
///     )],
/// );
/// ```
#[macro_export]
macro_rules! double_matrix {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::DoubleMatrix::new($value)))
    };
}

/// Create a [`DoubleMatrices`] double precision 4×4 matrix
/// array argument.
#[macro_export]
macro_rules! double_matrices {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::DoubleMatrices::new($value)))
    };
}

/// Create a [`String`] argument.
/// # Example
/// ```
/// // Create rendering context.
/// let ctx = nsi::Context::new(&[nsi::string!("streamfilename", "stdout")])
///     .expect("Could not create NSI context.");
/// ```
#[macro_export]
macro_rules! string {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::String::new($value)))
    };
}

/// Create a [`String`] array argument.
/// # Example
/// ```
/// # let ctx = nsi::Context::new(&[]).unwrap();
/// // One of these is not an actor:
/// ctx.set_attribute(
///     "dummy",
///     &[nsi::strings!(
///         "actors",
///         &["Klaus Kinski", "Giorgio Moroder", "Rainer Brandt"]
///     )],
/// );
/// ```
#[macro_export]
macro_rules! strings {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Strings::new($value)))
    };
}

/// Create a [`Reference`] argument.
#[macro_export]
macro_rules! reference {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Reference::new($value)))
    };
}

/// Create a [`Reference`] array argument.
#[macro_export]
macro_rules! references {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::References::new($value)))
    };
}

/// Create a [`Callback`] argument.
#[macro_export]
macro_rules! callback {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Callback::new($value)))
    };
}

/// Create a [`Pointer`] array argument.
#[macro_export]
macro_rules! pointer {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Pointer::new($value)))
    };
}

/// Create a [`Pointer`] array argument.
#[macro_export]
macro_rules! pointers {
    ($name: tt, $value: expr) => {
        nsi::Arg::new($name, nsi::ArgData::from(nsi::Pointers::new($value)))
    };
}