Skip to content

Conversation

@alpha-tango-kilo
Copy link
Contributor

@alpha-tango-kilo alpha-tango-kilo commented Apr 27, 2024

These two types in particular can be a bit finnicky to build by hand, so I've made some declarative macros for the job based on the popular crate maplit

They pre-allocate the correct capacity ahead of time thanks to macro counting magics 🧙🏼‍♂️ (For this, I added Dictionary::with_capacity, copying the documentation from the internal IndexMap)

I could always make a separate crate for these if you didn't want them in the main repo, but I figured they're a useful addition. Happy to bikeshed the macro names, I just picked something so I could make the code work and debate later :)

As an aside, please consider adjusting workflow permissions for a better contributor experience: https://matklad.github.io/2022/10/24/actions-permissions.html

Implementation & tests largely based on maplit (compatible license)
Add example of repetition
Shorten first example
RickyDaMa added a commit to daltonmaag/glyphs-plist that referenced this pull request Apr 29, 2024
alpha-tango-kilo added a commit to alpha-tango-kilo/plist-pls that referenced this pull request May 20, 2024
@alpha-tango-kilo
Copy link
Contributor Author

Hey @ebarnard, can see you've been active on the repo again recently. Anything you need from me on this PR or my other one?

@ebarnard
Copy link
Owner

ebarnard commented Jul 1, 2024

#143 is looking to add a plist! macro like the json! macro from serde_json. I feel like the dictionary syntax should be the same for both.

Also is it possible to use macros by path, e.g. plist::array!?

@aluedeke
Copy link

aluedeke commented Jul 2, 2024

isn't this more or less redundant as soon as #143 is ready? some examples for using plist!:

Dictionary:

let dict = plist!({
     "a": 1,
     "b": 2,
});

Array:

let array = plist!([1, 2, 3]);

@alpha-tango-kilo
Copy link
Contributor Author

In some cases it's nice to have a Dictionary or Array not wrapped as a Value, but otherwise yes the plist! macro is a superset of what I was looking to add here. I don't mind if the PR is ultimately deemed redundant and closed on that basis. Else, I'll see about addressing ebarnard's questions & suggestions when I get the time

@alpha-tango-kilo
Copy link
Contributor Author

I feel like the dictionary syntax should be the same for both

Tried to see if I could make this work, but the trivial change from => to : leads to a compile error:

error: `$key:expr` is followed by `:`, which is not allowed for `expr` fragments
  --> src\macros.rs:20:18
   |
20 |     ($($key:expr : $value:expr,)+) => { $crate::plist_dict!($($key => $value),+) };
   |                  ^ not allowed after `expr` fragments
   |
   = note: allowed there are: `=>`, `,` or `;`

I'm not a dtolnay level macro wizard so am not really up to the task of rewriting this as a tt muncher. Maybe a plist_dict! would be possible to build on top of #143 by calling into plist_internal!. You could probably implement plist_array! from this PR in the same way

I guess I've basically talked myself into having this PR closed in favour of #143 and maybe having these two more specialised macros added to that PR or built using it later

Is it possible to use macros by path?

Doesn't look like it, no. Check out this experiment I made in Rust Playground: link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants