Commit graph

22 commits

Author SHA1 Message Date
Emmanuel Gil Peyrot
f318dd460d xso: Use core instead of std wherever possible 2024-08-04 12:41:17 +00: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
c40023a65e xso: add feature flag marker to derive macros 2024-07-26 17:11:48 +02:00
Jonas Schäfer
204c82e6b0 xso: add more doc comments throughout 2024-07-25 22:22:55 +02:00
Emmanuel Gil Peyrot
6e1f24c385 xso: Apply clippy’s suggestions 2024-07-25 07:35:19 +00:00
Jonas Schäfer
7bc7e7a684 xso: remove remnants of IntoXml 2024-07-24 16:05:06 +02:00
Jonas Schäfer
ccf38cdf9b Port everything over to AsXml 2024-07-24 16:05:06 +02:00
Jonas Schäfer
4910b01244 xso: add text conversion traits for AsXml 2024-07-24 16:05:06 +02:00
Jonas Schäfer
d29b89d307 xso: introduce AsXml trait
This will soon replace the IntoXml trait. The idea here is that we
don't generally need to take ownership of values which are going to
be transformed into XML: most of the time, the XML text is created
by building a string from some more specific type, such as an
integer or an enum. Requiring to clone an entire structure for this
purpose is wasteful.

In other cases, we actually could reference data right from the structs
we are converting to XML. In those cases, assuming that an iterator
always generates owned data would be incorrect, too.

Hence, we introduce a new `Item` type which closely mirrors the
`rxml::Item` type, but where the constituents are `Cow`. In the upcoming
changes, we are going to work toward replacing all uses of `IntoXml`
with `AsXml`, as well as modifying the macros accordingly.
2024-07-24 16:05:06 +02:00
Emmanuel Gil Peyrot
b18385cfff xso: Fix all clippy warnings 2024-07-03 16:28:11 +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
2fb9fc6959 xso: add more doc_cfg labels 2024-06-30 13:05:37 +02:00
Jonas Schäfer
1f679c3af7 xso: add traits for XML text <-> value conversion
The traits have undergone a couple iterations and this is what we end up
with. The core issue which makes this entire thing ugly is the
Orphan Rule, preventing some trait implementations relating to types
which haven't been defined in this crate.

In an ideal world, we would implement FromXmlText and IntoXmlText for
all types implementing FromStr and/or fmt::Display.

This comes with two severe issues:

1. Downstream crates cannot chose to have different
   parsing/serialisation behaviour for "normal" text vs. xml.

2. We ourselves cannot define a behaviour for `Option<T>`. `Option<T>`
   does not implement `FromStr` (nor `Display`), but the standard
   library *could* do that at some point, and thus Rust doesn't let us
   implement e.g. `FromXmlText for Option<T> where T: FromXmlText`,
   if we also implement it on `T: FromStr`.

The second one hurts particularly once we get to optional attributes:
For these, we need to "detect" that the type is in fact `Option<T>`,
because we then need to invoke `FromXmlText` on `T` instead of
`Option<T>`. Unfortunately, we cannot do that: macros operate on token
streams and we have no type information available.

We can of course match on the name `Option`, but that breaks down when
users re-import `Option` under a different name. Even just enumerating
all the possible correct ways of using `Option` from the standard
library (there are more than three) would be a nuisance at best.

Hence, we need *another* trait or at least a specialized implementation
of `FromXmlText for Option<T>`, and we cannot do that if we blanket-impl
`FromXmlText` on `T: FromStr`.

That makes the traits what they are, and introduces the requirement that
we know about any upstream crate which anyone might want to parse from
or to XML. This sucks a lot, but that's the state of the world. We are
late to the party, and we cannot expect everyone to do the same they
have done for `serde` (many crates have a `feature = "serde"` which then
provides Serialize/Deserialize trait impls for their types).
2024-06-30 13:01:36 +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
9ec9a0f0c6 xso: introduce xso::from_bytes
This is useful both for doctests and other scenarios where you just
want to parse a bit of XML you already have.
2024-06-23 09:41:28 +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
49adbc49ac xso: make newlines after module comments consistent with other crates 2024-06-18 17:27:27 +02:00
Jonas Schäfer
dd76a62bdd xso: only warn on missing docs
The CI makes that an error anyway, and this allows developers to start
sketching stuff without running into errors all the time (and then
adding `/// TODO` to work around them).
2024-06-18 17:27:27 +02:00
Jonas Schäfer
17a38f190b xso: mark iterator type as non-breaking to change
Just like with the builder type, the concrete iterator type on IntoXml
is supposed to be an implementation detail. That allows switching freely
between various ways to generate such a type.
2024-06-18 17:20:27 +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
14a1d66bf8 xso: create library for streamed XML parsing
This library provides the traits to parse structs from XML and
serialise them into XML without having to buffer the document object
model in memory.

The only implementations it provides are for minidom, basically
providing a lower-level interface to `minidom::Element::from_reader` and
`minidom::Element::to_writer`.

This is the first stepping stone into a world where `xmpp_parsers` can
parse the structs directly from XML.
2024-06-18 16:54:11 +02:00