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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
Binn-IR
Copyright (C) 2018-2023 Anonymous
There are several releases over multiple years,
they are listed as ranges, such as: "2018-2023".
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/
//! # Encoding functions
use {
alloc::string::String,
std::io::Write,
crate::{Blob, IoResult, List, Map, Object, Size, Value},
};
/// # Encodes a value
///
/// Result: total bytes that have been written.
pub fn encode<W, T>(stream: &mut W, value: T) -> IoResult<Size> where W: Write, T: Into<Value> {
value.into().encode(stream)
}
/// # Encodes a [`Null`]
///
/// Result: total bytes that have been written.
///
/// [`Null`]: enum.Value.html#variant.Null
pub fn encode_null<W>(stream: &mut W) -> IoResult<Size> where W: Write {
Value::Null.encode(stream)
}
/// # Encodes a `bool` via [`True`] or [`False`]
///
/// Result: total bytes that have been written.
///
/// [`True`]: enum.Value.html#variant.True
/// [`False`]: enum.Value.html#variant.False
pub fn encode_bool<W>(stream: &mut W, b: bool) -> IoResult<Size> where W: Write {
let b = if b { Value::True } else { Value::False };
b.encode(stream)
}
/// # Encodes a [`U8`]
///
/// Result: total bytes that have been written.
///
/// [`U8`]: enum.Value.html#variant.U8
pub fn encode_u8<W>(stream: &mut W, u: u8) -> IoResult<Size> where W: Write {
Value::U8(u).encode(stream)
}
/// # Encodes an [`I8`]
///
/// Result: total bytes that have been written.
///
/// [`I8`]: enum.Value.html#variant.I8
pub fn encode_i8<W>(stream: &mut W, i: i8) -> IoResult<Size> where W: Write {
Value::I8(i).encode(stream)
}
/// # Encodes a [`U16`]
///
/// Result: total bytes that have been written.
///
/// [`U16`]: enum.Value.html#variant.U16
pub fn encode_u16<W>(stream: &mut W, u: u16) -> IoResult<Size> where W: Write {
Value::U16(u).encode(stream)
}
/// # Encodes an [`I16`]
///
/// Result: total bytes that have been written.
///
/// [`I16`]: enum.Value.html#variant.I16
pub fn encode_i16<W>(stream: &mut W, i: i16) -> IoResult<Size> where W: Write {
Value::I16(i).encode(stream)
}
/// # Encodes a [`U32`]
///
/// Result: total bytes that have been written.
///
/// [`U32`]: enum.Value.html#variant.U32
pub fn encode_u32<W>(stream: &mut W, u: u32) -> IoResult<Size> where W: Write {
Value::U32(u).encode(stream)
}
/// # Encodes an [`I32`]
///
/// Result: total bytes that have been written.
///
/// [`I32`]: enum.Value.html#variant.I32
pub fn encode_i32<W>(stream: &mut W, i: i32) -> IoResult<Size> where W: Write {
Value::I32(i).encode(stream)
}
/// # Encodes a [`U64`]
///
/// Result: total bytes that have been written.
///
/// [`U64`]: enum.Value.html#variant.U64
pub fn encode_u64<W>(stream: &mut W, u: u64) -> IoResult<Size> where W: Write {
Value::U64(u).encode(stream)
}
/// # Encodes an [`I64`]
///
/// Result: total bytes that have been written.
///
/// [`I64`]: enum.Value.html#variant.I64
pub fn encode_i64<W>(stream: &mut W, i: i64) -> IoResult<Size> where W: Write {
Value::I64(i).encode(stream)
}
/// # Encodes a [`Float`]
///
/// Result: total bytes that have been written.
///
/// [`Float`]: enum.Value.html#variant.Float
pub fn encode_float<W>(stream: &mut W, f: f32) -> IoResult<Size> where W: Write {
Value::Float(f).encode(stream)
}
/// # Encodes a [`Double`]
///
/// Result: total bytes that have been written.
///
/// [`Double`]: enum.Value.html#variant.Double
pub fn encode_double<W>(stream: &mut W, d: f64) -> IoResult<Size> where W: Write {
Value::Double(d).encode(stream)
}
/// # Encodes a [`Text`]
///
/// Result: total bytes that have been written.
///
/// [`Text`]: enum.Value.html#variant.Text
pub fn encode_text<W, T>(stream: &mut W, s: T) -> IoResult<Size> where W: Write, T: Into<String> {
Value::Text(s.into()).encode(stream)
}
/// # Encodes a [`DateTime`]
///
/// Result: total bytes that have been written.
///
/// [`DateTime`]: enum.Value.html#variant.DateTime
pub fn encode_date_time<W, T>(stream: &mut W, s: T) -> IoResult<Size> where W: Write, T: Into<String> {
Value::DateTime(s.into()).encode(stream)
}
/// # Encodes a [`Date`]
///
/// Result: total bytes that have been written.
///
/// [`Date`]: enum.Value.html#variant.Date
pub fn encode_date<W, T>(stream: &mut W, s: T) -> IoResult<Size> where W: Write, T: Into<String> {
Value::Date(s.into()).encode(stream)
}
/// # Encodes a [`Time`]
///
/// Result: total bytes that have been written.
///
/// [`Time`]: enum.Value.html#variant.Time
pub fn encode_time<W, T>(stream: &mut W, s: T) -> IoResult<Size> where W: Write, T: Into<String> {
Value::Time(s.into()).encode(stream)
}
/// # Encodes a [`DecimalStr`]
///
/// Result: total bytes that have been written.
///
/// [`DecimalStr`]: enum.Value.html#variant.DecimalStr
pub fn encode_decimal_str<W, T>(stream: &mut W, s: T) -> IoResult<Size> where W: Write, T: Into<String> {
Value::DecimalStr(s.into()).encode(stream)
}
/// # Encodes a [`Blob`]
///
/// Result: total bytes that have been written.
///
/// [`Blob`]: enum.Value.html#variant.Blob
pub fn encode_blob<W, T>(stream: &mut W, bytes: T) -> IoResult<Size> where W: Write, T: Into<Blob> {
Value::Blob(bytes.into()).encode(stream)
}
/// # Encodes a [`List`]
///
/// Result: total bytes that have been written.
///
/// [`List`]: enum.Value.html#variant.List
pub fn encode_list<W, T>(stream: &mut W, list: T) -> IoResult<Size> where W: Write, T: Into<List> {
Value::List(list.into()).encode(stream)
}
/// # Encodes a [`Map`]
///
/// Result: total bytes that have been written.
///
/// [`Map`]: enum.Value.html#variant.Map
pub fn encode_map<W, T>(stream: &mut W, map: T) -> IoResult<Size> where W: Write, T: Into<Map> {
Value::Map(map.into()).encode(stream)
}
/// # Encodes an [`Object`]
///
/// Result: total bytes that have been written.
///
/// [`Object`]: enum.Value.html#variant.Object
pub fn encode_object<W, T>(stream: &mut W, object: T) -> IoResult<Size> where W: Write, T: Into<Object> {
Value::Object(object.into()).encode(stream)
}