rustyasn 0.7.4

Abstract Syntax Notation One (ASN.1) encoding support for RustyFix
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
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
//! ASN.1 field type implementations that integrate with `RustyFix` `FieldType` trait.
//!
//! This module provides ASN.1 wrapper types that implement the `RustyFix` `FieldType` trait,
//! enabling seamless integration between ASN.1 encoding and the `RustyFix` ecosystem.

use crate::error::{DecodeError, EncodeError};
use rasn::{AsnType, Decode, Encode};
use rustyfix::{Buffer, FieldType};
use std::convert::TryFrom;

/// Error type for ASN.1 field type operations.
#[derive(Debug, thiserror::Error)]
pub enum Asn1FieldError {
    /// Invalid ASN.1 encoding.
    #[error("Invalid ASN.1 encoding: {0}")]
    Encode(#[from] EncodeError),
    /// Invalid ASN.1 decoding.
    #[error("Invalid ASN.1 decoding: {0}")]
    Decode(#[from] DecodeError),
    /// Invalid UTF-8 string.
    #[error("Invalid UTF-8 string: {0}")]
    Utf8(#[from] std::str::Utf8Error),
    /// Invalid numeric value.
    #[error("Invalid numeric value")]
    InvalidNumber,
    /// Invalid boolean value.
    #[error("Invalid boolean value")]
    InvalidBool,
    /// Repeating group parsing is not yet supported.
    #[error("Repeating group parsing is not yet supported. Group field tag: {tag}, count: {count}")]
    GroupParsingUnsupported {
        /// The field tag for the group count field
        tag: u32,
        /// The number of group entries that were expected
        count: usize,
    },
}

/// ASN.1 wrapper for UTF-8 strings.
#[derive(AsnType, Debug, Clone, PartialEq, Eq, Encode, Decode)]
#[rasn(crate_root = "rasn")]
pub struct Asn1String {
    #[rasn(tag(0))]
    inner: String,
}

impl Asn1String {
    /// Creates a new ASN.1 string.
    pub fn new(value: String) -> Self {
        Self { inner: value }
    }

    /// Gets the inner string value.
    pub fn as_str(&self) -> &str {
        &self.inner
    }

    /// Converts to inner string.
    pub fn into_string(self) -> String {
        self.inner
    }
}

impl From<String> for Asn1String {
    fn from(value: String) -> Self {
        Self::new(value)
    }
}

impl From<&str> for Asn1String {
    fn from(value: &str) -> Self {
        Self::new(value.to_string())
    }
}

impl<'a> FieldType<'a> for Asn1String {
    type Error = Asn1FieldError;
    type SerializeSettings = ();

    fn serialize_with<B>(&self, buffer: &mut B, _settings: Self::SerializeSettings) -> usize
    where
        B: Buffer,
    {
        // For FIX compatibility, serialize as plain UTF-8 string
        buffer.extend_from_slice(self.inner.as_bytes());
        self.inner.len()
    }

    fn deserialize(data: &'a [u8]) -> Result<Self, Self::Error> {
        let s = std::str::from_utf8(data)?;
        Ok(Self::new(s.to_string()))
    }

    fn deserialize_lossy(data: &'a [u8]) -> Result<Self, Self::Error> {
        // For lossy deserialization, use String::from_utf8_lossy
        let s = String::from_utf8_lossy(data);
        Ok(Self::new(s.to_string()))
    }
}

/// ASN.1 wrapper for integers.
#[derive(AsnType, Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
#[rasn(crate_root = "rasn")]
pub struct Asn1Integer {
    #[rasn(tag(0))]
    inner: i64,
}

impl Asn1Integer {
    /// Creates a new ASN.1 integer.
    pub fn new(value: i64) -> Self {
        Self { inner: value }
    }

    /// Gets the inner integer value.
    pub fn value(&self) -> i64 {
        self.inner
    }
}

impl From<i64> for Asn1Integer {
    fn from(value: i64) -> Self {
        Self::new(value)
    }
}

impl From<u32> for Asn1Integer {
    fn from(value: u32) -> Self {
        Self::new(i64::from(value))
    }
}

impl From<i32> for Asn1Integer {
    fn from(value: i32) -> Self {
        Self::new(i64::from(value))
    }
}

impl TryFrom<Asn1Integer> for u32 {
    type Error = Asn1FieldError;

    fn try_from(value: Asn1Integer) -> Result<Self, Self::Error> {
        u32::try_from(value.inner).map_err(|_| Asn1FieldError::InvalidNumber)
    }
}

impl<'a> FieldType<'a> for Asn1Integer {
    type Error = Asn1FieldError;
    type SerializeSettings = ();

    fn serialize_with<B>(&self, buffer: &mut B, _settings: Self::SerializeSettings) -> usize
    where
        B: Buffer,
    {
        // For FIX compatibility, serialize as decimal string
        let s = ToString::to_string(&self.inner);
        buffer.extend_from_slice(s.as_bytes());
        s.len()
    }

    fn deserialize(data: &'a [u8]) -> Result<Self, Self::Error> {
        let s = std::str::from_utf8(data)?;
        let value = s
            .parse::<i64>()
            .map_err(|_| Asn1FieldError::InvalidNumber)?;
        Ok(Self::new(value))
    }

    fn deserialize_lossy(data: &'a [u8]) -> Result<Self, Self::Error> {
        // For lossy parsing, try to parse as much as possible
        let mut result = 0i64;
        let mut sign = 1i64;
        let mut idx = 0;

        if data.is_empty() {
            return Ok(Self::new(0));
        }

        // Handle sign
        if data[0] == b'-' {
            sign = -1;
            idx = 1;
        } else if data[0] == b'+' {
            idx = 1;
        }

        // Parse digits
        while idx < data.len() && data[idx].is_ascii_digit() {
            result = result
                .saturating_mul(10)
                .saturating_add(i64::from(data[idx] - b'0'));
            idx += 1;
        }

        Ok(Self::new(result * sign))
    }
}

/// ASN.1 wrapper for unsigned integers.
#[derive(AsnType, Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
#[rasn(crate_root = "rasn")]
pub struct Asn1UInteger {
    #[rasn(tag(0))]
    inner: u64,
}

impl Asn1UInteger {
    /// Creates a new ASN.1 unsigned integer.
    pub fn new(value: u64) -> Self {
        Self { inner: value }
    }

    /// Gets the inner unsigned integer value.
    pub fn value(&self) -> u64 {
        self.inner
    }
}

impl From<u64> for Asn1UInteger {
    fn from(value: u64) -> Self {
        Self::new(value)
    }
}

impl From<u32> for Asn1UInteger {
    fn from(value: u32) -> Self {
        Self::new(u64::from(value))
    }
}

impl From<u16> for Asn1UInteger {
    fn from(value: u16) -> Self {
        Self::new(u64::from(value))
    }
}

impl TryFrom<Asn1UInteger> for u32 {
    type Error = Asn1FieldError;

    fn try_from(value: Asn1UInteger) -> Result<Self, Self::Error> {
        u32::try_from(value.inner).map_err(|_| Asn1FieldError::InvalidNumber)
    }
}

impl TryFrom<Asn1UInteger> for u16 {
    type Error = Asn1FieldError;

    fn try_from(value: Asn1UInteger) -> Result<Self, Self::Error> {
        u16::try_from(value.inner).map_err(|_| Asn1FieldError::InvalidNumber)
    }
}

impl<'a> FieldType<'a> for Asn1UInteger {
    type Error = Asn1FieldError;
    type SerializeSettings = ();

    fn serialize_with<B>(&self, buffer: &mut B, _settings: Self::SerializeSettings) -> usize
    where
        B: Buffer,
    {
        // For FIX compatibility, serialize as decimal string
        let s = ToString::to_string(&self.inner);
        buffer.extend_from_slice(s.as_bytes());
        s.len()
    }

    fn deserialize(data: &'a [u8]) -> Result<Self, Self::Error> {
        let s = std::str::from_utf8(data)?;
        let value = s
            .parse::<u64>()
            .map_err(|_| Asn1FieldError::InvalidNumber)?;
        Ok(Self::new(value))
    }

    fn deserialize_lossy(data: &'a [u8]) -> Result<Self, Self::Error> {
        // For lossy parsing, try to parse as much as possible
        let mut result = 0u64;
        let mut idx = 0;

        if data.is_empty() {
            return Ok(Self::new(0));
        }

        // Skip leading plus sign
        if data[0] == b'+' {
            idx = 1;
        }

        // Parse digits
        while idx < data.len() && data[idx].is_ascii_digit() {
            result = result
                .saturating_mul(10)
                .saturating_add(u64::from(data[idx] - b'0'));
            idx += 1;
        }

        Ok(Self::new(result))
    }
}

/// ASN.1 wrapper for boolean values.
#[derive(AsnType, Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
#[rasn(crate_root = "rasn")]
pub struct Asn1Boolean {
    #[rasn(tag(0))]
    inner: bool,
}

impl Asn1Boolean {
    /// Creates a new ASN.1 boolean.
    pub fn new(value: bool) -> Self {
        Self { inner: value }
    }

    /// Gets the inner boolean value.
    pub fn value(&self) -> bool {
        self.inner
    }
}

impl From<bool> for Asn1Boolean {
    fn from(value: bool) -> Self {
        Self::new(value)
    }
}

impl From<Asn1Boolean> for bool {
    fn from(value: Asn1Boolean) -> Self {
        value.inner
    }
}

impl<'a> FieldType<'a> for Asn1Boolean {
    type Error = Asn1FieldError;
    type SerializeSettings = ();

    fn serialize_with<B>(&self, buffer: &mut B, _settings: Self::SerializeSettings) -> usize
    where
        B: Buffer,
    {
        // For FIX compatibility, serialize as Y/N
        buffer.extend_from_slice(if self.inner { b"Y" } else { b"N" });
        1
    }

    fn deserialize(data: &'a [u8]) -> Result<Self, Self::Error> {
        match data {
            b"Y" | b"y" | b"1" | b"true" | b"TRUE" | b"True" => Ok(Self::new(true)),
            b"N" | b"n" | b"0" | b"false" | b"FALSE" | b"False" => Ok(Self::new(false)),
            _ => Err(Asn1FieldError::InvalidBool),
        }
    }

    fn deserialize_lossy(data: &'a [u8]) -> Result<Self, Self::Error> {
        // For lossy parsing, be more liberal
        if data.is_empty() {
            return Ok(Self::new(false));
        }

        match data[0] {
            b'Y' | b'y' | b'T' | b't' | b'1' => Ok(Self::new(true)),
            _ => Ok(Self::new(false)),
        }
    }
}

/// ASN.1 wrapper for byte arrays.
#[derive(AsnType, Debug, Clone, PartialEq, Eq, Encode, Decode)]
#[rasn(crate_root = "rasn")]
pub struct Asn1Bytes {
    #[rasn(tag(0))]
    inner: Vec<u8>,
}

impl Asn1Bytes {
    /// Creates a new ASN.1 byte array.
    pub fn new(data: Vec<u8>) -> Self {
        Self { inner: data }
    }

    /// Gets the inner byte array.
    pub fn as_bytes(&self) -> &[u8] {
        &self.inner
    }

    /// Converts to inner byte vector.
    pub fn into_bytes(self) -> Vec<u8> {
        self.inner
    }
}

impl From<Vec<u8>> for Asn1Bytes {
    fn from(data: Vec<u8>) -> Self {
        Self::new(data)
    }
}

impl From<&[u8]> for Asn1Bytes {
    fn from(data: &[u8]) -> Self {
        Self::new(data.to_vec())
    }
}

impl<'a> FieldType<'a> for Asn1Bytes {
    type Error = Asn1FieldError;
    type SerializeSettings = ();

    fn serialize_with<B>(&self, buffer: &mut B, _settings: Self::SerializeSettings) -> usize
    where
        B: Buffer,
    {
        // For FIX compatibility, serialize as raw bytes
        buffer.extend_from_slice(&self.inner);
        self.inner.len()
    }

    fn deserialize(data: &'a [u8]) -> Result<Self, Self::Error> {
        Ok(Self::new(data.to_vec()))
    }
}

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

    #[test]
    fn test_asn1_string_field_type() {
        let value = Asn1String::from("Hello World");

        // Test serialization
        let mut buffer: smallvec::SmallVec<[u8; 64]> = smallvec::SmallVec::new();
        let len = value.serialize(&mut buffer);
        assert_eq!(len, 11);
        assert_eq!(&buffer[..], b"Hello World");

        // Test deserialization
        let deserialized = Asn1String::deserialize(b"Test String")
            .expect("Failed to deserialize valid UTF-8 string");
        assert_eq!(deserialized.as_str(), "Test String");

        // Test lossy deserialization with invalid UTF-8
        let lossy = Asn1String::deserialize_lossy(b"Valid UTF-8")
            .expect("Lossy deserialization should not fail");
        assert_eq!(lossy.as_str(), "Valid UTF-8");
    }

    #[test]
    fn test_asn1_integer_field_type() {
        let value = Asn1Integer::from(42i64);

        // Test serialization
        let mut buffer: smallvec::SmallVec<[u8; 64]> = smallvec::SmallVec::new();
        let len = value.serialize(&mut buffer);
        assert_eq!(len, 2);
        assert_eq!(&buffer[..], b"42");

        // Test negative number
        let negative = Asn1Integer::from(-123i64);
        let mut buffer: smallvec::SmallVec<[u8; 64]> = smallvec::SmallVec::new();
        let len = negative.serialize(&mut buffer);
        assert_eq!(len, 4);
        assert_eq!(&buffer[..], b"-123");

        // Test deserialization
        let deserialized =
            Asn1Integer::deserialize(b"456").expect("Failed to deserialize valid integer");
        assert_eq!(deserialized.value(), 456);

        // Test lossy deserialization
        let lossy = Asn1Integer::deserialize_lossy(b"789abc")
            .expect("Lossy integer deserialization should not fail");
        assert_eq!(lossy.value(), 789);
    }

    #[test]
    fn test_asn1_uinteger_field_type() {
        let value = Asn1UInteger::from(123u64);

        // Test serialization
        let mut buffer: smallvec::SmallVec<[u8; 64]> = smallvec::SmallVec::new();
        let len = value.serialize(&mut buffer);
        assert_eq!(len, 3);
        assert_eq!(&buffer[..], b"123");

        // Test deserialization
        let deserialized = Asn1UInteger::deserialize(b"456")
            .expect("Failed to deserialize valid unsigned integer");
        assert_eq!(deserialized.value(), 456);

        // Test conversion
        let as_u32: u32 = deserialized.try_into().expect("Failed to convert to u32");
        assert_eq!(as_u32, 456);
    }

    #[test]
    fn test_asn1_boolean_field_type() {
        let true_value = Asn1Boolean::from(true);
        let false_value = Asn1Boolean::from(false);

        // Test serialization
        let mut buffer: smallvec::SmallVec<[u8; 64]> = smallvec::SmallVec::new();
        let len = true_value.serialize(&mut buffer);
        assert_eq!(len, 1);
        assert_eq!(&buffer[..], b"Y");

        let mut buffer: smallvec::SmallVec<[u8; 64]> = smallvec::SmallVec::new();
        let len = false_value.serialize(&mut buffer);
        assert_eq!(len, 1);
        assert_eq!(&buffer[..], b"N");

        // Test deserialization
        assert!(
            Asn1Boolean::deserialize(b"Y")
                .expect("Failed to deserialize 'Y' as boolean")
                .value()
        );
        assert!(
            Asn1Boolean::deserialize(b"1")
                .expect("Failed to deserialize '1' as boolean")
                .value()
        );
        assert!(
            !Asn1Boolean::deserialize(b"N")
                .expect("Failed to deserialize 'N' as boolean")
                .value()
        );
        assert!(
            !Asn1Boolean::deserialize(b"0")
                .expect("Failed to deserialize '0' as boolean")
                .value()
        );

        // Test lossy deserialization
        assert!(
            Asn1Boolean::deserialize_lossy(b"Yes")
                .expect("Lossy boolean deserialization should not fail")
                .value()
        );
        assert!(
            !Asn1Boolean::deserialize_lossy(b"No")
                .expect("Lossy boolean deserialization should not fail")
                .value()
        );
    }

    #[test]
    fn test_asn1_bytes_field_type() {
        let data = vec![0x01, 0x02, 0x03, 0xFF];
        let value = Asn1Bytes::from(data.clone());

        // Test serialization
        let mut buffer: smallvec::SmallVec<[u8; 64]> = smallvec::SmallVec::new();
        let len = value.serialize(&mut buffer);
        assert_eq!(len, 4);
        assert_eq!(&buffer[..], &data[..]);

        // Test deserialization
        let deserialized = Asn1Bytes::deserialize(&data).expect("Failed to deserialize byte array");
        assert_eq!(deserialized.as_bytes(), &data[..]);
    }
}