1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! # xylem
//! [![GitHub actions](https://github.com/SOF3/xylem/workflows/CI/badge.svg)](https://github.com/SOF3/xylem/actions?query=workflow%3ACI)
//! [![crates.io](https://img.shields.io/crates/v/xylem.svg)](https://crates.io/crates/xylem)
//! [![crates.io](https://img.shields.io/crates/d/xylem.svg)](https://crates.io/crates/xylem)
//! [![docs.rs](https://docs.rs/xylem/badge.svg)](https://docs.rs/xylem)
//! [![GitHub](https://img.shields.io/github/last-commit/SOF3/xylem)](https://github.com/SOF3/xylem)
//! [![GitHub](https://img.shields.io/github/stars/SOF3/xylem?style=social)](https://github.com/SOF3/xylem)
//!
//! Xylem is a stateful type conversion framework for Rust.
//!
//! ## Concepts
//! Xylem provides the [`Xylem`] trait,
//! which is similar to the [`std::convert::TryFrom`] trait,
//! but with the following differences:
//!
//! ### Stateful context
//! [`Xylem::convert`] passes a mutable [`Context`],
//! enabling stateful operations throughout the conversion process.
//! See [`Context`] documentation for details.
//!
//! ### Fixed concersion source
//! Unlike [`std::convert::TryFrom`],
//! [`Xylem`] takes the conversion source type
//! as an associated type [`Xylem::From`]
//! instead of a generic parameter.
//! This means each type can only be converted
//! from exactly one other specific type under a given [`Schema`].
//!
//! ### Schemas
//! [`Xylem`] accepts a type parameter `S` ("schema"),
//! which acts as a namespace defining the set of conversion rules.
//! This allows different downstream crates
//! to define their own conversion rules
//! without conflicting with each other.
//! For example, if crate `foo` wants to convert `bool` from `String`
//! and crate `bar` wants to convert `bool` from `i32`,
//! they can separately define schema types `foo::Xylem` and `bar::Xylem`,
//! then separately implement
//!
//! ```ignore
//! impl Xylem<foo::Schema> for bool {
//!     type From = String;
//!     // fn convert() omitted
//! }
//! impl Xylem<bar::Schema> for bool {
//!     type From = i32;
//!     // fn convert() omitted
//! }
//! ```
//!
//! Furthermore, since `foo::Schema` and `bar::Schema`
//! are declared in their own crates,
//! `Xylem<S>` is not considered a foreign trait,
//! so implementing custom conversion rules for [`std`] types
//! will not result in
//! [error E0220](https://doc.rust-lang.org/error-index.html#E0220) or
//! [error E0119](https://doc.rust-lang.org/error-index.html#E0119).
//!
//! To use the default conversion rules defined by xylem,
//! make the schema implement the [`SchemaExt`] trait.
//! There is a convenience macro [`declare_schema`] for this:
//!
//! ```rust
//! xylem::declare_schema!(Schema: xylem::SchemaExt);
//!
//! // we defined a schema type called `Schema`.
//! ```
//!
//! It is recommended to use `Schema` as the schema name
//! and declare it at the crate level,
//! because the [`Xylem`][xylem_codegen::Xylem] macro
//! uses `crate::Schema` as the default schema type.
//!
//! ## The `Xylem` macro
//! Xylem provides a [`Xylem`][xylem_codegen::Xylem] macro,
//! which derives the corresponding [`Xylem::From`] type
//! from a struct or enum
//! by replacing each type with the corresponding [`Xylem::From`] type,
//! as well as a [`Xylem`] implementation.
//! See the [`Xylem`][xylem_codegen::Xylem] documentation for details.
//!
//! Note that the order of fields matters
//! because xylem type conversion is stateful,
//! i.e. previous conversions may affect subsequent ones.
//!
//!
//! ## The `id` feature
//! With the `id` feature enabled,
//! xylem provides the [`Id`] type,
//! which is the motivational use case for xylem:
//! Deserialize a config file that references other fields by string ID,
//! replace each declaring ID with an integer storing its occurrence order,
//! and replace each referencing ID with the occurrence order of the declaring ID.
//!
//! The [`Id`] type takes two generic parameters, `S` and `X`.
//! The type `S` is just the schema type,
//! while the type `X` is the subject of identification.
//! `X` must also implement the [`Identifiable`] trait,
//! which has an associated type [`Identifiable::Scope`]
//! used to provide a namespace for the ID.
//! The declaring [`Id`] field must be declared under `X`,
//! and `X` must occur as a (transitive) child of the scope.
//! Further references to the ID of `X`
//! must occur also as transitive children of the scope,
//! because the scope is dropped when it completes parsing.
//!
//! Declaring IDs are marked with the argument `new = true`.
//! If the ID is to be cross-referenced after the scope drops,
//! also mark `track = true`.
//! Referencing IDs do not need to be marked,
//! but if they serve to import a scope,
//! they should be marked as `import = true`.
//!
//! See [tests/id.rs](https://docs.rs/crate/xylem/*/source/tests/id.rs) and
//! [tests/cross\_id.rs](https://docs.rs/crate/xylem/*/source/tests/cross_id.rs) for example usage.
//!
//! Note that it is not a design goal for xylem to support lookahead IDs.
//! Due to the stateful nature of xylem,
//! IDs are only indexed when the declaration has been scanned.
//! There is currently no plan to implement multiple passes
//! to pre-index IDs.

use std::any::TypeId;
use std::fmt;

// An internal re-export used for reusing arguments.
#[doc(hidden)]
pub use lazy_static::lazy_static;
/// Derives a [`Xylem`] implementation for a struct or enum
/// and the corresponding [`Xylem::From`] type.
///
/// In this page, "input" refers to the struct/enum written by the user manually,
/// and "derived" refers to the struct/enum generated by the macro
/// to be used as the [`Xylem::From`] type.
///
/// The derived type has the same structure as the input type
/// with each field type `Type` replaced by `<Type as Xylem<S>>::From`,
/// except otherwise specified by the field attributes.
/// Conversion takes place by mapping each field of the derived type
/// to the corresponding field of the input type
/// by calling [`Xylem::convert`].
///
/// # Container Attributes
/// The following attributes can be applied on the input.
///
/// ## `#[xylem(expose = Ident)]`
/// Expose the derived type with the specified name `Ident`.
/// The type has the same visibility as the input type.
///
/// ## `#[xylem(schema = path::to::Schema)]`
/// Specify the schema type to define for as `path::to::Schema`.
///
/// ## `#[xylem(serde(xxx))]`
/// Apply the serde attribute `xxx` to the derived type.
///
/// ## `#[xylem(derive(Foo, Bar))]`
/// Apply the derive macros `Foo`, `Bar` on the derived type.
///
/// ## `#[xylem(process)]`
/// Call [`Processable::preprocess`] before conversion,
/// call [`Processable::postprocess`] after conversion.
///
/// Requires the input type to implement the [`Processable`] trait.
///
/// # Field Attributes
/// The following attributes can be applied on the fields in the input.
/// As above, "input field" refers to the field written by the user manually,
/// and "derived field" refers to the field generated by the macro
/// that occur in the derived type.
/// Furthermore, the following assumes that
/// the attribute is applied on `foo: Bar` unless otherwise specified.
///
/// ## `#[xylem(serde(xxx))]`
/// Apply the serde attribute `xxx` to the derived field.
///
/// ## `#[xylem(preserve)]`:
/// The derived field will use the same type as the input field,
/// i.e. `#[xylem(preserve)] foo: Bar` generats `foo: Bar` in the derived type.
/// The value in the derived field is directly moved to the target field.
///
/// ## `#[xylem(transform = path(Type))]`
/// Generate a field `foo: Type` in the derived type,
/// and call `path(derived.foo)` during conversion.
/// `path` is the path to a function with the signature
/// `fn(Type) -> Result<Bar, S::Error>`.
/// For example, `#[xylem(transform = Ok(Type))]`
/// is equivalent to `#[xylem(preserve)]`.
///
/// ## `#[xylem(transform_with_context = path(Type))]`
/// Similar to `transform`, except `path` accepts an extra context parameter,
/// giving the signature `fn(Type, &mut S::Context) -> Result<Field, S::Error>`.
///
/// ## `#[xylem(default = expr)]`
/// Always uses `expr` (resolved every time the struct
/// or the enum variant is constructed) as the value.
/// Does not generate a field in the `From` type.
/// The expression should have type `Result<Field, S::Error>`,
/// where `Field` is the field type.
///
/// Comparing `default`, `preserve`, `transform` and `transform_with_context`:
/// - If a corresponding field is required in the derived type,
///     - If they have different types,
///         - If context is required, use `transform_with_context`.
///         - Otherwise, use `transform`.
///     - Otherwise, use `preserve`.
/// - Otherwise, use `default`.
///
/// ## `#[xylem(args(key1 = value1, key2 = value2))]`
/// Pass the given arguments in the [`Xylem::convert`] call.
/// Incompatible with `default`, `preserve`, `transform` and `transform_with_context`.
/// `key1` and `key2` are visible named fields in `<Bar as Xylem<S>>::Args`.
/// The values in the key are evaluated lazily and stored as a `static`.
/// The generated code is equivalent to the following:
///
/// ```ignore
/// lazy_static! {
///     static ref ARGS: Args = Args {
///         key1: value1,
///         key2: value2,
///     };
/// }
/// <Bar as Xylem<S>>::convert(derived.foo, context, &*ARGS)
/// ```
pub use xylem_codegen::Xylem;

#[cfg(feature = "id")]
pub mod id;
#[cfg(feature = "id")]
pub use id::{Id, IdArgs, IdString, Identifiable};
#[cfg(feature = "ext")]
mod ext;
#[cfg(feature = "ext")]
pub use ext::*;

/// Implementors of this trait have a special conversion rule under the schema `Schema`.
pub trait Xylem<S: Schema + ?Sized>: Sized + 'static {
    /// The type to convert from.
    type From: Sized;

    /// The args provided in the field.
    ///
    /// The type must be a struct that implements [`Default`],
    /// allowing the macro to instantiate it in the following format:
    ///
    /// ```ignore
    /// Args {
    ///    key1: value1,
    ///    key2: value2,
    ///    ..Default::default()
    /// }
    /// ```
    ///
    /// where the macro has the field attribute `#[xylem(key1 = value1, key2 = value2)]`.
    type Args: Default;

    /// Converts the `From` type to the `Self` type,
    /// registering the scope with the context.
    /// Do not override this method.
    #[inline]
    fn convert(
        from: Self::From,
        context: &mut <S as Schema>::Context,
        args: &Self::Args,
    ) -> Result<Self, <S as Schema>::Error> {
        let scope = context.start_scope::<Self>();
        let ret = Self::convert_impl(from, context, args)?;
        context.end_scope(scope);
        Ok(ret)
    }

    /// The implementation of the conversion.
    fn convert_impl(
        from: Self::From,
        context: &mut <S as Schema>::Context,
        args: &Self::Args,
    ) -> Result<Self, <S as Schema>::Error>;
}

/// The unit type is used as the dummy type for the root scope.
impl<S> Xylem<S> for ()
where
    S: Schema,
{
    type From = ();
    type Args = NoArgs;

    fn convert_impl(
        _: Self::From,
        _: &mut <S as Schema>::Context,
        _: &Self::Args,
    ) -> Result<Self, <S as Schema>::Error> {
        Ok(())
    }
}

/// Preprocessor and postprocessor extensions for [`Xylem`].
pub trait Processable<S: Schema + ?Sized>: Xylem<S> {
    /// This method is called at the beginning of [`Xylem::convert_impl`] if `#[xylem(process)]` is
    /// provided.
    fn preprocess(
        _from: &mut <Self as Xylem<S>>::From,
        _context: &mut <S as Schema>::Context,
    ) -> Result<(), <S as Schema>::Error> {
        Ok(())
    }

    /// This method is called just before [`Xylem::convert_impl`] returns if `#[xylem(process)]` is
    /// provided.
    fn postprocess(
        &mut self,
        _context: &mut <S as Schema>::Context,
    ) -> Result<(), <S as Schema>::Error> {
        Ok(())
    }
}

/// The schema type for a specific set of conversion rules.
///
/// Implementors should be declared in the same crate as the type they convert
/// to avoid [error E0210](https://doc.rust-lang.org/error-index.html#E0210).
pub trait Schema: 'static {
    /// The context type for this schema.
    type Context: Context;

    /// The error type for conversions in this schema.
    type Error: AbstractError;
}

/// The error type for a schema.
pub trait AbstractError: Sized {
    /// Creates a new error type.
    fn new<T: fmt::Display>(msg: T) -> Self;
}

#[cfg(feature = "anyhow")]
impl AbstractError for anyhow::Error {
    fn new<T: fmt::Display>(msg: T) -> Self { anyhow::anyhow!("{}", msg) }
}

/// The context of a conversion.
///
/// The context provides a stack of scopes.
/// A scope is typically the duration of the conversion of a value,
/// and the stack grows when types are converted recursively.
/// The top of the stack is the current scope.
///
/// Each layer of the stack has its own typemap,
/// which provides access to an arbitrary object bound accessible during the scope.
///
/// It is strongly discouraged to have recursive types
/// resulting in multiple layers of the scope to have the same type ID,
/// which may need to strange behavior when accessing the typemap,
/// becaues only the newest layer of the type is accessible.
pub trait Context: Default {
    /// Identifies a layer of scope.
    type Scope;

    /// Gets the nth topmost scope type ID.
    fn nth_last_scope(&self, n: usize) -> Option<TypeId>;

    /// Gets a shared reference to the storage of type `T`
    /// in the newest layer of the scope.
    fn get<T>(&self, scope: TypeId) -> Option<&T>
    where
        T: 'static;

    /// Gets a shared reference to the storage of type `T`
    /// in each layer, from top to bottom, if exists.
    fn get_each<T>(&self) -> Box<dyn Iterator<Item = &T> + '_>
    where
        T: 'static;

    /// Gets a mutable reference to the storage of type `T`
    /// in the newest layer of the scope.
    fn get_mut<T, F>(&mut self, scope: TypeId, default: F) -> &mut T
    where
        F: FnOnce() -> T,
        T: 'static;

    /// Pushes the type to the scope stack.
    ///
    /// This method is automatically called
    /// from [`Xylem::convert`].
    fn start_scope<T: 'static>(&mut self) -> Self::Scope;

    /// Pops a type from the scope stack.
    ///
    /// This method is automatically called
    /// from [`Xylem::convert`].
    fn end_scope(&mut self, scope: Self::Scope);
}

/// The default empty argument type.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoArgs;

#[cfg(feature = "typemap")]
mod typemap_context;
#[cfg(feature = "typemap")]
pub use typemap_context::DefaultContext;

/// Declare a normal schema type.
///
/// # Example
/// ```
/// xylem::declare_schema!(MySchema);
///
/// #[derive(xylem::Xylem)]
/// #[xylem(schema = MySchema)]
/// struct Foo {}
/// ```
#[macro_export]
macro_rules! declare_schema {
    ($(#[$meta:meta])* $vis:vis $name:ident $(: $($traits:path),+)?) => {
        $(#[$meta])*
        $vis enum $name {}

        impl $crate::Schema for $name {
            type Context = $crate::DefaultContext;
            type Error = anyhow::Error;
        }

        $($(
            impl $traits for $name {}
        )*)?
    }
}