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
//! Rust ↔ [`Value`] conversions for embedders.
//!
//! Scalar Rust values that map to Nybl without validation implement standard
//! [`From`]. Recursive values use [`IntoValue`] because constructing a Nybl
//! array, dict, or `Result` can exceed the runtime's value-depth invariant.
//! Reverse conversion is borrowed-first: [`FromValue`] accepts `&Value`, which
//! matches the `&[Value]` received by [`crate::NyblHost`] and permits zero-copy
//! extraction such as `&str`.
//!
//! `Number` to `f32` extraction uses range-checked IEEE-754 narrowing: finite
//! values outside `f32`'s finite range return [`ValueConversionError`] instead
//! of becoming infinity. In-range values may round to the nearest `f32`, while
//! `NaN` and positive or negative infinity are preserved.
//!
//! ```
//! use std::collections::BTreeMap;
//! use nybl::{FromValue, IntoValue, Value, nybl_value};
//!
//! # fn example() -> Result<(), nybl::ValueConversionError> {
//! let request = nybl_value!({
//! "name": "Ada",
//! "scores": [10, 20, 30],
//! "active": true,
//! })?;
//!
//! let fields: BTreeMap<String, Value> = request.to_rust()?;
//! let name: &str = fields["name"].to_rust()?;
//! let scores: Vec<i64> = FromValue::from_value(&fields["scores"])?;
//! assert_eq!(name, "Ada");
//! assert_eq!(scores, vec![10, 20, 30]);
//!
//! let response = Ok::<_, &str>(scores).into_value()?;
//! let round_trip: Result<Vec<i64>, &str> = response.to_rust()?;
//! assert_eq!(round_trip.unwrap(), vec![10, 20, 30]);
//! # Ok(())
//! # }
//! # example().unwrap();
//! ```
pub use ;
use crateValue;
use String;
use String;
/// Fallibly convert an owned Rust value into a Nybl [`Value`].
///
/// This local trait deliberately has explicit implementations instead of a
/// blanket `T: Into<Value>` implementation. That keeps generic `Vec`,
/// `Option`, `Result`, and map conversions coherent and lets downstream crates
/// implement `IntoValue` for their own local types.
/// Extract a Rust value from a borrowed Nybl [`Value`].
///
/// The lifetime permits zero-copy targets (`&str`, `&Value`) while the same
/// trait also supports owned targets such as `String`, `Vec<T>`, and
/// `BTreeMap<String, T>`.