synta 0.1.3

ASN.1 parser, decoder, and encoder library with DER/BER support and C FFI
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
# Troubleshooting Guide

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*

- [Installation Issues]#installation-issues
  - [Problem: `cargo install synta-codegen` fails]#problem-cargo-install-synta-codegen-fails
- [Parsing Errors]#parsing-errors
  - [Problem: "Expected identifier, got Symbol"]#problem-expected-identifier-got-symbol
  - [Problem: "Unexpected token" in constraints]#problem-unexpected-token-in-constraints
  - [Problem: "Unknown type" errors]#problem-unknown-type-errors
- [Code Generation Issues]#code-generation-issues
  - [Problem: Generated code has naming conflicts]#problem-generated-code-has-naming-conflicts
  - [Problem: Field names become invalid Rust identifiers]#problem-field-names-become-invalid-rust-identifiers
  - [Problem: SEQUENCE OF generates Vec instead of array]#problem-sequence-of-generates-vec-instead-of-array
- [Compilation Errors]#compilation-errors
  - [Problem: "cannot find type `Integer` in this scope"]#problem-cannot-find-type-integer-in-this-scope
  - [Problem: "trait `Encode` is not implemented"]#problem-trait-encode-is-not-implemented
  - [Problem: "use of undeclared type" for imported types]#problem-use-of-undeclared-type-for-imported-types
- [Runtime Issues]#runtime-issues
  - [Problem: Constraint validation fails unexpectedly]#problem-constraint-validation-fails-unexpectedly
  - [Problem: PATTERN validation not working]#problem-pattern-validation-not-working
  - [Problem: CONTAINING validation not working]#problem-containing-validation-not-working
  - [Problem: Decoding fails with "unexpected tag"]#problem-decoding-fails-with-unexpected-tag
- [Performance Issues]#performance-issues
  - [Problem: Code generation is slow]#problem-code-generation-is-slow
  - [Problem: Generated code compiles slowly]#problem-generated-code-compiles-slowly
  - [Problem: Runtime validation is slow]#problem-runtime-validation-is-slow
- [Getting Help]#getting-help
- [Quick Reference: Common Error Patterns]#quick-reference-common-error-patterns
- [Debug Mode]#debug-mode

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

Common issues and solutions when using synta-codegen.

## Table of Contents

- [Installation Issues]#installation-issues
- [Parsing Errors]#parsing-errors
- [Code Generation Issues]#code-generation-issues
- [Compilation Errors]#compilation-errors
- [Runtime Issues]#runtime-issues
- [Performance Issues]#performance-issues

---

## Installation Issues

### Problem: `cargo install synta-codegen` fails

**Symptoms:**
```
error: failed to compile synta-codegen
```

**Solutions:**

1. **Update Rust toolchain:**
   ```bash
   rustup update stable
   ```

2. **Check Rust version:**
   ```bash
   rustc --version  # Should be 1.70+ for edition 2021
   ```

3. **Install from source:**
   ```bash
   git clone https://codeberg.org/abbra/synta
   cd synta/synta-codegen
   cargo install --path .
   ```

---

## Parsing Errors

### Problem: "Expected identifier, got Symbol"

**Example:**
```
ParseError { message: "Expected identifier, got Symbol(\"{\")", line: 5, column: 20 }
```

**Common Causes:**

1. **Missing type name before definition:**
   ```asn1
   -- Wrong:
   ::= SEQUENCE { ... }
   
   -- Right:
   MyType ::= SEQUENCE { ... }
   ```

2. **Reserved keyword as identifier:**
   ```asn1
   -- Wrong:
   type ::= INTEGER
   
   -- Right:
   typeValue ::= INTEGER
   ```

**Solution:** Check the line and column number in the error message, verify syntax.

### Problem: "Unexpected token" in constraints

**Example:**
```asn1
MyString ::= IA5String (SIZE(1..10))  -- Missing space
```

**Solution:** Add proper spacing:
```asn1
MyString ::= IA5String (SIZE (1..10))  -- Correct
```

### Problem: "Unknown type" errors

**Example:**
```
ParseError: Unknown type 'Foo'
```

**Common Causes:**

1. **Type not defined:**
   ```asn1
   User ::= SEQUENCE {
       status  UserStatus  -- UserStatus not defined
   }
   ```

2. **Type not imported:**
   ```asn1
   MyModule DEFINITIONS ::= BEGIN
       -- Missing: IMPORTS UserStatus FROM OtherModule;
       User ::= SEQUENCE {
           status  UserStatus
       }
   END
   ```

**Solution:** Define or import the type:
```asn1
MyModule DEFINITIONS ::= BEGIN
    IMPORTS UserStatus FROM OtherModule;
    
    User ::= SEQUENCE {
        status  UserStatus
    }
END
```

---

## Code Generation Issues

### Problem: Generated code has naming conflicts

**Symptoms:**
```rust
error[E0428]: the name `MyType` is defined multiple times
```

**Cause:** Same type name in multiple modules without proper namespacing.

**Solution:** Use module prefixes when generating:
```bash
synta-codegen module1.asn1 --crate-imports -o src/module1.rs
synta-codegen module2.asn1 --crate-imports -o src/module2.rs
```

### Problem: Field names become invalid Rust identifiers

**Symptoms:**
```rust
pub struct MyStruct {
    pub type: Integer,  // 'type' is a keyword!
}
```

**Cause:** ASN.1 allows identifiers that are Rust keywords.

**Generated Code:** synta-codegen automatically escapes:
```rust
pub struct MyStruct {
    pub r#type: Integer,  // Escaped with r#
}
```

**Usage:**
```rust
let s = MyStruct {
    r#type: Integer::from(1),
};
```

### Problem: SEQUENCE OF generates Vec instead of array

**Expected:**
```rust
pub fixed: [Integer; 10]
```

**Generated:**
```rust
pub items: Vec<Integer>
```

**Explanation:** ASN.1 SEQUENCE OF is variable-length by default. Use SIZE constraint for documentation:
```asn1
FixedArray ::= SEQUENCE (SIZE (10)) OF INTEGER
```

Note: This still generates `Vec<Integer>`, but validates at runtime.

---

## Compilation Errors

### Problem: "cannot find type `Integer` in this scope"

**Symptoms:**
```
error[E0412]: cannot find type `Integer` in this scope
```

**Solution:** Add synta dependency to Cargo.toml:
```toml
[dependencies]
synta = "0.1"
```

### Problem: "trait `Encode` is not implemented"

**Symptoms:**
```
error[E0277]: the trait `Encode` is not implemented for `MyType`
```

**Causes:**

1. **Missing derive attribute:**
   ```rust
   // Add #[cfg_attr(feature = "derive", derive(Asn1Sequence))]
   ```

2. **Missing synta import:**
   ```rust
   use synta::{Encode, Decode};
   ```

3. **Custom type without Encode implementation:**
   ```rust
   // You need to manually implement Encode for custom types
   impl Encode for MyCustomType { ... }
   ```

### Problem: "use of undeclared type" for imported types

**Symptoms:**
```
error[E0412]: cannot find type `ImportedType` in this scope
```

**Cause:** Missing module import in Rust code.

**Solution:** Check generated imports at top of file:
```rust
use crate::other_module::ImportedType;
```

If missing, regenerate with `--crate-imports`:
```bash
synta-codegen schema.asn1 --crate-imports -o output.rs
```

---

## Runtime Issues

### Problem: Constraint validation fails unexpectedly

**Symptoms:**
```rust
let port = HttpPort::new(Integer::from(80))?;  // Error!
```

**Debugging:**

1. **Check constraint definition:**
   ```asn1
   HttpPort ::= INTEGER (80 | 443 | 8080)
   ```

2. **Verify value is in allowed set:**
   ```rust
   // Valid values: 80, 443, 8080
   let port = HttpPort::new(Integer::from(80))?;  // OK
   let port = HttpPort::new(Integer::from(8000))?;  // Error!
   ```

3. **Use unchecked constructor for testing:**
   ```rust
   let port = HttpPort::new_unchecked(Integer::from(8000));  // Bypasses validation
   ```

### Problem: PATTERN validation not working

**Symptoms:**
```rust
let email = Email::new(IA5String::from("invalid"))?;  // Should fail but doesn't
```

**Cause:** PATTERN validation requires `regex` feature.

**Solution:** Enable feature in Cargo.toml:
```toml
[dependencies]
synta = { version = "0.1", features = ["regex"] }
regex = "1.10"
once_cell = "1.19"
```

### Problem: CONTAINING validation not working

**Cause:** CONTAINING validation requires `validate_containing` feature.

**Solution:**
```toml
[dependencies]
synta = { version = "0.1", features = ["validate_containing"] }
```

### Problem: Decoding fails with "unexpected tag"

**Symptoms:**
```
Error: DecodeError { message: "unexpected tag" }
```

**Common Causes:**

1. **Wrong encoding format:**
   ```rust
   // Data is BER, but decoding as DER
   let result = synta::decode_der(&ber_data)?;  // Error!
   
   // Solution: Use correct decoder
   let result = synta::decode_ber(&ber_data)?;  // OK
   ```

2. **Wrong type:**
   ```rust
   let result: WrongType = synta::decode_der(&data)?;  // Error if data is DifferentType
   ```

3. **Corrupted data:**
   ```rust
   // Verify data integrity first
   ```

---

## Performance Issues

### Problem: Code generation is slow

**Symptoms:** Large schemas take minutes to generate.

**Solutions:**

1. **Split large schemas into modules:**
   ```bash
   # Instead of one 10,000 line schema
   synta-codegen huge-schema.asn1
   
   # Split into smaller modules
   synta-codegen base-types.asn1 -o base.rs
   synta-codegen extensions.asn1 -o ext.rs
   ```

2. **Use parallel generation for multiple files:**
   ```bash
   ls schemas/*.asn1 | xargs -P 4 -I {} synta-codegen {} -o generated/{}.rs
   ```

### Problem: Generated code compiles slowly

**Cause:** Very large generated files.

**Solutions:**

1. **Enable incremental compilation:**
   ```toml
   [profile.dev]
   incremental = true
   ```

2. **Split into multiple modules**

3. **Use `--release` for final builds:**
   ```bash
   cargo build --release
   ```

### Problem: Runtime validation is slow

**Cause:** Complex constraints with many unions/intersections.

**Solutions:**

1. **Use `new_unchecked()` for trusted data:**
   ```rust
   // For data from trusted sources
   let port = HttpPort::new_unchecked(value);
   ```

2. **Simplify constraints if possible:**
   ```asn1
   -- Instead of: INTEGER (1|2|3|4|5|6|7|8|9|10|...)
   -- Use: INTEGER (1..100)
   ```

3. **Cache validated values:**
   ```rust
   // Validate once, reuse many times
   let validated = MyType::new(value)?;
   ```

---

## Getting Help

If your issue isn't covered here:

1. **Check documentation:**
   - [ASN.1 Support]asn1-support.md
   - [Limitations]limitations.md
   - [Tutorial]tutorial.md

2. **Verify your ASN.1 syntax:**
   - Check against ITU-T X.680 specification
   - Try a simpler version to isolate the problem

3. **Create minimal reproduction:**
   ```asn1
   TestModule DEFINITIONS ::= BEGIN
       SimpleType ::= INTEGER  -- Simplest possible case
   END
   ```

4. **Report issues:**
   - GitHub: https://codeberg.org/abbra/synta/issues
   - Include: ASN.1 input, command used, error message, Rust version

---

## Quick Reference: Common Error Patterns

| Error Message | Likely Cause | Quick Fix |
|---------------|--------------|-----------|
| "Expected identifier" | Syntax error | Check ASN.1 syntax |
| "Unknown type" | Missing import/definition | Add IMPORTS or define type |
| "cannot find type `Integer`" | Missing synta dependency | Add to Cargo.toml |
| "unexpected tag" | Wrong decoder or corrupted data | Use correct decode_* function |
| "constraint validation failed" | Value out of range | Check constraint definition |
| "PATTERN validation disabled" | Missing regex feature | Enable `regex` feature |

---

## Debug Mode

For detailed debugging, set environment variable:

```bash
RUST_LOG=debug synta-codegen schema.asn1 -o output.rs
```

This provides verbose output during parsing and generation.