Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Table -> InlineTable #74

Closed
gabbifish opened this issue Dec 7, 2019 · 4 comments · Fixed by #199
Closed

[feature] Table -> InlineTable #74

gabbifish opened this issue Dec 7, 2019 · 4 comments · Fixed by #199
Labels
C-enhancement Category: Raise on the bar on expectations

Comments

@gabbifish
Copy link

gabbifish commented Dec 7, 2019

Hello! I'm trying to turn

[site]
bucket = ""
entry-point = "workers-site"

into an inline table using toml_edit. Oddly enough, when I parse my toml, I get

Table(Table { items: {"bucket": TableKeyValue { key: Repr { decor: Decor { prefix: "", suffix: " " }, raw_value: "bucket" }, value: Value(String(Formatted { value: "", repr: Repr { decor: Decor { prefix: " ", suffix: "" }, raw_value: "\"\"" } })) }, "entry-point": TableKeyValue { key: Repr { decor: Decor { prefix: "", suffix: " " }, raw_value: "entry-point" }, value: Value(String(Formatted { value: "workers-site", repr: Repr { decor: Decor { prefix: " ", suffix: "" }, raw_value: "\"workers-site\"" } })) }}, decor: Decor { prefix: "\n", suffix: "" }, implicit: false, position: Some(1) })

Type Table(Table... was not I expected to see above, and I suspect it's why calling .as_inline_table() fails on the table provided above. (instead, calling .as_inline_table() returns None).

Any pieces of advice? :)

@ordian
Copy link
Member

ordian commented Dec 7, 2019

hey @gabbifish! This is the expected output. Inline tables are something different: https://github.com/toml-lang/toml#user-content-inline-table.
For example,

site = { bucket = "", entry-point = "workers-site" }

This would be an equivalent inline table in your case. Does that make sense?

@ordian
Copy link
Member

ordian commented Dec 7, 2019

Ah, I see you're trying to convert a Table into an InlineTable and expecting as_inline_table to do the conversion. But that's not what it does. It performs a dynamic check and returns Some(InlineTable) if it's actually is an InlineTable.
Currently, the functionality you asking is missing: https://github.com/ordian/toml_edit/blob/b679b5e610b9a19f48fa0268f9174062e9c43962/src/table.rs#L8

PRs are welcome :)

@ordian ordian added C-enhancement Category: Raise on the bar on expectations help wanted labels Dec 7, 2019
@ordian ordian changed the title Table parsing seems to fail [feature] Table -> InlineTable Dec 7, 2019
@pshirshov
Copy link

Here is my snippet which converts Table into InlineTable:

fn make_inline_tbls(paths_arr: &mut ArrayOfTables) {
    for x in (0..paths_arr.len()) {
        let sub = paths_arr.get_mut(x).expect("");
        let tgt_raw = sub.entry("target");

        tgt_raw.as_inline_table_mut().map(|e| e.fmt());
        let tgt_table = tgt_raw.as_table().expect("");
        let mut tgt_inline = InlineTable::default();
        tgt_table.iter().for_each(|i| {
            let key = i.0;
            let value = i.1.as_value().expect("").to_owned();
            tgt_inline.get_or_insert(key, value);
        });
        tgt_inline.fmt();


        let mut tgt_val = Value::InlineTable(tgt_inline);
        let tgt_item = Item::Value(tgt_val);
        *tgt_raw = tgt_item;

    }
}

I'm not a rustacean, so, probably, this is a piece of crap, though it works. Would really like to see a built-in API.

@epage
Copy link
Member

epage commented Sep 12, 2021

The next release includes a item.into_value() which will turn a table into an inline table. We don't have an in-place version yet, so you'll have to remove, convert, and insert.

epage added a commit to epage/toml_edit that referenced this issue Sep 12, 2021
This also made it trivial to optimize the conversion of Items to Values
by not re-allocating the underlying containers.

Fixes toml-rs#74
epage added a commit that referenced this issue Sep 12, 2021
This also made it trivial to optimize the conversion of Items to Values
by not re-allocating the underlying containers.

Fixes #74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Raise on the bar on expectations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants