Commit graph

49 commits

Author SHA1 Message Date
Jonas Schäfer
6440209f95 xso: reject duplicate children
This was an oversight. Even though we apparently don't have tests for
this anywhere, it is what the old functional macros do.
2024-08-03 11:58:19 +00:00
Jonas Schäfer
2b346c4e87 parsers: port more things to derive macros 2024-08-03 13:05:37 +02:00
Jonas Schäfer
93ba2797be xso-proc: implement support for collections of children 2024-08-03 12:20:04 +02:00
Jonas Schäfer
5c3ed1435f parsers: delete now-unused generate_element_enum macro 2024-08-03 12:19:42 +02:00
Jonas Schäfer
c028c3b91a xso: implement exhaustive enums
These more closely mirror how enums work currently with the macros.
Non-exhaustive enums may be useful though and kind of were the natural
thing to implement.
2024-08-03 12:19:28 +02:00
Jonas Schäfer
4845715add xso: implement support for enums 2024-08-01 15:28:22 +02:00
Jonas Schäfer
507a3df652 xso-proc: remove Span information from generated type names
By removing that, the lint won't trigger for identifiers with trailing
underscores (which become then embedded underscores which normally trips
the `non_camel_case_types` lint).
2024-08-01 12:53:20 +00:00
Jonas Schäfer
3089832090 xso-proc: remove stripping of trailing _ from type names
Users can now rename the generated types altogether, which means that we
do not need this anymore to avoid lints.
2024-08-01 12:53:20 +00:00
Jonas Schäfer
c90752aa51 xso: add support for overriding names of generated types
In 1265f4b, we introduced a change which may cause a conflict of type
names when deriving the traits on two different types. While a
workaround existed (use `mod`s to isolate the implementation), that is
ugly.

This commit allows overriding the choice of type names.
2024-08-01 12:53:20 +00:00
Jonas Schäfer
819a869ffb parsers: replace some more generate_element! calls with derive macros 2024-07-27 09:39:13 +02:00
Jonas Schäfer
cd9f2033f3 xso: add support for boxed children
This allows building recursive tree structures.
2024-07-27 08:24:25 +02:00
Jonas Schäfer
01336802b4 xso-proc: add support for optional children 2024-07-27 08:24:25 +02:00
Jonas Schäfer
5d284bbd3a parsers: port some elements with children to derive macros 2024-07-26 22:31:42 +02:00
Jonas Schäfer
5bd36eccfc xso-proc: add support for child elements 2024-07-26 22:31:42 +02:00
Emmanuel Gil Peyrot
2eb6ca2f94 xmpp-parsers: Remove util::text_node_codecs
This codec system was useful for the previous parser style, but we have
converted them all to xso now!
2024-07-25 18:42:18 +02:00
Emmanuel Gil Peyrot
7b66de1166 xmpp-parsers: Stop reexporting extra symbols
Let’s continue reexporting jid and minidom, but not their inner pub
items, users of this crate can go one level deeper if they need that.

Only xso::error::Error is still useful to reexport, as this is part of
the public API of all of our parsers.
2024-07-25 14:36:09 +00:00
Jonas Schäfer
ccf38cdf9b Port everything over to AsXml 2024-07-24 16:05:06 +02:00
Jonas Schäfer
1367764f85 parsers: use Base64 codec to derive more things 2024-07-02 06:56:30 +02:00
Jonas Schäfer
c83ff286e0 xso-proc: add support for text codecs
Text codecs allow to customize the conversion of data from/to XML,
in particular in two scenarios:

1. When the type for which the behaviour is to be defined comes from a
   foreign crate, preventing the implementation of
   FromXmlText/IntoXmlText.

2. When there is not one obvious, or more than one sensible, way to
   convert a value to XML text and back.
2024-07-02 06:56:30 +02:00
Jonas Schäfer
ae30221e3c xso: only fail on non-whitespace unknown text
We hereby ignore whitespace-only unexpected text, because that's
generally harmless.
2024-07-01 08:01:42 +02:00
Jonas Schäfer
1e3c075fbf parsers: remove now-obsolete JidCodec 2024-07-01 08:01:42 +02:00
Jonas Schäfer
b0803f831b xso-proc: add support for parsing text content 2024-07-01 08:01:42 +02:00
Jonas Schäfer
0e4865006c parsers: port more elements to derive macros 2024-06-30 13:14:06 +02:00
Jonas Schäfer
0c57be3e61 xso-proc: add support for defaulting in attribute parsing 2024-06-30 13:05:37 +02:00
Jonas Schäfer
4e9c4883a3 parsers: add xso text trait implementations to types
This allows them to be used as attribute in xso-proc derived structs.
2024-06-30 13:05:15 +02:00
Jonas Schäfer
c0fc7f49cf xso-proc: add support for non-String typed attributes 2024-06-30 13:01:36 +02:00
Jonas Schäfer
6ade419030 xso-proc: improve combinatorial test coverage for attribute fields
This is to avoid another regression slip through the test suite, as had
happened in d4d520e (fixed in this commit's parent).
2024-06-25 13:55:07 +00:00
Jonas Schäfer
c0d109d9be xso-proc: fix renaming attributes using a static item
This was broken by d4d520e by accident, and of course we didn't have
tests for that :-).

(we do, now)
2024-06-25 13:55:07 +00:00
Jonas Schäfer
d4d520e1f6 xso-proc: add support for built-in prefixes in attribute names
This simplifies the use of built-in XML attributes such as xml:lang.
2024-06-24 12:39:32 +02:00
Jonas Schäfer
84de7fc248 xso-proc: add support for namespaced attributes 2024-06-24 12:39:32 +02:00
Jonas Schäfer
219d682295 xso-proc: add support for renaming attributes
This is akin to `#[serde(rename = ..)]` and thus useful.
2024-06-24 11:33:10 +02:00
Jonas Schäfer
212c5c4a83 xso-proc: add support for parsing attributes into Strings
This is bare-bones and is missing many features which we intend to add
in future commits, such as parsing from attributes whose names differ
from the field names and parsing into non-String types.
2024-06-24 11:33:10 +02:00
Jonas Schäfer
183bef5cf6 xso-proc: completely overengineer everything for no good reason!
Well, not really, of course. All of this will make sense once we start
adding support for fields and non-struct types. Refactoring the code now
before we start to add actual member field parsing is much easier.

How do I know that this will work out? Well, my crystal ball knows it.
Don't believe me? Okay, ChatGPT told me ... Alright alright, I went
through the entire process of implementing this feature *twice* at this
point and have a pretty good idea of where to draw the abstraction lines
so that everything falls neatly into place. You'll have to trust me on
this one.

(Or, you know, check out old branches in my xmpp-rs repo. That might
work, too. `feature/derive-macro-streaming-full` might be a name to look
for if you dare.)
2024-06-23 18:43:34 +02:00
Jonas Schäfer
2efef5ceeb xso-proc: add support for string literals for namespaces
Makes it easier to use ad-hoc.
2024-06-23 12:40:18 +02:00
Jonas Schäfer
4d1166b66d xso-proc: allow paths as XML names
Not sure if this is something useful to have, but it feels consistent
with `namespace`.
2024-06-23 12:40:18 +02:00
Jonas Schäfer
bc785fde28 parsers: replace generate_empty_element with derive 2024-06-23 12:20:02 +02:00
Jonas Schäfer
0adfd1218b xso-proc: start making derive macros for FromXml and IntoXml
For now, these macros only support empty elements. Everything else will
be rejected with a compile-time error.
2024-06-23 12:20:02 +02:00
Jonas Schäfer
6ef8dbefa3 parsers: use Error type from xso
This is a large change and as such, it needs good motivation. Let me
remind you of the ultimate goal: we want a derive macro which allows us
to FromXml/IntoXml, and that derive macro should be usable from
`xmpp_parsers` and other crates.

For that, any code generated by the derive macro mustn't depend on any
code in the `xmpp_parsers` crate, because you cannot name the crate you
are in portably (`xmpp_parsers::..` wouldn't resolve within
`xmpp_parsers`, and `crate::..` would point at other crates if the macro
was used in other crates).

We also want to interoperate with code already implementing
`TryFrom<Element>` and `Into<Element>` on structs. This ultimately
requires that we have an error type which is shared by the two
implementations and that error type must be declared in the `xso` crate
to be usable by the macros.

Thus, we port the error type over to use the type declared in `xso`.

This changes the structure of the error type greatly; I do not think
that `xso` should have to know about all the different types we are
parsing there and they don't deserve special treatment. Wrapping them in
a `Box<dyn ..>` seems more appropriate.
2024-06-23 09:40:52 +02:00
Jonas Schäfer
cb3da52ba2 parsers: add streamable parsing
This adds shims which provide FromXml and IntoXml implementations to
*all* macro-generated types in `xmpp_parsers`. Mind that this does not
cover all types in `xmpp_parsers`, but a good share of them.

This is another first step toward real, fully streamed parsing.
2024-06-18 16:54:11 +02:00
Jonas Schäfer
2f7d5edb8a Make TryFrom<Element> chainable
This allows constructs like:

```rust
let residual = match Iq::try_from(stanza) {
  Ok(iq) => return handle_iq(..),
  Err(Error::TypeMismatch(_, _, v)) => v,
  Err(other) => return handle_parse_error(..),
};
let residual = match Message::try_from(stanza) {
  ..
};
let residual = ..
log::warn!("unhandled object: {:?}", residual);
```

The interesting part of this is that this could be used in a loop over a
Vec<Box<dyn FnMut(Element) -> ControlFlow<SomeResult, Element>>, i.e. in
a parsing loop for a generic XML/XMPP stream.

The advantage is that the stanza.is() check runs only once (in
check_self!) and doesn't need to be duplicated outside, and it reduces
the use of magic strings.
2024-03-03 14:50:29 +00:00
Jonas Schäfer
3b3a4ff0c8 Do not .clone() Element in code generated with generate_element
This should improve performance a little.
2024-03-03 15:05:11 +01:00
Werner Kroneman
18cb6f6e2d Refactored the "helpers" so that they use a common Codec trait; this makes them composable as well. 2024-01-10 19:49:53 +00:00
Emmanuel Gil Peyrot
3f90e84c5b xmpp-parsers: Add a hexadecimal codec
That one accepts both uppercase and lowercase hexadecimal input, and
outputs in lowercase.

It requires no separator between bytes, unlike ColonSeparatedHex.
2024-01-05 17:36:43 +01:00
b522eaf7f3 parsers: clippy pass
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2023-12-15 19:26:06 +00:00
Emmanuel Gil Peyrot
4460a59bc8 xmpp-parsers: XEP-0257: Correctly serialise no-cert-management
This element was previously always put in the generated element.
2023-09-05 17:08:30 +02:00
Emmanuel Gil Peyrot
022a920300 xmpp-parsers: Update to the new jid crate 2023-06-20 18:59:26 +02:00
Emmanuel Gil Peyrot
2955a0fe60 parsers: Bump base64
Version 0.21 replaced base64::decode() with an Engine trait and multiple
structs implementing it for various alphabets, various performance
profiles, etc.  It is slightly longer to import but in the end does the
very same thing.
2023-04-03 11:28:41 +02:00
Emmanuel Gil Peyrot
9e9f484bd9 xmpp-parsers: Simplify trailing comma handling in macros
Since Rust 1.32.0 (so basically forever ago) we can use the $(…)?
construct in macros to mean one or zero times this chunk of tokens.
This allows making the last comma optional in lists of things.
2022-03-22 15:59:00 +01:00
Emmanuel Gil Peyrot
9410849d7a Rename the xmpp-parsers directory to parsers
This doesn’t change anything to the name of the crate, just makes
autocompletion easier by not sharing the same prefix as the xmpp crate.
2021-10-11 08:24:16 +02:00