/rust/registry/src/index.crates.io-6f17d22bba15001f/rmpv-1.3.0/src/encode/value.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use std::io::Write; |
2 | | |
3 | | use rmp::encode::{ |
4 | | write_array_len, write_bin, write_bool, write_ext_meta, write_f32, write_f64, write_map_len, |
5 | | write_nil, write_sint, write_str, write_uint, |
6 | | }; |
7 | | |
8 | | use super::Error; |
9 | | use crate::{IntPriv, Integer, Utf8String, Value}; |
10 | | |
11 | | /// Encodes and attempts to write the most efficient representation of the given Value. |
12 | | /// |
13 | | /// # Note |
14 | | /// |
15 | | /// All instances of `ErrorKind::Interrupted` are handled by this function and the underlying |
16 | | /// operation is retried. |
17 | 0 | pub fn write_value<W>(wr: &mut W, val: &Value) -> Result<(), Error> |
18 | 0 | where W: Write |
19 | 0 | { |
20 | 0 | match *val { |
21 | | Value::Nil => { |
22 | 0 | write_nil(wr).map_err(Error::InvalidMarkerWrite)?; |
23 | | } |
24 | 0 | Value::Boolean(val) => { |
25 | 0 | write_bool(wr, val).map_err(Error::InvalidMarkerWrite)?; |
26 | | } |
27 | 0 | Value::Integer(Integer { n }) => { |
28 | 0 | match n { |
29 | 0 | IntPriv::PosInt(n) => { |
30 | 0 | write_uint(wr, n)?; |
31 | | } |
32 | 0 | IntPriv::NegInt(n) => { |
33 | 0 | write_sint(wr, n)?; |
34 | | } |
35 | | } |
36 | | } |
37 | 0 | Value::F32(val) => { |
38 | 0 | write_f32(wr, val)?; |
39 | | } |
40 | 0 | Value::F64(val) => { |
41 | 0 | write_f64(wr, val)?; |
42 | | } |
43 | 0 | Value::String(Utf8String { ref s }) => match *s { |
44 | 0 | Ok(ref val) => write_str(wr, val)?, |
45 | 0 | Err(ref err) => write_bin(wr, &err.0)?, |
46 | | }, |
47 | 0 | Value::Binary(ref val) => { |
48 | 0 | write_bin(wr, val)?; |
49 | | } |
50 | 0 | Value::Array(ref vec) => { |
51 | 0 | write_array_len(wr, vec.len() as u32)?; |
52 | 0 | for v in vec { |
53 | 0 | write_value(wr, v)?; |
54 | | } |
55 | | } |
56 | 0 | Value::Map(ref map) => { |
57 | 0 | write_map_len(wr, map.len() as u32)?; |
58 | 0 | for (key, val) in map { |
59 | 0 | write_value(wr, key)?; |
60 | 0 | write_value(wr, val)?; |
61 | | } |
62 | | } |
63 | 0 | Value::Ext(ty, ref data) => { |
64 | 0 | write_ext_meta(wr, data.len() as u32, ty)?; |
65 | 0 | wr.write_all(data).map_err(Error::InvalidDataWrite)?; |
66 | | } |
67 | | } |
68 | | |
69 | 0 | Ok(()) |
70 | 0 | } |