hiroz 0.2.0

Native Rust ROS 2 implementation using Zenoh
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
//! Conversion between MessageSchema and ROS 2 TypeDescription.
//!
//! This module provides bidirectional conversion between the runtime
//! `MessageSchema` type used for dynamic messages and the wire-format
//! `TypeDescription` types defined in `hiroz-schema`.
//!
//! # Usage
//!
//! ```rust,ignore
//! use hiroz::dynamic::{MessageSchema, FieldType};
//! use hiroz_schema::{TypeDescriptionMsg, calculate_hash};
//!
//! // Create a schema programmatically
//! let schema = MessageSchema::builder("geometry_msgs/msg/Point")
//!     .field("x", FieldType::Float64)
//!     .field("y", FieldType::Float64)
//!     .field("z", FieldType::Float64)
//!     .build()?;
//!
//! // Convert to TypeDescription for wire format or hashing
//! let type_desc_msg = schema.to_type_description_msg()?;
//!
//! // Calculate type hash
//! let hash = calculate_hash(&type_desc_msg);
//! println!("Type hash: {}", hash.to_rihs_string());
//! ```

use std::collections::HashMap;
use std::sync::Arc;

use hiroz_schema::{
    FieldDescription, FieldTypeDescription, TypeDescription, TypeDescriptionMsg, TypeHash, TypeId,
    calculate_hash,
};

use super::error::DynamicError;
use super::schema::{FieldSchema, FieldType, MessageSchema};

/// Extension trait for MessageSchema to support TypeDescription conversion.
pub trait MessageSchemaTypeDescription {
    /// Convert this schema to a TypeDescriptionMsg (includes referenced types).
    fn to_type_description_msg(&self) -> Result<TypeDescriptionMsg, DynamicError>;

    /// Convert this schema to a single TypeDescription (without referenced types).
    fn to_type_description(&self) -> Result<TypeDescription, DynamicError>;

    /// Calculate the RIHS01 type hash for this schema.
    fn compute_type_hash(&self) -> Result<TypeHash, DynamicError>;
}

impl MessageSchemaTypeDescription for MessageSchema {
    fn to_type_description_msg(&self) -> Result<TypeDescriptionMsg, DynamicError> {
        let mut referenced = Vec::new();
        let mut visited = HashMap::new();

        // First, collect all referenced type descriptions
        collect_referenced_types(self, &mut referenced, &mut visited)?;

        // Sort referenced types alphabetically by type name (required for hash consistency)
        referenced.sort_by(|a, b| a.type_name.cmp(&b.type_name));

        Ok(TypeDescriptionMsg {
            type_description: self.to_type_description()?,
            referenced_type_descriptions: referenced,
        })
    }

    fn to_type_description(&self) -> Result<TypeDescription, DynamicError> {
        let fields = self
            .fields
            .iter()
            .map(field_schema_to_description)
            .collect::<Result<Vec<_>, _>>()?;

        Ok(TypeDescription {
            type_name: self.type_name.clone(),
            fields,
        })
    }

    fn compute_type_hash(&self) -> Result<TypeHash, DynamicError> {
        let msg = self.to_type_description_msg()?;
        Ok(calculate_hash(&msg))
    }
}

/// Collect all referenced (nested) type descriptions recursively.
fn collect_referenced_types(
    schema: &MessageSchema,
    referenced: &mut Vec<TypeDescription>,
    visited: &mut HashMap<String, bool>,
) -> Result<(), DynamicError> {
    for field in &schema.fields {
        collect_field_type_references(&field.field_type, referenced, visited)?;
    }
    Ok(())
}

/// Recursively collect references from a field type.
fn collect_field_type_references(
    field_type: &FieldType,
    referenced: &mut Vec<TypeDescription>,
    visited: &mut HashMap<String, bool>,
) -> Result<(), DynamicError> {
    match field_type {
        FieldType::Message(nested_schema) if !visited.contains_key(&nested_schema.type_name) => {
            visited.insert(nested_schema.type_name.clone(), true);

            // First collect this type's nested references
            collect_referenced_types(nested_schema, referenced, visited)?;

            // Then add this type's description
            let td = MessageSchemaTypeDescription::to_type_description(nested_schema.as_ref())?;
            referenced.push(td);
        }
        FieldType::Message(_) => {}
        FieldType::Array(inner, _)
        | FieldType::Sequence(inner)
        | FieldType::BoundedSequence(inner, _) => {
            collect_field_type_references(inner, referenced, visited)?;
        }
        _ => {} // Primitives have no references
    }
    Ok(())
}

/// Convert a FieldSchema to a FieldDescription.
fn field_schema_to_description(field: &FieldSchema) -> Result<FieldDescription, DynamicError> {
    Ok(FieldDescription {
        name: field.name.clone(),
        field_type: field_type_to_description(&field.field_type)?,
        default_value: String::new(), // Default values not included in hash
    })
}

/// Convert a dynamic FieldType to a FieldTypeDescription.
fn field_type_to_description(field_type: &FieldType) -> Result<FieldTypeDescription, DynamicError> {
    match field_type {
        // Primitives
        FieldType::Bool => Ok(FieldTypeDescription::primitive(TypeId::BOOL)),
        FieldType::Int8 => Ok(FieldTypeDescription::primitive(TypeId::INT8)),
        FieldType::Int16 => Ok(FieldTypeDescription::primitive(TypeId::INT16)),
        FieldType::Int32 => Ok(FieldTypeDescription::primitive(TypeId::INT32)),
        FieldType::Int64 => Ok(FieldTypeDescription::primitive(TypeId::INT64)),
        FieldType::Uint8 => Ok(FieldTypeDescription::primitive(TypeId::UINT8)),
        FieldType::Uint16 => Ok(FieldTypeDescription::primitive(TypeId::UINT16)),
        FieldType::Uint32 => Ok(FieldTypeDescription::primitive(TypeId::UINT32)),
        FieldType::Uint64 => Ok(FieldTypeDescription::primitive(TypeId::UINT64)),
        FieldType::Float32 => Ok(FieldTypeDescription::primitive(TypeId::FLOAT32)),
        FieldType::Float64 => Ok(FieldTypeDescription::primitive(TypeId::FLOAT64)),
        FieldType::String => Ok(FieldTypeDescription::primitive(TypeId::STRING)),

        // Bounded string
        FieldType::BoundedString(capacity) => Ok(FieldTypeDescription {
            type_id: TypeId::STRING,
            capacity: 0,
            string_capacity: *capacity as u64,
            nested_type_name: String::new(),
        }),

        // Nested message (single)
        FieldType::Message(schema) => Ok(FieldTypeDescription::nested(
            TypeId::NESTED_TYPE,
            &schema.type_name,
        )),

        // Fixed-size array
        FieldType::Array(inner, size) => {
            let base_type_id = get_base_type_id(inner)?;
            let array_type_id = base_type_id + TypeId::ARRAY_OFFSET;

            if let FieldType::Message(schema) = inner.as_ref() {
                Ok(FieldTypeDescription::nested_array(
                    array_type_id,
                    *size as u64,
                    &schema.type_name,
                ))
            } else {
                Ok(FieldTypeDescription::array(array_type_id, *size as u64))
            }
        }

        // Unbounded sequence
        FieldType::Sequence(inner) => {
            let base_type_id = get_base_type_id(inner)?;
            let seq_type_id = base_type_id + TypeId::UNBOUNDED_SEQUENCE_OFFSET;

            if let FieldType::Message(schema) = inner.as_ref() {
                Ok(FieldTypeDescription::nested(seq_type_id, &schema.type_name))
            } else {
                Ok(FieldTypeDescription::primitive(seq_type_id))
            }
        }

        // Bounded sequence
        FieldType::BoundedSequence(inner, capacity) => {
            let base_type_id = get_base_type_id(inner)?;
            let seq_type_id = base_type_id + TypeId::BOUNDED_SEQUENCE_OFFSET;

            if let FieldType::Message(schema) = inner.as_ref() {
                Ok(FieldTypeDescription::nested_array(
                    seq_type_id,
                    *capacity as u64,
                    &schema.type_name,
                ))
            } else {
                Ok(FieldTypeDescription::array(seq_type_id, *capacity as u64))
            }
        }
    }
}

/// Get the base type ID for a field type (without array/sequence modifiers).
fn get_base_type_id(field_type: &FieldType) -> Result<u8, DynamicError> {
    match field_type {
        FieldType::Bool => Ok(TypeId::BOOL),
        FieldType::Int8 => Ok(TypeId::INT8),
        FieldType::Int16 => Ok(TypeId::INT16),
        FieldType::Int32 => Ok(TypeId::INT32),
        FieldType::Int64 => Ok(TypeId::INT64),
        FieldType::Uint8 => Ok(TypeId::UINT8),
        FieldType::Uint16 => Ok(TypeId::UINT16),
        FieldType::Uint32 => Ok(TypeId::UINT32),
        FieldType::Uint64 => Ok(TypeId::UINT64),
        FieldType::Float32 => Ok(TypeId::FLOAT32),
        FieldType::Float64 => Ok(TypeId::FLOAT64),
        FieldType::String | FieldType::BoundedString(_) => Ok(TypeId::STRING),
        FieldType::Message(_) => Ok(TypeId::NESTED_TYPE),
        // For nested arrays/sequences, we get the innermost type
        FieldType::Array(inner, _)
        | FieldType::Sequence(inner)
        | FieldType::BoundedSequence(inner, _) => get_base_type_id(inner),
    }
}

/// Convert a TypeDescriptionMsg back to a MessageSchema.
pub fn type_description_msg_to_schema(
    msg: &TypeDescriptionMsg,
) -> Result<Arc<MessageSchema>, DynamicError> {
    // Build a map of all referenced types
    let mut type_map: HashMap<String, Arc<MessageSchema>> = HashMap::new();

    // First pass: create all schemas without resolving nested types
    for ref_td in &msg.referenced_type_descriptions {
        let schema = type_description_to_schema_partial(ref_td)?;
        type_map.insert(ref_td.type_name.clone(), Arc::new(schema));
    }

    // Second pass: resolve nested types in referenced schemas
    // Process in dependency order: iterate until all are resolved
    let mut resolved_map: HashMap<String, Arc<MessageSchema>> = HashMap::new();
    let mut remaining: Vec<&TypeDescription> = msg.referenced_type_descriptions.iter().collect();

    while !remaining.is_empty() {
        let mut made_progress = false;
        remaining.retain(|ref_td| {
            // Try to build with current resolved_map
            match type_description_to_schema_full(ref_td, &resolved_map) {
                Ok(schema) => {
                    resolved_map.insert(ref_td.type_name.clone(), Arc::new(schema));
                    made_progress = true;
                    false // Remove from remaining
                }
                Err(_) => true, // Keep in remaining, dependencies not ready yet
            }
        });

        if !made_progress && !remaining.is_empty() {
            // Circular dependency or missing type
            return Err(DynamicError::SerializationError(format!(
                "Cannot resolve dependencies for types: {}",
                remaining
                    .iter()
                    .map(|t| t.type_name.as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            )));
        }
    }

    // Build the main schema with resolved references
    type_description_to_schema_full(&msg.type_description, &resolved_map).map(Arc::new)
}

/// Create a partial MessageSchema without resolving nested types.
fn type_description_to_schema_partial(td: &TypeDescription) -> Result<MessageSchema, DynamicError> {
    let parts: Vec<&str> = td.type_name.split('/').collect();
    if parts.len() != 3 {
        return Err(DynamicError::InvalidTypeName(td.type_name.clone()));
    }

    Ok(MessageSchema {
        type_name: td.type_name.clone(),
        package: parts[0].to_string(),
        name: parts[2].to_string(),
        fields: Vec::new(), // Will be filled later
        type_hash: None,
    })
}

/// Create a full MessageSchema with resolved nested types.
fn type_description_to_schema_full(
    td: &TypeDescription,
    type_map: &HashMap<String, Arc<MessageSchema>>,
) -> Result<MessageSchema, DynamicError> {
    let parts: Vec<&str> = td.type_name.split('/').collect();
    if parts.len() != 3 {
        return Err(DynamicError::InvalidTypeName(td.type_name.clone()));
    }

    let fields = td
        .fields
        .iter()
        .map(|fd| field_description_to_schema(fd, type_map))
        .collect::<Result<Vec<_>, _>>()?;

    Ok(MessageSchema {
        type_name: td.type_name.clone(),
        package: parts[0].to_string(),
        name: parts[2].to_string(),
        fields,
        type_hash: None,
    })
}

/// Convert a FieldDescription back to a FieldSchema.
fn field_description_to_schema(
    fd: &FieldDescription,
    type_map: &HashMap<String, Arc<MessageSchema>>,
) -> Result<FieldSchema, DynamicError> {
    let field_type = field_type_description_to_type(&fd.field_type, type_map)?;
    Ok(FieldSchema {
        name: fd.name.clone(),
        field_type,
        default_value: None,
    })
}

/// Convert a FieldTypeDescription back to a FieldType.
fn field_type_description_to_type(
    ftd: &FieldTypeDescription,
    type_map: &HashMap<String, Arc<MessageSchema>>,
) -> Result<FieldType, DynamicError> {
    let base_type_id = TypeId::base_type(ftd.type_id);
    let is_array = TypeId::is_array(ftd.type_id);
    let is_bounded_seq = TypeId::is_bounded_sequence(ftd.type_id);
    let is_unbounded_seq = TypeId::is_unbounded_sequence(ftd.type_id);

    // Get the base field type
    let base_type = match base_type_id {
        TypeId::BOOL => FieldType::Bool,
        TypeId::INT8 => FieldType::Int8,
        TypeId::INT16 => FieldType::Int16,
        TypeId::INT32 => FieldType::Int32,
        TypeId::INT64 => FieldType::Int64,
        TypeId::UINT8 => FieldType::Uint8,
        TypeId::UINT16 => FieldType::Uint16,
        TypeId::UINT32 => FieldType::Uint32,
        TypeId::UINT64 => FieldType::Uint64,
        TypeId::FLOAT32 => FieldType::Float32,
        TypeId::FLOAT64 => FieldType::Float64,
        TypeId::STRING => {
            if ftd.string_capacity > 0 {
                FieldType::BoundedString(ftd.string_capacity as usize)
            } else {
                FieldType::String
            }
        }
        TypeId::NESTED_TYPE => {
            let schema = type_map.get(&ftd.nested_type_name).ok_or_else(|| {
                DynamicError::FieldNotFound(format!(
                    "Referenced type not found: {}",
                    ftd.nested_type_name
                ))
            })?;
            FieldType::Message(schema.clone())
        }
        _ => {
            return Err(DynamicError::SerializationError(format!(
                "Unknown type ID: {}",
                base_type_id
            )));
        }
    };

    // Apply array/sequence modifiers
    if is_array {
        Ok(FieldType::Array(Box::new(base_type), ftd.capacity as usize))
    } else if is_bounded_seq {
        Ok(FieldType::BoundedSequence(
            Box::new(base_type),
            ftd.capacity as usize,
        ))
    } else if is_unbounded_seq {
        Ok(FieldType::Sequence(Box::new(base_type)))
    } else {
        Ok(base_type)
    }
}

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

    #[test]
    fn test_primitive_type_to_description() {
        let schema = MessageSchema::builder("std_msgs/msg/Int32")
            .field("data", FieldType::Int32)
            .build()
            .unwrap();

        let td = schema.to_type_description().unwrap();
        assert_eq!(td.type_name, "std_msgs/msg/Int32");
        assert_eq!(td.fields.len(), 1);
        assert_eq!(td.fields[0].name, "data");
        assert_eq!(td.fields[0].field_type.type_id, TypeId::INT32);
    }

    #[test]
    fn test_string_type_to_description() {
        let schema = MessageSchema::builder("std_msgs/msg/String")
            .field("data", FieldType::String)
            .build()
            .unwrap();

        let td = schema.to_type_description().unwrap();
        assert_eq!(td.fields[0].field_type.type_id, TypeId::STRING);
    }

    #[test]
    fn test_array_type_to_description() {
        let schema = MessageSchema::builder("test_msgs/msg/ArrayTest")
            .field("data", FieldType::Array(Box::new(FieldType::Float64), 3))
            .build()
            .unwrap();

        let td = schema.to_type_description().unwrap();
        assert_eq!(td.fields[0].field_type.type_id, TypeId::FLOAT64_ARRAY);
        assert_eq!(td.fields[0].field_type.capacity, 3);
    }

    #[test]
    fn test_sequence_type_to_description() {
        let schema = MessageSchema::builder("test_msgs/msg/SeqTest")
            .field("data", FieldType::Sequence(Box::new(FieldType::Int32)))
            .build()
            .unwrap();

        let td = schema.to_type_description().unwrap();
        assert_eq!(
            td.fields[0].field_type.type_id,
            TypeId::INT32_UNBOUNDED_SEQUENCE
        );
    }

    #[test]
    fn test_nested_message_to_description() {
        let vector3_schema = MessageSchema::builder("geometry_msgs/msg/Vector3")
            .field("x", FieldType::Float64)
            .field("y", FieldType::Float64)
            .field("z", FieldType::Float64)
            .build()
            .unwrap();

        let twist_schema = MessageSchema::builder("geometry_msgs/msg/Twist")
            .field("linear", FieldType::Message(vector3_schema.clone()))
            .field("angular", FieldType::Message(vector3_schema))
            .build()
            .unwrap();

        let msg = twist_schema.to_type_description_msg().unwrap();
        assert_eq!(msg.type_description.type_name, "geometry_msgs/msg/Twist");
        assert_eq!(msg.referenced_type_descriptions.len(), 1); // Only Vector3
        assert_eq!(
            msg.referenced_type_descriptions[0].type_name,
            "geometry_msgs/msg/Vector3"
        );

        // Verify nested type fields
        assert_eq!(msg.type_description.fields[0].name, "linear");
        assert_eq!(
            msg.type_description.fields[0].field_type.type_id,
            TypeId::NESTED_TYPE
        );
        assert_eq!(
            msg.type_description.fields[0].field_type.nested_type_name,
            "geometry_msgs/msg/Vector3"
        );
    }

    #[test]
    fn test_type_hash_computation() {
        let schema = MessageSchema::builder("std_msgs/msg/String")
            .field("data", FieldType::String)
            .build()
            .unwrap();

        let hash = schema.compute_type_hash().unwrap();
        let rihs = hash.to_rihs_string();
        assert!(rihs.starts_with("RIHS01_"));
        assert_eq!(rihs.len(), 7 + 64); // RIHS01_ + 64 hex chars
    }

    #[test]
    fn test_roundtrip_conversion() {
        let original = MessageSchema::builder("geometry_msgs/msg/Point")
            .field("x", FieldType::Float64)
            .field("y", FieldType::Float64)
            .field("z", FieldType::Float64)
            .build()
            .unwrap();

        let msg = original.to_type_description_msg().unwrap();
        let restored = type_description_msg_to_schema(&msg).unwrap();

        assert_eq!(original.type_name, restored.type_name);
        assert_eq!(original.fields.len(), restored.fields.len());

        for (orig_field, restored_field) in original.fields.iter().zip(restored.fields.iter()) {
            assert_eq!(orig_field.name, restored_field.name);
        }
    }

    #[test]
    fn test_nested_roundtrip() {
        let vector3_schema = MessageSchema::builder("geometry_msgs/msg/Vector3")
            .field("x", FieldType::Float64)
            .field("y", FieldType::Float64)
            .field("z", FieldType::Float64)
            .build()
            .unwrap();

        let twist_schema = MessageSchema::builder("geometry_msgs/msg/Twist")
            .field("linear", FieldType::Message(vector3_schema.clone()))
            .field("angular", FieldType::Message(vector3_schema))
            .build()
            .unwrap();

        let msg = twist_schema.to_type_description_msg().unwrap();
        let restored = type_description_msg_to_schema(&msg).unwrap();

        assert_eq!(twist_schema.type_name, restored.type_name);
        assert_eq!(twist_schema.fields.len(), restored.fields.len());

        // Verify nested fields
        if let FieldType::Message(nested) = &restored.fields[0].field_type {
            assert_eq!(nested.type_name, "geometry_msgs/msg/Vector3");
            assert_eq!(nested.fields.len(), 3);
        } else {
            panic!("Expected Message type for linear field");
        }
    }

    #[test]
    fn test_all_primitive_types() {
        let schema = MessageSchema::builder("test_msgs/msg/AllPrimitives")
            .field("bool_val", FieldType::Bool)
            .field("int8_val", FieldType::Int8)
            .field("int16_val", FieldType::Int16)
            .field("int32_val", FieldType::Int32)
            .field("int64_val", FieldType::Int64)
            .field("uint8_val", FieldType::Uint8)
            .field("uint16_val", FieldType::Uint16)
            .field("uint32_val", FieldType::Uint32)
            .field("uint64_val", FieldType::Uint64)
            .field("float32_val", FieldType::Float32)
            .field("float64_val", FieldType::Float64)
            .field("string_val", FieldType::String)
            .build()
            .unwrap();

        let td = schema.to_type_description().unwrap();
        assert_eq!(td.fields.len(), 12);

        // Verify type IDs
        assert_eq!(td.fields[0].field_type.type_id, TypeId::BOOL);
        assert_eq!(td.fields[1].field_type.type_id, TypeId::INT8);
        assert_eq!(td.fields[2].field_type.type_id, TypeId::INT16);
        assert_eq!(td.fields[3].field_type.type_id, TypeId::INT32);
        assert_eq!(td.fields[4].field_type.type_id, TypeId::INT64);
        assert_eq!(td.fields[5].field_type.type_id, TypeId::UINT8);
        assert_eq!(td.fields[6].field_type.type_id, TypeId::UINT16);
        assert_eq!(td.fields[7].field_type.type_id, TypeId::UINT32);
        assert_eq!(td.fields[8].field_type.type_id, TypeId::UINT64);
        assert_eq!(td.fields[9].field_type.type_id, TypeId::FLOAT32);
        assert_eq!(td.fields[10].field_type.type_id, TypeId::FLOAT64);
        assert_eq!(td.fields[11].field_type.type_id, TypeId::STRING);
    }

    #[test]
    fn test_bounded_string() {
        let schema = MessageSchema::builder("test_msgs/msg/BoundedString")
            .field("bounded", FieldType::BoundedString(256))
            .build()
            .unwrap();

        let td = schema.to_type_description().unwrap();
        assert_eq!(td.fields[0].field_type.type_id, TypeId::STRING);
        assert_eq!(td.fields[0].field_type.string_capacity, 256);
    }

    #[test]
    fn test_bounded_sequence() {
        let schema = MessageSchema::builder("test_msgs/msg/BoundedSeq")
            .field(
                "data",
                FieldType::BoundedSequence(Box::new(FieldType::Uint8), 100),
            )
            .build()
            .unwrap();

        let td = schema.to_type_description().unwrap();
        assert_eq!(
            td.fields[0].field_type.type_id,
            TypeId::UINT8_BOUNDED_SEQUENCE
        );
        assert_eq!(td.fields[0].field_type.capacity, 100);
    }

    #[test]
    fn test_nested_array() {
        let point_schema = MessageSchema::builder("geometry_msgs/msg/Point")
            .field("x", FieldType::Float64)
            .field("y", FieldType::Float64)
            .field("z", FieldType::Float64)
            .build()
            .unwrap();

        let schema = MessageSchema::builder("test_msgs/msg/PointArray")
            .field(
                "points",
                FieldType::Array(Box::new(FieldType::Message(point_schema)), 10),
            )
            .build()
            .unwrap();

        let msg = schema.to_type_description_msg().unwrap();
        assert_eq!(
            msg.type_description.fields[0].field_type.type_id,
            TypeId::NESTED_TYPE_ARRAY
        );
        assert_eq!(msg.type_description.fields[0].field_type.capacity, 10);
        assert_eq!(
            msg.type_description.fields[0].field_type.nested_type_name,
            "geometry_msgs/msg/Point"
        );
        assert_eq!(msg.referenced_type_descriptions.len(), 1);
    }

    #[test]
    fn test_nested_unbounded_sequence() {
        let point_schema = MessageSchema::builder("geometry_msgs/msg/Point")
            .field("x", FieldType::Float64)
            .field("y", FieldType::Float64)
            .field("z", FieldType::Float64)
            .build()
            .unwrap();

        let schema = MessageSchema::builder("test_msgs/msg/PointList")
            .field(
                "points",
                FieldType::Sequence(Box::new(FieldType::Message(point_schema))),
            )
            .build()
            .unwrap();

        let msg = schema.to_type_description_msg().unwrap();
        assert_eq!(
            msg.type_description.fields[0].field_type.type_id,
            TypeId::NESTED_TYPE_UNBOUNDED_SEQUENCE
        );
        assert_eq!(
            msg.type_description.fields[0].field_type.nested_type_name,
            "geometry_msgs/msg/Point"
        );
    }

    #[test]
    fn test_deeply_nested_three_levels() {
        // Inner -> Middle -> Outer
        let inner_schema = MessageSchema::builder("test_msgs/msg/Inner")
            .field("value", FieldType::Float64)
            .build()
            .unwrap();

        let middle_schema = MessageSchema::builder("test_msgs/msg/Middle")
            .field("inner", FieldType::Message(inner_schema))
            .build()
            .unwrap();

        let outer_schema = MessageSchema::builder("test_msgs/msg/Outer")
            .field("middle", FieldType::Message(middle_schema))
            .build()
            .unwrap();

        let msg = outer_schema.to_type_description_msg().unwrap();

        // Should have 2 referenced types: Inner and Middle
        assert_eq!(msg.referenced_type_descriptions.len(), 2);

        // Referenced types should be sorted alphabetically
        let ref_names: Vec<&str> = msg
            .referenced_type_descriptions
            .iter()
            .map(|td| td.type_name.as_str())
            .collect();
        assert!(ref_names.contains(&"test_msgs/msg/Inner"));
        assert!(ref_names.contains(&"test_msgs/msg/Middle"));
    }

    #[test]
    fn test_hash_deterministic_and_valid() {
        // std_msgs/msg/String - verify hash is deterministic and well-formed
        // Note: To verify against ROS 2, run: ros2 interface show std_msgs/msg/String --show-hash
        let schema = MessageSchema::builder("std_msgs/msg/String")
            .field("data", FieldType::String)
            .build()
            .unwrap();

        let hash1 = schema.compute_type_hash().unwrap();
        let hash2 = schema.compute_type_hash().unwrap();

        // Hash should be deterministic
        assert_eq!(hash1, hash2);

        // Hash should be valid RIHS01 format
        let rihs = hash1.to_rihs_string();
        assert!(rihs.starts_with("RIHS01_"));
        assert_eq!(rihs.len(), 7 + 64);

        // Verify our computed hash (for regression testing)
        assert_eq!(
            rihs,
            "RIHS01_df668c740482bbd48fb39d76a70dfd4bd59db1288021743503259e948f6b1a18"
        );
    }

    #[test]
    fn test_roundtrip_with_arrays() {
        let original = MessageSchema::builder("test_msgs/msg/Arrays")
            .field("fixed", FieldType::Array(Box::new(FieldType::Int32), 5))
            .field(
                "unbounded",
                FieldType::Sequence(Box::new(FieldType::Float64)),
            )
            .field(
                "bounded",
                FieldType::BoundedSequence(Box::new(FieldType::Uint8), 100),
            )
            .build()
            .unwrap();

        let msg = original.to_type_description_msg().unwrap();
        let restored = type_description_msg_to_schema(&msg).unwrap();

        assert_eq!(original.type_name, restored.type_name);
        assert_eq!(original.fields.len(), restored.fields.len());

        // Verify array types
        if let FieldType::Array(inner, size) = &restored.fields[0].field_type {
            assert!(matches!(inner.as_ref(), FieldType::Int32));
            assert_eq!(*size, 5);
        } else {
            panic!("Expected Array type");
        }

        if let FieldType::Sequence(inner) = &restored.fields[1].field_type {
            assert!(matches!(inner.as_ref(), FieldType::Float64));
        } else {
            panic!("Expected Sequence type");
        }

        if let FieldType::BoundedSequence(inner, cap) = &restored.fields[2].field_type {
            assert!(matches!(inner.as_ref(), FieldType::Uint8));
            assert_eq!(*cap, 100);
        } else {
            panic!("Expected BoundedSequence type");
        }
    }

    #[test]
    fn test_referenced_types_sorted() {
        // Create schema with multiple nested types to verify sorting
        let type_c = MessageSchema::builder("pkg/msg/TypeC")
            .field("c", FieldType::Int32)
            .build()
            .unwrap();

        let type_a = MessageSchema::builder("pkg/msg/TypeA")
            .field("a", FieldType::Int32)
            .build()
            .unwrap();

        let type_b = MessageSchema::builder("pkg/msg/TypeB")
            .field("b", FieldType::Int32)
            .build()
            .unwrap();

        // Add in non-alphabetical order
        let main = MessageSchema::builder("pkg/msg/Main")
            .field("c", FieldType::Message(type_c))
            .field("a", FieldType::Message(type_a))
            .field("b", FieldType::Message(type_b))
            .build()
            .unwrap();

        let msg = main.to_type_description_msg().unwrap();

        // Referenced types should be sorted alphabetically
        let ref_names: Vec<&str> = msg
            .referenced_type_descriptions
            .iter()
            .map(|td| td.type_name.as_str())
            .collect();

        assert_eq!(ref_names[0], "pkg/msg/TypeA");
        assert_eq!(ref_names[1], "pkg/msg/TypeB");
        assert_eq!(ref_names[2], "pkg/msg/TypeC");
    }

    #[test]
    fn test_duplicate_nested_types_deduplicated() {
        // Same nested type used multiple times should only appear once
        let point_schema = MessageSchema::builder("geometry_msgs/msg/Point")
            .field("x", FieldType::Float64)
            .field("y", FieldType::Float64)
            .field("z", FieldType::Float64)
            .build()
            .unwrap();

        let schema = MessageSchema::builder("test_msgs/msg/Triangle")
            .field("p1", FieldType::Message(point_schema.clone()))
            .field("p2", FieldType::Message(point_schema.clone()))
            .field("p3", FieldType::Message(point_schema))
            .build()
            .unwrap();

        let msg = schema.to_type_description_msg().unwrap();

        // Point should only appear once in referenced types
        assert_eq!(msg.referenced_type_descriptions.len(), 1);
        assert_eq!(
            msg.referenced_type_descriptions[0].type_name,
            "geometry_msgs/msg/Point"
        );
    }
}