Commit graph

2,529 commits

Author SHA1 Message Date
Jonas Schäfer
8c7b503f12 xso: reorganise the code a little
The main lib.rs is getting a bit cluttered, so I'm trying to bring some
order into the chaos by moving some things into other modules.

skip-changelog, because there are no user-facing changes (names which
have been moved are doc(inline)'d and pub use'd in the main lib, and
other things (trait implementations) aren't addressable by users).
2025-05-06 07:42:57 +02:00
Jonas Schäfer
4c47946b81 xso-proc: update inline comment with resolution from upstream bug
Reminder: when something feels too good to be true, it likely is.

skip-changelog, because this is no user-facing change.
2025-05-03 17:57:15 +02:00
Jonas Schäfer
51ce661f8d tokio-xmpp: do not discard XmlLangStack when boxing the stream
The stream may have read some data already (such as the stream
header). When initially writing this code, I had missed that we do
actually also carry the parser state over: I misread the
AsyncReader::wrap(..) command as something like AsyncReader::new, i.e.
starting off with fresh parser state, so I assumed that we need a fresh
XmlLangStack, too.

This is wrong: `p` comes from `self.parser.into_inner()` above and is
the parser state, so we need to carry the lang stack along, as well.

This fixes:

```
thread 'tokio-runtime-worker' panicked at […]/rxml-0.13.1/src/xml_lang.rs:87:13:
pop from empty XmlLangStack
```

happening during stream shutdown. The panic was first reported by
@ppjet6, so thanks for that and the keen eye.

skip-changelog, because the bug has not been released yet.
2025-05-03 17:41:13 +02:00
Link Mauve
7f0a7b2247 xmpp-parsers: Switch to #[xml(lang)] wherever it’s possible
So far this new meta doesn’t work for the BTreeMap<Lang, String>
pattern, which is often used to provide multiple versions of a payload.

skip-changelog: This isn’t a user-facing change.
2025-05-03 10:07:54 +02:00
Link Mauve
3f873d2b42 xmpp-parsers: Remove some unneeded clones 2025-05-03 10:06:59 +02:00
Jonas Schäfer
a222dc70de xso: provide nicer error messages on missing trait implementations 2025-05-02 17:16:59 +02:00
Jonas Schäfer
de39f410d5 xso-proc: improve spans for error messages
For codec-based error messages, this reduces the amount of errors per
violation to one. For all others, it improves the placement of the error
slightly, but we still get duplicates.

I couldn't figure out the remaining discrepancies in the spans ...
2025-05-02 17:16:59 +02:00
Jonas Schäfer
25d89e02ea xso: make convert_via_fromstr_and_display available
Useful for dependent crates which want an easy way to provide the
AsXmlText / FromXmlText traits.
2025-05-02 17:16:59 +02:00
Jonas Schäfer
2a940fb5e0 xso-proc: work around regression in rustc nightly
skip-changelog, this is no user-facing change.

See-Also: https://github.com/rust-lang/rust/issues/140585
2025-05-02 16:29:58 +02:00
Jonas Schäfer
c0262fbafb xso: add support for attribute-switched enums
skip-changelog, because enums have been introduced in the same release,
so this is merely an addition to the already changelogged enum feature.
2025-04-27 18:34:48 +00:00
Jonas Schäfer
f4b981dc78 xso-proc: allow injecting states into the AsItemsSubmachine
This is useful if constant data is to be emitted into the element
header and a prerequisite for attribute-switched enums.
2025-04-27 18:34:48 +00:00
Jonas Schäfer
847032ed35 xso-proc: add support for matching elements using match
The `match` expression will have to be provided by the caller. Use cases
could be to express the `NameSwitchedEnum` more clearly (by switching on
the XML element's name) or introducing new kinds of switched enums.
2025-04-27 18:34:48 +00:00
Maxime “pep” Buquet
4346e4b940 xmpp::muc::room: reverse check for rooms_joined
skip-changelog, fixes bug introduced in the same release

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-27 16:28:02 +02:00
Jonas Schäfer
67f3ffaced xso: re-implement #[xml(lang)] on top of #[xml(attribute)]
This gives us all the goodies of `default`, `type_` and `codec` without
having to duplicate lots of code (and I think the `match`-iness of the
new macro code is still within limits).

However, we still keep them as separate `#[xml(..)]` attributes, because
their semantics are very different and it is sensible to make them stand
out.

skip-changelog, because `#[xml(lang)]` was introduced in this version.
2025-04-27 14:19:31 +02:00
Jonas Schäfer
341d49fecf xso: extend the documentation overall
Sprinkling it with examples, adding more words to make things
(hopefully) clearer, using a similar structure for different items etc.
etc.
2025-04-27 14:08:38 +02:00
Jonas Schäfer
cffb5e7e65 xso: make Context use a builder-like pattern
That way, we can (hopefully) add more fields in the future without
breaking stuff.

I don't yet know what such fields could be, though.
2025-04-27 14:08:38 +02:00
Jonas Schäfer
57972e9db0 xso: make from_bytes available without std
This also deuglifies some of the parsing code by avoiding the
interaction with the I/O errors emitted from rxml::Reader.
2025-04-27 14:08:38 +02:00
Jonas Schäfer
fe0050dbd8 xso: fix build without std 2025-04-27 14:08:38 +02:00
Jonas Schäfer
8f47c9d2ca minidom: fix build without std 2025-04-27 14:08:38 +02:00
Jonas Schäfer
ebe53b8eab xso: remove #[doc(inline)] from TextCodec
Using `#[doc(inline)]` there makes it appear in two places in the
documentation, which may be confusing (as it's not fully obvious that
both places are in fact the same trait).
2025-04-27 14:08:38 +02:00
Jonas Schäfer
67e7e9da15 xso: improve documentation around text traits for third-party types
The previous wording was a bit ominous in places ("Because of the
unfortunate situation as described in `FromXmlText`"). This should be
clearer and provides hopefully clearer instructions.
2025-04-27 14:08:38 +02:00
Jonas Schäfer
8688524e1a xso: fix some documentation in minidom_compat 2025-04-27 13:44:07 +02:00
Jonas Schäfer
a7d556ce6f xso: add useful note
I tried to remove it, fell into that trap. Let's save the next person
some work.
2025-04-27 13:44:06 +02:00
Jonas Schäfer
c4de8725fc xso: deprecate try_from_element
It is not necessary anymore, because we switched from `IntoXml` to
`AsXml`, allowing `transform` to work with a reference instead of
consuming its input.

Before that, `try_from_element` was the only way to fallibly attempt to
parse something from `Element` without having to clone the entire DOM.
2025-04-27 13:44:06 +02:00
Jonas Schäfer
88561269bd xso: fix documentation typos 2025-04-27 13:43:48 +02:00
Jonas Schäfer
9fdb1564f6 xso: reject attempts to match the same XML attribute in different fields
This was a bit tricky to build, because it is possible to have an
indirection through a `static` there. Thanks to Rust's extensive
const-fn capabilities, though, it's in fact possible to cover all cases.

We still do two different checks to improve user experience. If we can,
from within the proc macro, determine that two fields refer to the same
XML attribute (because their namespace/name values use the same Rust
tokens), then we reject the fields with a clear error message pointing
at both fields.

In the other case, when there's e.g. `#[xml(lang)]` and
`#[xml(attribute(namespace = rxml::XMLNS_XML, name = "lang"))]`, the
macro cannot be sure that XMLNS_XML is in fact the XML namespace. For
that case, we generate code which is evaluated at compile time (and
has no runtime impact) which panics if the namespace and name of two
attribute-matching fields is the same.

The error message will be less clear (because it contains extra,
unchangeable wording like "evaluation of constant value failed" and "the
evaluated program panicked at", which may be a bit confusing) than the
message generated by the macros themselves, but it's a price we have to
pay unfortunately.

Note that this check may seem cosmetic and purely for better user
experience, but it is in fact needed to avoid generating not-well-formed
and/or not-namespace-well-formed XML: As `AsXml` generates `xso::Item`,
where each attribute is emitted separated (and not aggregated in a
map structure), a naive (and efficient) implementation of a writer might
not double-check that no duplicate attributes are generated.
2025-04-27 11:18:50 +02:00
Jonas Schäfer
174eea5e5d xso: clarify behaviour when two fields match the same xml attribute 2025-04-23 17:25:18 +02:00
Jonas Schäfer
d9a21dd8c9 Track xml:lang throughout parsing 2025-04-22 17:55:44 +02:00
Jonas Schäfer
31d7bbf622 xso: discard xml:lang implicitly
Because this attribute may occur in random places, it makes no sense
failing on it. We discard it after attribute processing though, so it
can still be captured by structs which are explicitly interested in it.
2025-04-22 17:55:12 +02:00
Jonas Schäfer
70d635b0fb Bump rxml to 0.13.1
rxml 0.13.0 changed the quoting style to make it consistent, so we have
to patch up all affected places where we match exact strings.
2025-04-22 17:47:45 +02:00
Maxime “pep” Buquet
477af3c895 xmpp: new 'escape-hatch' feature
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-20 13:41:56 +00:00
Link Mauve
a8c66bc783 minidom, tokio-xmpp: Use Option::as_deref()
This simplifies the Option::as_ref().map(|foo| &**foo) pattern a lot.

skip-changelog: Completely internal change.
2025-04-19 00:06:13 +02:00
Jonas Schäfer
75e0cb7dc3 parsers: improve xml:lang handling
This is still far from being a full fix. Still, we can avoid the lang
attribute (by allowing discarding the `xml:lang` value via the feature
introduced a couple commits ago) and we can avoid emitting `xml:lang=""`
where no language was previously set using the mechanism introduced for
<message/> in 5172fb5e.
2025-04-18 13:16:21 +02:00
Jonas Schäfer
5172fb5e0f parsers: port Message to use the derive macros 2025-04-18 12:56:03 +02:00
Jonas Schäfer
0fd69d2ff8 xso-proc: fix order-dependent compilation bug
The test case which is added fails to compile unless one puts the
`parent` field before the `id` field. The cause is explained somewhat by
the change, but I'll spell it out here nontheless.

Previously, the loop in `Compound::make_as_item_iter_statemachine`
assumed that the serialisation order of fields would match their
declaration order. That is not generally true: attributes must be
serialised before element content, because they must be emitted before
the element header is closed.

This change thus splits the generated states into "header" states (for
everything before the end of the element header (think `>`)) and
"body" states (for everything after and including the end of the
element header). After all fields have been processed, we can then
add the data fields of the body fields to the header states so that
they are carried through the generated state machine until they are
needed in the body.
2025-04-18 12:41:12 +02:00
Jonas Schäfer
134255d1de parsers: port data_forms to use derive macros 2025-04-18 10:49:26 +02:00
Jonas Schäfer
8e81c41f59 xso: add support for post-deserialization callback 2025-04-18 10:49:26 +02:00
Maxime “pep” Buquet
da219d4fed
xmpp: handle SIGINT (^C) in hello_bot example
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-13 21:16:17 +02:00
Maxime “pep” Buquet
23c947d081 tokio-xmpp: clippy run
skip-changelog

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-08 13:57:21 +02:00
Maxime “pep” Buquet
1b635854ad parsers: clippy run
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-08 13:57:21 +02:00
Maxime “pep” Buquet
4c2fac0ac3 jid: clippy run
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-08 12:47:56 +02:00
Maxime “pep” Buquet
4994ac20f9 xso-proc: clippy run
Caught a bug hidden behind #[cfg(not(feature = "minidom"))]

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-08 12:47:56 +02:00
Maxime “pep” Buquet
5b62672541 xso: clippy run
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-08 12:47:56 +02:00
Maxime “pep” Buquet
2555bedaa8
CI: add .show-version block in docs job
skip-changelog

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-08 12:43:30 +02:00
Maxime “pep” Buquet
a1247a0e3c parsers: Add support for XEP-0335: JSON Containers
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-07 18:35:43 +00:00
Maxime “pep” Buquet
ab7e6de3e1 xso: Add support for serde_json::Value
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-07 18:35:43 +00:00
Maxime “pep” Buquet
4780f89604 xmpp: new Agent::new method
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-04-07 18:26:27 +00:00
Link Mauve
5ae38545e4 xmpp: Shorten the default caps node to our main domain
We do own xmpp.rs, so let’s use that instead of gitlab.com.
2025-04-07 11:57:01 +02:00
Jonas Schäfer
84c4b5731c parsers: port stanza_error over to derive macros 2025-04-06 08:36:15 +02:00
Jonas Schäfer
b05ee0280a xso: add support for selectively discarding text and attributes 2025-04-06 08:36:15 +02:00