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
.- Binary
Operation left operator right
.- Binding
Inherit inherit from attributes[0] attributes[1] ...;
- Binding
KeyValue from = to;
.- Error
- An Error, with message and positions.
- Float
1.23
.- Function
head: body
.- Function
Application function argument[0] argument[1] ...
.- Function
Head Destructured {arguments, ...} @ identifier
.- Function
Head Destructured Argument identifier ? default
.- Function
Head Simple identifier
.- HasAttribute
expression ? attribute_path
.- Identifier
identifier
.- IfThen
Else if predicate then then else else_
.- Indented
String ''parts''
.- Integer
123
.- LetIn
let bindings in target
.- List
[element[0] element[1] ...]
.- Map
- (
rec
){ bindings }
. - Parsed
- The result of parse() and parse_bytes().
- Part
Expression ${expression}
.- Part
Interpolation "${expression}"
.- PartRaw
"content"
.- Path
/path/to/file
.- Position
- Line and column, starting at 1.
- Property
Access expression.attribute_path
.- Search
NixPath <nixpkgs>
.- Span
- Start and end position of an element.
- String_
"parts"
.- Trivia
Boundary - Trivia
Comment - Trivia
Multiline Comment - Trivia
Whitespace - Unary
Operation operator operand
.- Uri
https://example.com
(deprecated).- With
with expression; target
.
Enums§
- Binary
Operator Addition
,Concatenation
, …- Binding
KeyValue
andInherit
.- Expression
Assert
,BinaryOperation
, …- Function
Head Destructured
(a @ {b, c, ...}
) orSimple
(arg:
).- Function
Head Destructured Identifier None
,LeftAt
(identifier @
),RightAt
(@ identifier
)- Part
Expression
,Interpolation
, orRaw
.- Trivia
Comment
,MultilineComment
, andWhitespace
.- Unary
Operator Not
andNegate
.
Functions§
- parse
- Parse the
input
String. - parse_
bytes - Parse the
input
bytes (Vec<u8>).