serde_value_utils/
lib.rs

1// Copyright 2019-present, OVH SAS
2// All rights reserved.
3//
4// This OVH Software is licensed to you under the MIT license <LICENSE-MIT
5// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
6// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
7// modified, or distributed except according to those terms. Please review the Licences for the
8// specific language governing permissions and limitations relating to use of the SAFE Network
9// Software.
10
11//! Bundle of tools to use with serde_value.
12//!
13//! # Quickstart
14//!
15//! You can start using it by first adding it to your `Cargo.toml`:
16//!
17//! ```toml
18//! [dependencies]
19//! serde = "1.0"
20//! serde_derive = "1.0"
21//! serde_value_utils = "0.1"
22//! ```
23//!
24//! Then, create a structure which implement the `serde::Serialize` trait and use it with any
25//! serde lib.
26//!
27//! # Example: to_flatten_maptree
28//!
29//! ```rust
30//! #[macro_use]
31//! extern crate serde_derive;
32//! extern crate serde_json;
33//! extern crate serde_value_utils;
34//!
35//! #[derive(Serialize, Clone, Debug)]
36//! struct SubFoo {
37//!     a: String,
38//!     b: u64,
39//! }
40//!
41//! #[derive(Serialize, Clone, Debug)]
42//! struct Foo {
43//!     a: String,
44//!     b: f64,
45//!     c: Vec<i8>,
46//!     d: SubFoo,
47//! }
48//!
49//! fn main() {
50//!     let foo = Foo { a: "test".into(), b: 0.5, c: vec![5, 9], d: SubFoo { a: "subtest".into(), b: 695217 } };
51//!     let ser = serde_value_utils::to_flatten_maptree("_", Some("_"), &foo).unwrap();
52//!     println!("{}", serde_json::to_string_pretty(&ser).unwrap());
53//! }
54//! ```
55//! **Output**:
56//! ```json
57//!  {
58//!   "_a": "test",
59//!   "_b": 0.5,
60//!   "_c_0": 5,
61//!   "_c_1": 9,
62//!   "_d_a": "subtest",
63//!   "_d_b": 695217
64//! }
65//! ```
66//! ## Feature with-schema
67//!
68//! The feature `with-schema` allow to suffix fields names to suits to the
69//! [LDP naming conventions](https://docs.ovh.com/fr/logs-data-platform/field-naming-conventions/).
70//!
71//! In your `Cargo.toml`, set:
72//!
73//! ```toml
74//! [dependencies]
75//! serde_value_utils = { version = "0.1", features = ["with-schema"] }
76//! ```
77//!
78//! Re-run the previous example, and now the output will be :
79//!
80//! ```json
81//! {
82//!   "_a": "test",
83//!   "_b_float": 0.5,
84//!   "_c_0_long": 5,
85//!   "_c_1_long": 9,
86//!   "_d_a": "subtest",
87//!   "_d_b_double": 695217
88//! }
89//! ```
90//! # Example: try_detect_type
91//!
92//! ```rust
93//! extern crate serde_value_utils;
94//!
95//! use serde_value_utils::try_detect_type;
96//!
97//! fn main() {
98//!     println!("{:?}", try_detect_type("6.5"));
99//! }
100//! ```
101//! **Output**:
102//! ```
103//! F64(6.5)
104//! ```
105#![doc(
106html_logo_url = "https://eu.api.ovh.com/images/com-square-bichro.png",
107html_favicon_url = "https://www.ovh.com/favicon.ico",
108)]
109#![deny(warnings, missing_docs)]
110extern crate serde;
111extern crate serde_value;
112
113pub use detect::try_detect_type;
114pub use flatten::to_flatten_maptree;
115
116mod flatten;
117mod detect;