Crate nixel

source ·
Expand description

NixEl is a Rust library that turns Nix code into a correct, typed data-structured.

Just use the parse() or parse_bytes() methods to build a Parsed structure and use the Expression enum variants to traverse the AST.

You can also retrieve Trivia::Comments, Trivia::MultilineComments and Trivia::Whitespace, using the Parsed::trivia_before() and Parsed::trivia_after() methods.

For example:

let input: String = String::from(
    r#"
        # Greet the user
        "Hello, World!"
        # Bye!
    "#,
);

let parsed: nixel::Parsed = nixel::parse(input);

match &*parsed.expression {
    nixel::Expression::String(string) => {
        assert_eq!(
            &string.span,
            &nixel::Span {
                start: nixel::Position { line: 3, column: 9 }.into(),
                end: nixel::Position { line: 3, column: 24 }.into(),
            }
            .into()
        );
        assert_eq!(
            &parsed.trivia_before(&string.span.start)[1],
            &nixel::Trivia::Comment(nixel::TriviaComment {
                content: "# Greet the user".into(),
                span: nixel::Span {
                    start: nixel::Position { line: 2, column: 9 }.into(),
                    end: nixel::Position { line: 2, column: 25 }.into(),
                }
                .into()
            })
        );
        assert_eq!(
            &string.parts[0],
            &nixel::Part::Raw(nixel::PartRaw {
                content: "Hello, World!".into(),
                span: nixel::Span {
                    start: nixel::Position { line: 3, column: 10 }.into(),
                    end: nixel::Position { line: 3, column: 23 }.into(),
                }
                .into()
            })
        );
        assert_eq!(
            &parsed.trivia_after(&string.span.end)[1],
            &nixel::Trivia::Comment(nixel::TriviaComment {
                content: "# Bye!".into(),
                span: nixel::Span {
                    start: nixel::Position { line: 4, column: 9 }.into(),
                    end: nixel::Position { line: 4, column: 15 }.into(),
                }
                .into()
            })
        );
    },
    expression => unreachable!("Expected a String, got: {expression:#?}"),
}

Source code can be found at github.com/kamadorueda/nixel.

Structs

assert expression; target.
left operator right.
inherit from attributes[0] attributes[1] ...;
from = to;.
An Error, with message and positions.
1.23.
head: body.
function argument[0] argument[1] ....
{arguments, ...} @ identifier.
expression ? attribute_path.
identifier.
if predicate then then else else_.
''parts''.
let bindings in target.
[element[0] element[1] ...].
(rec) { bindings }.
The result of parse() and parse_bytes().
${expression}.
"${expression}".
"content".
/path/to/file.
Line and column, starting at 1.
expression.attribute_path.
<nixpkgs>.
Start and end position of an element.
"parts".
operator operand.
https://example.com (deprecated).
with expression; target.

Enums

Addition, Concatenation, …
KeyValue and Inherit.
Assert, BinaryOperation, …
Destructured (a @ {b, c, ...}) or Simple (arg:).
None, LeftAt (identifier @), RightAt (@ identifier)
Expression, Interpolation, or Raw.
Comment, MultilineComment, and Whitespace.
Not and Negate.

Functions

Parse the input String.
Parse the input bytes (Vec<u8>).