musli_value/lib.rs
1//! [<img alt="github" src="https://img.shields.io/badge/github-udoprog/musli-8da0cb?style=for-the-badge&logo=github" height="20">](https://github.com/udoprog/musli)
2//! [<img alt="crates.io" src="https://img.shields.io/crates/v/musli-value.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/musli-value)
3//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-musli--value-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/musli-value)
4//!
5//! Value support for [Müsli] suitable for buffering and interchange between
6//! formats.
7//!
8//! [Müsli]: https://github.com/udoprog/musli
9
10#![deny(missing_docs)]
11#![no_std]
12#![cfg_attr(doc_cfg, feature(doc_cfg))]
13
14#[cfg(feature = "alloc")]
15extern crate alloc;
16
17#[cfg(feature = "std")]
18extern crate std;
19
20mod de;
21mod en;
22mod error;
23mod type_hint;
24mod value;
25
26/// Convenient result alias for use with `musli_value`.
27pub type Result<T, E = Error> = core::result::Result<T, E>;
28
29#[doc(inline)]
30pub use self::value::{AsValueDecoder, Value};
31#[doc(inline)]
32pub use error::Error;
33
34use en::ValueEncoder;
35use musli::mode::Binary;
36use musli::{Decode, Encode};
37use musli_utils::Options;
38
39const OPTIONS: Options = musli_utils::options::new().build();
40
41/// Encode something that implements [Encode] into a [Value].
42pub fn encode<T>(value: T) -> Result<Value, Error>
43where
44 T: Encode<Binary>,
45{
46 use musli::en::Encoder;
47
48 let mut output = Value::Unit;
49
50 musli_utils::allocator::with(|alloc| {
51 let cx = musli_utils::context::Same::<_, Binary, Error>::new(&alloc);
52 ValueEncoder::<OPTIONS, _, _>::new(&cx, &mut output).encode(value)?;
53 Ok(output)
54 })
55}
56
57/// Decode a [Value] into a type which implements [Decode].
58pub fn decode<'de, T>(value: &'de Value) -> Result<T, Error>
59where
60 T: Decode<'de, Binary>,
61{
62 use musli::de::Decoder;
63
64 musli_utils::allocator::with(|alloc| {
65 let cx = musli_utils::context::Same::<_, Binary, Error>::new(&alloc);
66 value.decoder::<OPTIONS, _>(&cx).decode()
67 })
68}
69
70/// Decode a [Value] into a type which implements [Decode] using a custom
71/// context.
72pub fn decode_with<'de, C, T>(cx: &C, value: &'de Value) -> Result<T, C::Error>
73where
74 C: ?Sized + musli::Context,
75 T: Decode<'de, C::Mode>,
76{
77 use musli::de::Decoder;
78
79 cx.clear();
80 value.decoder::<OPTIONS, _>(cx).decode()
81}