avmnif-rs 0.4.1

Safe NIF toolkit for AtomVM written in Rust
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
//! Tagged Map serialization for creating Erlang-compatible ADTs
//!
//! This module provides automatic serialization of Rust types into
//! Erlang maps with type discriminators, enabling type-safe communication
//! between Rust ports/NIFs and Erlang processes.
//!
//! # Design Philosophy
//!
//! All operations are generic and work with any AtomTableOps implementation.
//! No global state, no hardcoded dependencies - pure dependency injection.
//!
//! # Examples
//!
//! ```rust,ignore
//! use avmnif_rs::tagged::{TaggedMap, TaggedError};
//! use avmnif_rs::testing::mocks::MockAtomTable;
//!
//! #[derive(TaggedMap)]
//! struct SensorReading {
//!     temperature: f32,
//!     humidity: f32,
//!     timestamp: u64,
//! }
//!
//! // In tests:
//! let table = MockAtomTable::new();
//! let reading = SensorReading { temperature: 23.5, humidity: 45.2, timestamp: 1634567890 };
//! let term = reading.to_tagged_map(&table)?;
//! let parsed = SensorReading::from_tagged_map(term, &table)?;
//!
//! // In production:
//! let table = AtomTable::from_global();
//! let term = reading.to_tagged_map(&table)?;
//! ```

extern crate alloc;

use crate::atom::{AtomTableOps, AtomError, atoms};
use crate::term::{AtomIndex, TermValue};
use alloc::{string::String, string::ToString, vec, vec::Vec, format};
use core::fmt;

// ── Error Handling ──────────────────────────────────────────────────────────

/// Errors that can occur during tagged map operations
#[derive(Debug, Clone, PartialEq)]
pub enum TaggedError {
    /// Atom-related error (atom creation, lookup, etc.)
    AtomError(AtomError),
    /// Wrong type for operation
    WrongType { expected: &'static str, found: &'static str },
    /// Index/key out of bounds  
    OutOfBounds { index: usize, max: usize },
    /// Required field missing from map
    MissingField(String),
    /// Type discriminator doesn't match expected type
    TypeMismatch { expected: String, found: String },
    /// Invalid enum variant
    InvalidVariant { enum_name: String, variant: String },
    /// Memory allocation failed
    OutOfMemory,
    /// Invalid UTF-8 in binary
    InvalidUtf8,
    /// Nested error with path context
    NestedError { path: String, source: alloc::boxed::Box<TaggedError> },
    /// Generic error with message
    Other(String),
}

impl TaggedError {
    /// Create a nested error with path context
    pub fn nested(path: impl Into<String>, source: TaggedError) -> Self {
        TaggedError::NestedError {
            path: path.into(),
            source: alloc::boxed::Box::new(source),
        }
    }
    
    /// Create a type mismatch error
    pub fn type_mismatch(expected: impl Into<String>, found: impl Into<String>) -> Self {
        TaggedError::TypeMismatch {
            expected: expected.into(),
            found: found.into(),
        }
    }
    
    /// Create a missing field error
    pub fn missing_field(field: impl Into<String>) -> Self {
        TaggedError::MissingField(field.into())
    }
    
    /// Create an invalid variant error
    pub fn invalid_variant(enum_name: impl Into<String>, variant: impl Into<String>) -> Self {
        TaggedError::InvalidVariant {
            enum_name: enum_name.into(),
            variant: variant.into(),
        }
    }
}

impl fmt::Display for TaggedError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TaggedError::AtomError(e) => write!(f, "atom error: {}", e),
            TaggedError::WrongType { expected, found } => 
                write!(f, "wrong type: expected {}, found {}", expected, found),
            TaggedError::OutOfBounds { index, max } => 
                write!(f, "index {} out of bounds (max: {})", index, max),
            TaggedError::MissingField(field) => 
                write!(f, "missing required field: {}", field),
            TaggedError::TypeMismatch { expected, found } => 
                write!(f, "type mismatch: expected {}, found {}", expected, found),
            TaggedError::InvalidVariant { enum_name, variant } => 
                write!(f, "invalid variant '{}' for enum {}", variant, enum_name),
            TaggedError::OutOfMemory => write!(f, "out of memory"),
            TaggedError::InvalidUtf8 => write!(f, "invalid UTF-8"),
            TaggedError::NestedError { path, source } => 
                write!(f, "error at {}: {}", path, source),
            TaggedError::Other(msg) => write!(f, "{}", msg),
        }
    }
}

impl From<AtomError> for TaggedError {
    fn from(error: AtomError) -> Self {
        TaggedError::AtomError(error)
    }
}

/// Result type for tagged map operations
pub type TaggedResult<T> = core::result::Result<T, TaggedError>;

// ── Core Trait ──────────────────────────────────────────────────────────────

/// Trait for types that can be converted to/from tagged Erlang maps
/// 
/// All operations are generic and work with any AtomTableOps implementation.
pub trait TaggedMap: Sized {
    /// Convert this type to a tagged Erlang map using any atom table
    /// 
    /// The resulting map will have a `type` field with the type discriminator
    /// and additional fields for the struct/enum data.
    fn to_tagged_map<T: AtomTableOps>(&self, table: &T) -> TaggedResult<TermValue>;
    
    /// Create this type from a tagged Erlang map using any atom table
    /// 
    /// Validates the `type` field matches the expected type and extracts
    /// the remaining fields to reconstruct the Rust type.
    fn from_tagged_map<T: AtomTableOps>(map: TermValue, table: &T) -> TaggedResult<Self>;
    
    /// Get the type atom name for this type (used for discriminator)
    fn type_name() -> &'static str;
}

// ── Helper Functions ────────────────────────────────────────────────────────

/// Convert Rust identifier to snake_case atom name
/// 
/// Examples:
/// - `SensorReading` -> `"sensor_reading"`
/// - `HTTPClient` -> `"httpclient"`
/// - `XMLParser` -> `"xmlparser"`
pub fn to_snake_case(name: &str) -> String {
    let mut result = String::new();
    let chars: Vec<char> = name.chars().collect();
    
    for (i, &ch) in chars.iter().enumerate() {
        if ch.is_uppercase() {
            // Check if we should add an underscore
            let should_add_underscore = if i == 0 {
                false // Never add underscore at start
            } else {
                let prev_char = chars[i - 1];
                // Add underscore if previous char was lowercase (camelCase boundary)
                prev_char.is_lowercase()
            };
            
            if should_add_underscore {
                result.push('_');
            }
            
            result.push(ch.to_lowercase().next().unwrap());
        } else {
            result.push(ch);
        }
    }
    
    result
}

/// Get atom index for a type name, creating it if necessary
pub fn get_type_atom<T: AtomTableOps>(type_name: &str, table: &T) -> TaggedResult<AtomIndex> {
    let atom_index = table.ensure_atom_str(type_name).map_err(TaggedError::from)?;
    Ok(atom_index)
}

/// Get the standard "type" field atom
pub fn type_field_atom<T: AtomTableOps>(table: &T) -> TaggedResult<AtomIndex> {
    let atom_index = table.ensure_atom_str("type").map_err(TaggedError::from)?;
    Ok(atom_index)
}

/// Get the standard "variant" field atom (for enums)
pub fn variant_field_atom<T: AtomTableOps>(table: &T) -> TaggedResult<AtomIndex> {
    let atom_index = table.ensure_atom_str("variant").map_err(TaggedError::from)?;
    Ok(atom_index)
}

/// Extract map value by atom key
pub fn get_map_value(map: &TermValue, key_atom: AtomIndex) -> TaggedResult<&TermValue> {
    match map {
        TermValue::Map(pairs) => {
            let key = TermValue::Atom(key_atom);
            pairs.iter()
                .find(|(k, _)| k == &key)
                .map(|(_, v)| v)
                .ok_or_else(|| TaggedError::Other(format!("key not found in map")))
        }
        _ => Err(TaggedError::WrongType { expected: "map", found: "other" }),
    }
}

/// Extract required string field from map
pub fn extract_string_field<T: AtomTableOps>(map: &TermValue, field_name: &str, table: &T) -> TaggedResult<String> {
    let field_atom = get_type_atom(field_name, table)?;
    let value = get_map_value(map, field_atom)?;
    
    match value {
        TermValue::Binary(bytes) => {
            String::from_utf8(bytes.clone()).map_err(|_| TaggedError::InvalidUtf8)
        }
        _ => Err(TaggedError::WrongType { expected: "binary/string", found: "other" }),
    }
}

/// Extract required integer field from map
pub fn extract_int_field<T: AtomTableOps>(map: &TermValue, field_name: &str, table: &T) -> TaggedResult<i32> {
    let field_atom = get_type_atom(field_name, table)?;
    let value = get_map_value(map, field_atom)?;
    
    match value {
        TermValue::SmallInt(i) => Ok(*i),
        _ => Err(TaggedError::WrongType { expected: "integer", found: "other" }),
    }
}

/// Extract required float field from map  
pub fn extract_float_field<T: AtomTableOps>(map: &TermValue, field_name: &str, table: &T) -> TaggedResult<f64> {
    let field_atom = get_type_atom(field_name, table)?;
    let value = get_map_value(map, field_atom)?;
    
    match value {
        TermValue::Float(f) => Ok(*f),
        TermValue::SmallInt(i) => Ok(*i as f64), // Allow integer to float conversion
        _ => Err(TaggedError::WrongType { expected: "float", found: "other" }),
    }
}

/// Extract required boolean field from map
pub fn extract_bool_field<T: AtomTableOps>(map: &TermValue, field_name: &str, table: &T) -> TaggedResult<bool> {
    let field_atom = get_type_atom(field_name, table)?;
    let value = get_map_value(map, field_atom)?;
    
    let true_atom = atoms::true_atom(table).map_err(TaggedError::from)?;
    let false_atom = atoms::false_atom(table).map_err(TaggedError::from)?;
    
    match value {
        TermValue::Atom(atom_idx) => {
            if *atom_idx == true_atom {
                Ok(true)
            } else if *atom_idx == false_atom {
                Ok(false)
            } else {
                Err(TaggedError::WrongType { expected: "boolean", found: "other atom" })
            }
        }
        _ => Err(TaggedError::WrongType { expected: "boolean", found: "other" }),
    }
}

/// Extract optional field from map
pub fn extract_optional_field<R, F, A>(
    map: &TermValue, 
    field_name: &str, 
    table: &A,
    extractor: F
) -> TaggedResult<Option<R>>
where
    F: FnOnce(&TermValue, &A) -> TaggedResult<R>,
    A: AtomTableOps,
{
    let field_atom = get_type_atom(field_name, table)?;
    
    match get_map_value(map, field_atom) {
        Ok(value) => {
            let nil_atom = atoms::nil(table).map_err(TaggedError::from)?;
            match value {
                TermValue::Atom(atom_idx) if *atom_idx == nil_atom => Ok(None),
                _ => extractor(value, table).map(Some),
            }
        }
        Err(_) => Ok(None), // Field not present
    }
}

/// Validate map has expected type discriminator
pub fn validate_type_discriminator<T: AtomTableOps>(map: &TermValue, expected_type: &str, table: &T) -> TaggedResult<()> {
    let type_atom = type_field_atom(table)?;
    let expected_type_atom = get_type_atom(expected_type, table)?;
    
    let type_value = get_map_value(map, type_atom)?;
    
    match type_value {
        TermValue::Atom(actual_type_atom) => {
            if *actual_type_atom == expected_type_atom {
                Ok(())
            } else {
                // Try to get readable atom name for error
                let actual_name = match table.get_atom_string(*actual_type_atom) {
                    Ok(atom_ref) => atom_ref.as_str().unwrap_or("unknown").to_string(),
                    Err(_) => "unknown".to_string(),
                };
                Err(TaggedError::type_mismatch(expected_type, actual_name))
            }
        }
        _ => Err(TaggedError::WrongType { expected: "atom", found: "other" }),
    }
}

// ── Generic Primitive Type Implementations ─────────────────────────────────

// These allow primitive types to be used directly in tagged structs

impl TaggedMap for i32 {
    fn to_tagged_map<T: AtomTableOps>(&self, table: &T) -> TaggedResult<TermValue> {
        let type_atom = get_type_atom("i32", table)?;
        let value_atom = get_type_atom("value", table)?;
        
        let pairs = alloc::vec![
            (TermValue::Atom(type_field_atom(table)?), TermValue::Atom(type_atom)),
            (TermValue::Atom(value_atom), TermValue::SmallInt(*self)),
        ];
        
        Ok(TermValue::Map(pairs))
    }
    
    fn from_tagged_map<T: AtomTableOps>(map: TermValue, table: &T) -> TaggedResult<Self> {
        validate_type_discriminator(&map, "i32", table)?;
        extract_int_field(&map, "value", table)
    }
    
    fn type_name() -> &'static str {
        "i32"
    }
}

impl TaggedMap for String {
    fn to_tagged_map<T: AtomTableOps>(&self, table: &T) -> TaggedResult<TermValue> {
        let type_atom = get_type_atom("string", table)?;
        let value_atom = get_type_atom("value", table)?;
        
        let pairs = alloc::vec![
            (TermValue::Atom(type_field_atom(table)?), TermValue::Atom(type_atom)),
            (TermValue::Atom(value_atom), TermValue::Binary(self.as_bytes().to_vec())),
        ];
        
        Ok(TermValue::Map(pairs))
    }
    
    fn from_tagged_map<T: AtomTableOps>(map: TermValue, table: &T) -> TaggedResult<Self> {
        validate_type_discriminator(&map, "string", table)?;
        extract_string_field(&map, "value", table)
    }
    
    fn type_name() -> &'static str {
        "string"
    }
}

impl TaggedMap for bool {
    fn to_tagged_map<T: AtomTableOps>(&self, table: &T) -> TaggedResult<TermValue> {
        let type_atom = get_type_atom("bool", table)?;
        let value_atom = get_type_atom("value", table)?;
        let bool_atom = if *self { 
            atoms::true_atom(table).map_err(TaggedError::from)? 
        } else { 
            atoms::false_atom(table).map_err(TaggedError::from)? 
        };
        
        let pairs = alloc::vec![
            (TermValue::Atom(type_field_atom(table)?), TermValue::Atom(type_atom)),
            (TermValue::Atom(value_atom), TermValue::Atom(bool_atom)),
        ];
        
        Ok(TermValue::Map(pairs))
    }
    
    fn from_tagged_map<T: AtomTableOps>(map: TermValue, table: &T) -> TaggedResult<Self> {
        validate_type_discriminator(&map, "bool", table)?;
        extract_bool_field(&map, "value", table)
    }
    
    fn type_name() -> &'static str {
        "bool"
    }
}

impl<U: TaggedMap> TaggedMap for Option<U> {
    fn to_tagged_map<T: AtomTableOps>(&self, table: &T) -> TaggedResult<TermValue> {
        match self {
            Some(value) => {
                let inner_map = value.to_tagged_map(table)?;
                let type_atom = get_type_atom("option", table)?;
                let variant_atom = variant_field_atom(table)?;
                let some_atom = get_type_atom("some", table)?;
                let value_atom = get_type_atom("value", table)?;
                
                let pairs = alloc::vec![
                    (TermValue::Atom(type_field_atom(table)?), TermValue::Atom(type_atom)),
                    (TermValue::Atom(variant_atom), TermValue::Atom(some_atom)),
                    (TermValue::Atom(value_atom), inner_map),
                ];
                
                Ok(TermValue::Map(pairs))
            }
            None => {
                let type_atom = get_type_atom("option", table)?;
                let variant_atom = variant_field_atom(table)?;
                let none_atom = atoms::nil(table).map_err(TaggedError::from)?;
                
                let pairs = alloc::vec![
                    (TermValue::Atom(type_field_atom(table)?), TermValue::Atom(type_atom)),
                    (TermValue::Atom(variant_atom), TermValue::Atom(none_atom)),
                ];
                
                Ok(TermValue::Map(pairs))
            }
        }
    }
    
    fn from_tagged_map<T: AtomTableOps>(map: TermValue, table: &T) -> TaggedResult<Self> {
        validate_type_discriminator(&map, "option", table)?;
        
        let variant_atom = variant_field_atom(table)?;
        let variant_value = get_map_value(&map, variant_atom)?;
        
        let some_atom = get_type_atom("some", table)?;
        let none_atom = atoms::nil(table).map_err(TaggedError::from)?;
        
        match variant_value {
            TermValue::Atom(atom_idx) if *atom_idx == some_atom => {
                let value_atom = get_type_atom("value", table)?;
                let inner_map = get_map_value(&map, value_atom)?;
                let inner_value = U::from_tagged_map(inner_map.clone(), table)?;
                Ok(Some(inner_value))
            }
            TermValue::Atom(atom_idx) if *atom_idx == none_atom => {
                Ok(None)
            }
            _ => Err(TaggedError::invalid_variant("Option", "unknown")),
        }
    }
    
    fn type_name() -> &'static str {
        "option"
    }
}

impl<U: TaggedMap> TaggedMap for Vec<U> {
    fn to_tagged_map<T: AtomTableOps>(&self, table: &T) -> TaggedResult<TermValue> {
        let type_atom = get_type_atom("vec", table)?;
        let elements_atom = get_type_atom("elements", table)?;
        
        // Convert each element to tagged map
        let mut element_maps = Vec::new();
        for item in self {
            element_maps.push(item.to_tagged_map(table)?);
        }
        
        let elements_list = TermValue::from_vec(element_maps);
        
        let pairs = alloc::vec![
            (TermValue::Atom(type_field_atom(table)?), TermValue::Atom(type_atom)),
            (TermValue::Atom(elements_atom), elements_list),
        ];
        
        Ok(TermValue::Map(pairs))
    }
    
    fn from_tagged_map<T: AtomTableOps>(map: TermValue, table: &T) -> TaggedResult<Self> {
        validate_type_discriminator(&map, "vec", table)?;
        
        let elements_atom = get_type_atom("elements", table)?;
        let elements_value = get_map_value(&map, elements_atom)?;
        
        let elements_vec = elements_value.list_to_vec();
        let mut result = Vec::new();
        
        for element_map in elements_vec {
            let item = U::from_tagged_map(element_map, table)?;
            result.push(item);
        }
        
        Ok(result)
    }
    
    fn type_name() -> &'static str {
        "vec"
    }
}

// ── Re-exports ──────────────────────────────────────────────────────────────

// Re-export the derive macro when available
#[cfg(feature = "derive")]
pub use avmnif_derive::TaggedMap;