Commit graph xmpp-rs/xso/src
Author SHA1 Message Date
Jonas Schäfer
bdae845b14 xso: relax the feature = "std" requirement for xso::dynxso 2025-07-13 11:10:51 +00:00
Jonas Schäfer
345209bce9 xso: add specialized Xso<dyn Trait> container 2025-07-13 11:10:51 +00:00
Jonas Schäfer
7f5542e8d4 xso: add support for dynamically typed XSOs 2025-07-13 11:10:51 +00:00
Jonas Schäfer
126588b47d xso: allow FromXml implementations to provide matching hints
That way, callers can put multiple candidate implementations in, for
example, a sorted vector and more efficiently select candidates to try
when looking at a new element.
2025-07-13 11:10:51 +00:00
Jonas Schäfer
d7b62488dc xso: add type-erased and dyn-compatible variant of AsXml
That way, we don't need to know the specific type of iterator or even
iteree anymore. This can turn out useful when working with `Box<dyn _>`,
that is, in contexts where we don't know the (possible or actual) types
at compile time.
2025-07-13 11:10:51 +00:00
Jonas Schäfer
ee9ffb2d9c xso: make FromEventsBuilder implementations around Box more versatile
By removing the implicit `Sized` bound, they can be used with `dyn _`.
The `Sized` bound is unnecessary for a boxed value, so it is a
superfluous restriction. Also, the use with `dyn _` may turn out useful
in the future when we want to allow type-erased XSOs.
2025-07-13 11:10:51 +00:00
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
a222dc70de xso: provide nicer error messages on missing trait implementations 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
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
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
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
8e81c41f59 xso: add support for post-deserialization callback 2025-04-18 10:49:26 +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
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
Jonas Schäfer
b05ee0280a xso: add support for selectively discarding text and attributes 2025-04-06 08:36:15 +02:00
Maxime “pep” Buquet
b89d3ab04d xso: Add PrintRawXml helper struct with Display
Add a helper struct to be able to display Raw Xml, useful for debug
logs.

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
2025-02-12 20:17:41 +01:00
Emmanuel Gil Peyrot
356ad0d46e xso: Fix all failing clippy tests
They are all sensible to me.

skip-changelog: Internal changes.
2025-02-12 19:11:32 +00:00
Emmanuel Gil Peyrot
1823afbc71 xso-proc: Add the default flag to the element meta
This allows the payload to be absent, and requires the field type to be
Option<minidom::Element>.
2025-01-26 16:40:05 +00:00
Emmanuel Gil Peyrot
c72cf3ddd1 xso-proc: Add n = 1 flag to element meta
This allows exactly one arbitrary payload in any element, and is handled
after every other element with a more specific matcher has been parsed.

Both the #[xml(element(n = 1))] meta and its shortcut #[xml(element)]
are allowed and treated the exact same way.
2025-01-26 16:40:05 +00:00
Jonas Schäfer
beae4ae66b xso: fix no_std support
This reverts the Revert in 6ec275d3 which had to be done in order to
un-break usage of xso in std crates.
2025-01-26 15:07:17 +00:00
Emmanuel Gil Peyrot
01a0c51a2f xso-proc: Add support for the codec field on attribute meta
This allows a custom TextCodec to be used for encoding and decoding the
attribute’s value, instead of FromXmlText and AsOptionalXmlText.
2025-01-25 18:07:44 +01:00
Jonas Schäfer
763a603e49 xso: fix documentation typos
skip-changelog
2025-01-25 12:32:41 +00:00
Jonas Schäfer
4c9a271842 xso: remove incorrect/obsolete comment 2025-01-25 12:24:03 +01:00
Jonas Schäfer
69787569c9 xso: implement TextCodec<_> on all T: base64::engine::Engine
The `xso::text::Base64` struct remains as a shorthand (because frankly,
I find the const names in the base64 crate very unwieldly), but you can
now use any of the base64 engines as codec.
2025-01-25 12:22:05 +01:00
Jonas Schäfer
727e57b756 Implement #[xml(flag)] meta 2025-01-21 19:12:43 +01:00
Jonas Schäfer
6ec275d381 Revert "xso-proc: Replace std stuff with alloc/core stuff"
This reverts commit 4e5f0bc961.

Unfortunately, in std contexts, the `alloc` crate is not imported. That
means we cannot rely on it being accessible and it in fact breaks builds
of crates which are not `no_std`.

Fixes #155.
2025-01-20 17:52:35 +01:00
Jonas Schäfer
b8af0d8fa2 xmpp_parsers: --features disable-validation
It was broken in multiple ways:

- xso did not honour it: unknown children and attributes would cause a
  parse error even with `--features disable-validation` set on parsers.
  For this, we introduce a new feature flag on xso, `non-pedantic`,
  which defaults unknown children and attributes to discard instead of
  fail.

  Note that individual XSOs can still choose to be always pedantic or
  always lenient by explicitly declaring the intent via the
  `on_unknown_child` and `on_unknown_attribute` metas.

- Many tests in `xmpp_parsers` were broken with `--features
  disable-validation`. They now all pass while *still* being rn with
  `disable-validation` set: In that case, they test that parsing in fact
  succeeds.
2024-12-20 14:50:14 +01:00
Jonas Schäfer
58698f633f xso: only take reference in transform
This avoids the need for an expensive clone. Since we switched to AsXml
instead of IntoXml, we don't necessarily have to clone the data when
building new elements, only when it's absolutely necessary. The clones
then happen implicitly in the ItemToEvent iterator used internally.

This mostly fixes #86, with the caveat that there's no absolutely cheap
test: On success, the entire element will be copied, while on failure,
you learn about it rather quickly.
2024-12-20 12:44:43 +01:00
c8d3c068e0 xso: Replace std::io calls with use std::io 2024-12-19 20:51:57 +01:00
6a662e6a2c xso: Replace std stuff with alloc/core stuff 2024-12-19 20:51:57 +01:00
4e5f0bc961 xso-proc: Replace std stuff with alloc/core stuff 2024-12-19 20:51:56 +01:00
1c1b960265 xso: Add std feature ; default-features compiles in no-std 2024-12-19 20:38:28 +01:00
8c033a3fa0 xso: Implement FromXmlText and AsXmlText for NodePart,DomainPart,ResourcePart 2024-12-18 15:43:09 +00:00
Jonas Schäfer
7074344834 xso: correctly handle I/O errors during start event in from_reader
Without this, it'd panic, which is "not ideal", as they say.
2024-12-18 14:47:08 +00:00
Jonas Schäfer
fa039c588b xso: improve wording in AsXmlText and AsOptionalXmlText 2024-12-16 19:04:23 +01:00
Emmanuel Gil Peyrot
5e9635add3 rustfmt has changed, so update xso so CI is happy again 2024-12-15 16:26:05 +01:00
Jonas Schäfer
7d8ffe45a7 xso: add support for ignoring unknown stuff in extracts 2024-10-26 17:37:32 +00:00