nushell/crates/nu_plugin_formats/src/from/ini.rs
Ian Manske 9996e4a1f8
Shrink the size of Expr (#12610)
# Description
Continuing from #12568, this PR further reduces the size of `Expr` from
64 to 40 bytes. It also reduces `Expression` from 128 to 96 bytes and
`Type` from 32 to 24 bytes.

This was accomplished by:
- for `Expr` with multiple fields (e.g., `Expr::Thing(A, B, C)`),
merging the fields into new AST struct types and then boxing this struct
(e.g. `Expr::Thing(Box<ABC>)`).
- replacing `Vec<T>` with `Box<[T]>` in multiple places. `Expr`s and
`Expression`s should rarely be mutated, if at all, so this optimization
makes sense.

By reducing the size of these types, I didn't notice a large performance
improvement (at least compared to #12568). But this PR does reduce the
memory usage of nushell. My config is somewhat light so I only noticed a
difference of 1.4MiB (38.9MiB vs 37.5MiB).

---------

Co-authored-by: Stefan Holderbach <sholderbach@users.noreply.github.com>
2024-04-24 15:46:35 +00:00

106 lines
3.1 KiB
Rust

use crate::FromCmds;
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
use nu_protocol::{
record, Category, Example, LabeledError, Record, ShellError, Signature, Type, Value,
};
pub struct FromIni;
impl SimplePluginCommand for FromIni {
type Plugin = FromCmds;
fn name(&self) -> &str {
"from ini"
}
fn usage(&self) -> &str {
"Parse text as .ini and create table."
}
fn signature(&self) -> Signature {
Signature::build(self.name())
.input_output_types(vec![(Type::String, Type::record())])
.category(Category::Formats)
}
fn examples(&self) -> Vec<Example> {
examples()
}
fn run(
&self,
_plugin: &FromCmds,
_engine: &EngineInterface,
call: &EvaluatedCall,
input: &Value,
) -> Result<Value, LabeledError> {
let span = input.span();
let input_string = input.coerce_str()?;
let head = call.head;
let ini_config: Result<ini::Ini, ini::ParseError> = ini::Ini::load_from_str(&input_string);
match ini_config {
Ok(config) => {
let mut sections = Record::new();
for (section, properties) in config.iter() {
let mut section_record = Record::new();
// section's key value pairs
for (key, value) in properties.iter() {
section_record.push(key, Value::string(value, span));
}
let section_record = Value::record(section_record, span);
// section
match section {
Some(section_name) => {
sections.push(section_name, section_record);
}
None => {
// Section (None) allows for key value pairs without a section
if !properties.is_empty() {
sections.push(String::new(), section_record);
}
}
}
}
// all sections with all its key value pairs
Ok(Value::record(sections, span))
}
Err(err) => Err(ShellError::UnsupportedInput {
msg: format!("Could not load ini: {err}"),
input: "value originates from here".into(),
msg_span: head,
input_span: span,
}
.into()),
}
}
}
pub fn examples() -> Vec<Example<'static>> {
vec![Example {
example: "'[foo]
a=1
b=2' | from ini",
description: "Converts ini formatted string to record",
result: Some(Value::test_record(record! {
"foo" => Value::test_record(record! {
"a" => Value::test_string("1"),
"b" => Value::test_string("2"),
}),
})),
}]
}
#[test]
fn test_examples() -> Result<(), nu_protocol::ShellError> {
use nu_plugin_test_support::PluginTest;
PluginTest::new("formats", crate::FromCmds.into())?.test_command_examples(&FromIni)
}