synta 0.1.12

ASN.1 parser, decoder, and encoder library with DER/BER support and C FFI
Documentation
# Troubleshooting

Common issues and solutions when using synta-codegen and the synta crates.

## Installation

### `cargo install synta-codegen` fails

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

**Solutions:**

Update your Rust toolchain to the latest stable release:
```bash
rustup update stable
rustc --version
```

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

---

## Parsing errors

### "Expected identifier, got Symbol"

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

Common causes:

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

   -- Right:
   MyType ::= SEQUENCE { ... }
   ```

2. Reserved keyword used as identifier:
   ```asn1
   -- Wrong:
   type ::= INTEGER

   -- Right:
   typeValue ::= INTEGER
   ```

Check the line and column number in the error and verify the surrounding syntax.

### "Unexpected token" in constraints

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

Add proper spacing between the keyword and parenthesis:
```asn1
MyString ::= IA5String (SIZE (1..10))  -- Correct
```

### "Unknown type" errors

```
ParseError: Unknown type 'Foo'
```

Either the type is not defined or not imported:

```asn1
MyModule DEFINITIONS ::= BEGIN
    IMPORTS UserStatus FROM OtherModule;

    User ::= SEQUENCE {
        status  UserStatus
    }
END
```

---

## Code generation issues

### Generated code has naming conflicts

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

The same type name appears in multiple modules without namespacing. Generate each
module to its own file using separate module prefixes:

```bash
synta-codegen module1.asn1 --crate-imports -o src/module1.rs
synta-codegen module2.asn1 --crate-imports -o src/module2.rs
```

### Field names become invalid Rust identifiers

ASN.1 allows identifiers that are Rust keywords. synta-codegen automatically
escapes them with `r#`:

```rust
pub struct MyStruct {
    pub r#type: Integer,  // Escaped with r#
}
```

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

### SEQUENCE OF generates `Vec` instead of an array

ASN.1 SEQUENCE OF is variable-length. A SIZE constraint generates runtime
validation but still produces `Vec<T>`:

```asn1
FixedArray ::= SEQUENCE (SIZE (10)) OF INTEGER
```

This generates `Vec<Integer>` that validates its length at decode/construct time.
Fixed-size arrays (`[T; N]`) are not generated.

---

## Compilation errors

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

Add the synta dependency:

```toml
[dependencies]
synta = "0.1"
```

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

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

Possible causes:

1. Missing derive attribute — check that `#[cfg_attr(feature = "derive", derive(Asn1Sequence))]`
   is present (regenerate with `synta-codegen` if needed).
2. Missing `use synta::{Encode, Decode};` at the top of the file.
3. Hand-written type without an `Encode` impl — implement it manually.

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

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

Regenerate with `--crate-imports` so that import statements are emitted:

```bash
synta-codegen schema.asn1 --crate-imports -o output.rs
```

---

## Runtime issues

### Constraint validation fails unexpectedly

```rust
let port = HttpPort::new(80u16)?;  // Error
```

Check the constraint definition in the ASN.1 schema. If the value is known to be
valid and the check is the bottleneck, use `new_unchecked` for trusted data:

```rust
let port = HttpPort::new_unchecked(80u16);
```

### PATTERN validation not working

PATTERN validation is gated on a `regex` feature in the **consuming crate**,
not in synta.  Add the `regex` and `once_cell` dependencies to your crate:

```toml
[features]
regex = ["dep:regex", "dep:once_cell"]

[dependencies]
regex = { version = "1.10", optional = true }
once_cell = { version = "1.19", optional = true }
```

### CONTAINING validation not working

CONTAINING validation is gated on a `validate_containing` feature in the
**consuming crate**, not in synta.  Declare the feature in your `Cargo.toml`:

```toml
[features]
validate_containing = []
```

### Decoding fails with "unexpected tag"

Common causes:

1. **Wrong encoding mode** — data is BER but you are using DER mode:
   ```rust
   use synta::{Decoder, Encoding};

   // Wrong — DER mode rejects BER-specific encodings:
   let mut decoder = Decoder::new(&ber_data, Encoding::Der);

   // Right — BER mode accepts any BER-legal encoding:
   let mut decoder = Decoder::new(&ber_data, Encoding::Ber);
   ```

2. **Wrong target type** — the decoded bytes represent a different ASN.1 type.

3. **Corrupted input** — verify the data source and transfer integrity.

---

## Performance issues

### Code generation is slow for large schemas

Split the schema into smaller modules and generate them separately:

```bash
synta-codegen base-types.asn1 -o src/base.rs
synta-codegen extensions.asn1 -o src/ext.rs
```

### Generated code compiles slowly

Enable incremental compilation and split large generated files into multiple Rust
modules:

```toml
[profile.dev]
incremental = true
```

### Runtime validation is slow

1. Use `new_unchecked()` for data from trusted sources.
2. Simplify constraint expressions (ranges are faster than large union lists).
3. Cache validated values rather than re-validating on every use.

---

## Quick reference

| 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 verbose output during parsing and generation:

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

---

## Getting help

1. Check the ASN.1 syntax against the limitations documented in
   [asn1/limitations.md]asn1/limitations.md.
2. Reproduce the issue with the smallest possible schema.
3. Report issues at <https://codeberg.org/abbra/synta/issues> — include: ASN.1
   input, command used, error message, Rust version.