Skip to main content

substrate_data/schema/convert/
write.rs

1//! A schema's value out: to the binary form, to text.
2
3use crate::binary::Writer;
4use crate::schema::Schema;
5use crate::text;
6
7/// `v` in the binary form, written straight ([`Schema::encode`]).
8pub fn to_bytes<T: Schema>(v: &T) -> Vec<u8> {
9    let mut w = Writer::new();
10    v.encode(&mut w);
11    w.finish()
12}
13
14/// `v` in the binary form, appended to `out` (a whole document, header
15/// first): a buffer that is kept and reused grows once.
16pub fn to_bytes_into<T: Schema>(v: &T, out: &mut Vec<u8>) {
17    let mut w = Writer::appending(std::mem::take(out));
18    v.encode(&mut w);
19    *out = w.finish();
20}
21
22/// `value` as text, indented.
23pub fn to_text<T: Schema>(value: &T) -> String {
24    text::to_string_pretty(&value.write())
25}