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
/*!
Integration between `sval` and `serde`.

Add the `serde` feature to your `Cargo.toml` to enable this module:

```toml,no_run
[dependencies.sval]
features = ["serde"]
```

In no-std environments, `serde` support can be enabled using the `serde_no_std` feature
instead:

```toml,ignore
[dependencies.sval]
features = ["serde_no_std"]
```

# From `sval` to `serde`

A type that implements [`sval::Value`](../value/trait.Value.html) can be converted into
a type that implements [`serde::Serialize`]:

```
# struct MyValue;
# impl sval::value::Value for MyValue {
#     fn stream(&self, stream: &mut sval::value::Stream) -> Result<(), sval::value::Error> {
#         unimplemented!()
#     }
# }
# let my_value = MyValue;
let my_serialize = sval::serde::to_serialize(my_value);
```

When using `serde_no_std`, there are some limitations on what kinds of `sval::Value`s you
can convert into `serde::Serialize`s:

- Any type that uses [`value::Stream::map_key_begin`], [`value::Stream::map_value_begin`],
or [`value::Stream::seq_elem_begin`] would require buffering, so will return an error instead
in no-std environments.

# From `serde` to `sval`

A type that implements [`serde::Serialize`] can be converted into
a type that implements [`sval::Value`](../value/trait.Value.html):

```
# struct MySerialize;
# impl serde_lib::Serialize for MySerialize {
#     fn serialize<S: serde_lib::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
#         unimplemented!()
#     }
# }
# let my_serialize = MySerialize;
let my_value = sval::serde::to_value(my_serialize);
```

[`value::Stream::map_key_begin`]: ../value/struct.Stream.html#method.map_key_begin
[`value::Stream::map_value_begin`]: ../value/struct.Stream.html#method.map_value_begin
[`value::Stream::seq_elem_begin`]: ../value/struct.Stream.html#method.seq_elem_begin
*/

mod error;

mod to_serialize;
mod to_value;

use crate::{
    Error,
    Stream,
    Value,
};

use serde_lib::ser::{
    Serialize,
    Serializer,
};

/**
Convert a [`Value`] into a [`Serialize`].

If the `Value` uses nested maps or sequences where the keys, values
or elements aren't known upfront then this method will need to allocate
for them.
*/
pub fn to_serialize(value: impl Value) -> impl Serialize {
    to_serialize::ToSerialize(value)
}

/**
Serialize a [`Value`] using the given [`Serializer`].
*/
pub fn serialize<S>(serializer: S, value: impl Value) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    to_serialize(value).serialize(serializer)
}

/**
Convert a [`Serialize`] into a [`Value`].
*/
pub fn to_value(value: impl Serialize) -> impl Value {
    to_value::ToValue(value)
}

/**
Stream a [`Serialize`] using the given [`Stream`].
*/
pub fn stream(stream: impl Stream, value: impl Serialize) -> Result<(), Error> {
    crate::stream(stream, to_value(value))
}

#[doc(hidden)]
#[cfg(feature = "serde_std")]
pub const IS_NO_STD: bool = false;

#[doc(hidden)]
#[cfg(not(feature = "serde_std"))]
pub const IS_NO_STD: bool = true;