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
assert expression; target.
BinaryOperation
left operator right.
BindingInherit
inherit from attributes[0] attributes[1] ...;
BindingKeyValue
from = to;.
Error
An Error, with message and positions.
Float
1.23.
Function
head: body.
FunctionApplication
function argument[0] argument[1] ....
FunctionHeadDestructured
{arguments, ...} @ identifier.
FunctionHeadDestructuredArgument
identifier ? default.
FunctionHeadSimple
identifier.
HasAttribute
expression ? attribute_path.
Identifier
identifier.
IfThenElse
if predicate then then else else_.
IndentedString
''parts''.
Integer
123.
LetIn
let bindings in target.
List
[element[0] element[1] ...].
Map
(rec) { bindings }.
Parsed
The result of parse() and parse_bytes().
PartExpression
${expression}.
PartInterpolation
"${expression}".
PartRaw
"content".
Path
/path/to/file.
Position
Line and column, starting at 1.
PropertyAccess
expression.attribute_path.
SearchNixPath
<nixpkgs>.
Span
Start and end position of an element.
String_
"parts".
TriviaBoundary
TriviaComment
TriviaMultilineComment
TriviaWhitespace
UnaryOperation
operator operand.
Uri
https://example.com (deprecated).
With
with expression; target.

Enums§

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

Functions§

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