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
//! # ser_nix
//!
//! Nix is a declarative, atomic, and reproducible package manager
//! that is configured with the nix programming language
//!
//! ```nix
//! {
//! a = 1;
//! b = "Hello World";
//! submap.foo = "bar";
//! }
//! ````
//!
//! ser_nix can be used to serialise arbitrary rust types into
//! corresponding nix data types. As the name implies, ser_nix
//! does *not* provide deserialisation capabilities, as the
//! process for doing so is non-trivial, and requires evaluating
//! nix code.
//!
//! ser_nix tries to follow the idioms of other serde libraries,
//! like [serde_json](https://docs.rs/serde_json/latest/serde_json/index.html).
//!
//! ```rust
//! use serde::Serialize;
//! use ser_nix::to_string;
//!
//! #[derive(Serialize)]
//! struct Person {
//! name: String,
//! age: u8,
//! }
//!
//! let cm = Person {
//! name: "John Doe".into(),
//! age: 65,
//! };
//!
//! let serialized = to_string(&cm).unwrap();
//!
//! let expected = "{\n name = \"John Doe\";\n age = 65;\n}".to_string();
//!
//! assert_eq!(serialized, expected);
//! ````
//!
//! ## Option and None values
//!
//! Following serde_json conventions, `Option::None` values are serialized
//! as `null` by default. To omit fields when they are `None`, use the
//! `#[serde(skip_serializing_if = "Option::is_none")]` attribute:
//!
//! ```rust
//! use serde::Serialize;
//! use ser_nix::to_string;
//!
//! #[derive(Serialize)]
//! struct Config {
//! enabled: Option<bool>,
//! #[serde(skip_serializing_if = "Option::is_none")]
//! optional: Option<String>,
//! }
//!
//! let config = Config {
//! enabled: None,
//! optional: None,
//! };
//!
//! let serialized = to_string(&config).unwrap();
//! // Output: { enabled = null; }
//! ```
//!
//! ## Nix paths
//!
//! In Nix, paths like `./foo.nix` or `/etc/nixos/configuration.nix` are written
//! without quotes. There are two ways to serialize paths as unquoted Nix paths:
//!
//! ### Using `NixPathBuf` wrapper type
//!
//! ```rust
//! use serde::Serialize;
//! use ser_nix::{to_string, NixPathBuf};
//!
//! #[derive(Serialize)]
//! struct NixConfig {
//! source: NixPathBuf,
//! }
//!
//! let config = NixConfig {
//! source: NixPathBuf::new("./hardware-configuration.nix"),
//! };
//!
//! let serialized = to_string(&config).unwrap();
//! // Output: { source = ./hardware-configuration.nix; }
//! ```
//!
//! For borrowed paths, use `NixPath<'a>`:
//!
//! ```rust
//! use ser_nix::{to_string, NixPath};
//! use std::path::Path;
//!
//! let path = Path::new("./config.nix");
//! let result = to_string(&NixPath::new(path)).unwrap();
//! assert_eq!(result, "./config.nix");
//! ```
//!
//! ### Using `#[serde(serialize_with = "...")]`
//!
//! ```rust
//! use serde::Serialize;
//! use ser_nix::to_string;
//! use std::path::PathBuf;
//!
//! #[derive(Serialize)]
//! struct NixConfig {
//! #[serde(serialize_with = "ser_nix::as_nix_path")]
//! source: PathBuf,
//! description: String,
//! }
//!
//! let config = NixConfig {
//! source: PathBuf::from("./hardware-configuration.nix"),
//! description: "Hardware config".to_string(),
//! };
//!
//! let serialized = to_string(&config).unwrap();
//! // source is unquoted: ./hardware-configuration.nix
//! // description is quoted: "Hardware config"
//! ```
//!
//! ## Disclaimer
//!
//! This library was created mostly to be used as a subcomponent of my main
//! project, [mkdev](https://github.com/4jamesccraven/mkdev). Because of that
//! it is not as a full featured as other serde implemenatations, but I intend
//! to change that over time
pub use Error;
pub use ;
pub use ;
use Serializer;
use Serialize;
/// Serialise the given data structure as a String of Nix data
///
/// # Errors
///
/// Serialization can fail if the implemenatation of `Serialize` for `T`
/// fails.
/// Removes extra whitespace that gets left behind due to indentation