Skip to main content

Module parser

Module parser 

Source
Expand description

.env file parser — merged implementation.

Combines the correctness of the original parser (typed errors, key validation, circular-expansion detection, depth limiting) with the feature set of the refactored parser (backtick quotes, multiline values, bare $VAR expansion, inline-comment stripping, configurable value trimming, and a proper Default impl).

§Format support

FeatureSupported
KEY=value
export KEY=value
# comments
Inline # comments✓ (opt-in via ParserConfig::allow_inline_comments)
Double-quoted values
Single-quoted values
Backtick-quoted values
Multiline values✓ (opt-in via ParserConfig::allow_multiline)
${VAR} expansion
$VAR expansion
Circular expansion guard
Strict uppercase keys✓ (opt-in via ParserConfig::strict)

§Compatibility with other modules

Every call site in the codebase uses one of these three patterns:

// Pattern A — most common (all commands)
let parser = Parser::default();
let env_file = parser.parse_file(".env")?;
env_file.vars  // HashMap<String, String>

// Pattern B — config override (validate --strict, tests)
let parser = Parser::new(ParserConfig { strict: true, ..Default::default() });

// Pattern C — parse from string (tests, template command)
let vars = parser.parse_content("KEY=value")?;

This merged parser satisfies all three patterns without any call-site changes. See § Compatibility notes below for per-module details.

§Error handling

All errors are ParseError variants. Because the commands wrap parser calls with anyhow::Context (.with_context(|| ...)), the structured error converts automatically into anyhow::Error at the boundary — no changes needed in any command file.

§Compatibility notes by module

ModuleMethod usedBreaking change?
commands/validate.rsparse_file(&str)None — signature preserved
commands/diff.rsparse_file(&str)None
commands/scan.rsparse_file(&str)None
commands/convert.rsparse_file(&str)None
commands/sync.rsparse_file(&str)None
commands/template.rsparse_file(&str)None
commands/migrate.rsparse_file(&str)None
commands/backup.rsNot used directlyN/A
commands/doctor.rsparse_file(&str)None
commands/init.rsparse_file(&str)None
core/converter.rsEnvFile.varsNone — field name preserved
Testsparse_contentNone — method name preserved

Structs§

EnvFile
The result of parsing a .env file or string.
Parser
.env file parser.
ParserConfig
Controls parser behaviour. Construct with Default::default() and override individual fields as needed.

Enums§

ParseError
Structured parse errors with line numbers and context.

Type Aliases§

ParseResult
Convenience alias used throughout the parser internals.