6 releases (breaking)
0.5.1 | Oct 2, 2024 |
---|---|
0.5.0 | Sep 10, 2024 |
0.4.0 | Jun 5, 2024 |
0.3.0 | Jun 3, 2024 |
0.1.0 | Jun 2, 2024 |
#1263 in Procedural macros
230 downloads per month
Used in nxml-rs
18KB
257 lines
nxml-rs
nxml-rs is a Rust rewrite of NXML.
NXML is a pseudo-XML parser that can read and write the oddly formatted and often invalid XML files from Noita. NXML (the C#/.NET version linked before) is itself a port of the XML parser from Poro, which is used by the custom Falling Everything engine on which Noita runs.
In short, nxml-rs is an XML parser that is 100% equivalent to Noita's parser. It is just as non-conformant to the XML specification as Noita itself. It can also produce semantically equivalent output in the form of a string.
Additionally (just for fun), nxml-rs provides an nxml!
macro that allows you
to create nxml elements by writing almost-XML (text content requires some extra
characters and is generally poorly supported and not really used by Noita
anyway) directly in your Rust code.
Because it's Rust and thus it was relatively easy to do, the parsing is almost
zero-copy - a single exception being non-continuous bare text,
<a>hello<b/>world</a>
, which is non-existingly rare and is a preserved quirk
of how noita parser handles that anyway, handled with Cow
.
Example
use nxml_rs::*;
let mut entity = nxml_rs::parse(r#"
<Entity>
<LuaComponent
script_source_file="mods/blah/etc/test.lua"
execute_every_n_frame="-1">
</LuaComponent>
<TestComponent blah="blah" />
</Entity>
"#).unwrap();
// A little DSL sugar thing, / is alias for .child("name").unwrap(),
// and % is for .attr("name").unwrap()
assert_eq!("-1", &entity / "LuaComponent" % "execute_every_n_frame");
let a_lot = 0;
let speed = 0;
// Make a new element with builder methods
let extra = Element::new("ElectricityComponent")
.with_attr("energy", a_lot)
.with_attr("probability_to_heat", "0")
.with_attr("speed", speed);
// But it's more convenient to use the macro, the builder is rarely ever
// needed (and the following macro expands to above code)
let extra = nxml! {
<ElectricityComponent energy={a_lot} probability_to_heat="0" {speed} />
};
// Clone everything into owned strings, making it a bit nicer to work with
let mut owned = entity.to_owned();
// A modification - entity.children is just a vec ¯\_(ツ)_/¯
owned.children.insert(1, extra);
// Still has the sugar
assert_eq!("0", &owned / "ElectricityComponent" % "probability_to_heat");
// And can be rendered back into a string (.display() making it pretty-printed)
assert_eq!(owned.display().to_string(), r#"
<Entity>
<LuaComponent script_source_file="mods/blah/etc/test.lua" execute_every_n_frame="-1"/>
<ElectricityComponent energy="0" probability_to_heat="0" speed="0"/>
<TestComponent blah="blah"/>
</Entity>
"#.trim());
// DSL is defined for both of them, and / works with &mut
(&mut owned / "LuaComponent").remove_attr("execute_every_n_frame");
(&mut entity / "LuaComponent").remove_attr("execute_every_n_frame");
entity.children.remove(1);
// The EntityRef can be rendered too
assert_eq!(entity.to_string(), "<Entity><LuaComponent script_source_file=\"mods/blah/etc/test.lua\"/></Entity>");
Cargo features
indexmap
- UseIndexMap
from theindexmap
crate instead ofHashMap
for attributes. This is needed to preserve the order of attributes in the parsed elements. Enabled by default.compact_str
- UseCompactString
from thecompact_str
crate instead of regular RustString
s in the owned element. The short string optimization makes total sense here as most element and attribute names and values will fit into the inlined buffer, drastically reducing the number of small allocations and indirections. Enabled by default.
Dependencies
~260–710KB
~17K SLoC