squirrelchat/smol-toml

Is it possible to specify style?

Opened this issue · 5 comments

Eg, specify that a field should be a multiline string, or an inline dict.

It isn't possible to specify anything related to style at this time when stringifying at this time unfortunately. I don't think it'd be possible to use some sort of hacky trick to get it to work, at least without post-processing outside of the lib...

It would be great to have options about the output style, I definitely agree and it'd be a great addition to the lib! I'm just a bit unsure on how styles regarding how to format a given field could be passed. Since smol-toml works with plain js objects it'd have to be passed as an option of the stringify function and I can't think of anything that wouldn't be a bit of a pain to use... If you have any ideas feel free to share!

Hm, part of me finds it a bit sad to be re-creating the object and not using "plain objects" as it makes the whole thing much less transparent 🤔

Ideally I wanted smol-toml to have a way to keep the document's format by using extra metadata in a Symbol key; but the problem is that adding new properties makes it difficult to update metadata at the same time... and I'd love to avoid having to resort to special objects like { type: 'multiline-string', value: '...' } that'd break the transparent toml -> js object -> toml conversion chain 😔

The other idea I had was to let stringify accept a schema-like thing that'd define how things are stringified, something like:

stringify(obj, {
  keysStyle: {
    myMultilineString: { multiline: true },
    table: {
      otherMultilineString: { multiline: true, literal: true },
      dotted: { dotted: true },
    },
  },
})

But I have no idea if this is good or bad 🤔

Yes, agreed, it isn't too clean, and a schema like approach would be pretty nice.

Maybe also it should accept deep key access (eg 'data.name.bio': {multiline: true}).

I'd go with the simpler the better - a single option, prefer-multiline-table (or similar), on a configuration object passed to stringify.