skip to content
==> include/mesh/rsfmt.h <==
1/* @ajax/mesh-rsfmt: the text Rust's formatting produces, for byte-identical replies.
2 *
3 * meshd's replies and log lines were written with Rust's `{:?}` and `{}`; the C
4 * port must say exactly the same. Appends to a mesh_buf. */
5#ifndef MESH_RSFMT_H
6#define MESH_RSFMT_H
7
8#include "mesh/buf.h"
9
10#include <stddef.h>
11#include <stdint.h>
12
13/* `{:?}` of a str: double quotes; \" \\ \n \r \t \0 escaped, other control
14 * characters (and DEL) as \u{hex}; everything else as it is. */
15void mesh_rs_debug_str(mesh_buf *b, const char *s, size_t n);
16void mesh_rs_debug_cstr(mesh_buf *b, const char *s);
17
18/* `{:?}` of a std::time::Duration: 10s, 1.5s, 500ms, 2.5ms, 40µs, 7ns. */
19void mesh_rs_duration(mesh_buf *b, uint64_t secs, uint32_t nanos);
20static inline void mesh_rs_duration_ms(mesh_buf *b, uint64_t ms) {
21    mesh_rs_duration(b, ms / 1000, (uint32_t)(ms % 1000) * 1000000u);
22}
23
24/* `{}` of an f32 / f64: the shortest digits that read back to the same value,
25 * written without an exponent (1e20 is "100000000000000000000", 1e-7 is
26 * "0.0000001"); NaN, inf, -inf; -0 for negative zero. */
27void mesh_rs_f32(mesh_buf *b, float v);
28void mesh_rs_f64(mesh_buf *b, double v);
29
30/* `{:.p}` of an f64 (as Rust: round half to even on the exact value; NaN, inf). */
31void mesh_rs_fixed(mesh_buf *b, double v, int p);
32
33/* String::from_utf8_lossy: valid UTF-8 copied, each maximal invalid
34 * subsequence replaced by U+FFFD (as Rust and the Unicode standard do). */
35void mesh_rs_lossy(mesh_buf *b, const void *bytes, size_t n);
36/* str::from_utf8 succeeds. */
37int mesh_rs_utf8_valid(const void *bytes, size_t n);
38
39/* Rust's number parsing (str::parse), for byte-identical acceptance and errors.
40 * Return 0, or an error code whose text mesh_rs_parse_error gives:
41 * "cannot parse integer from empty string", "invalid digit found in string",
42 * "number too large to fit in target type", "invalid float literal",
43 * "cannot parse float from empty string". */
44enum { MESH_RS_EMPTY = 1, MESH_RS_INVALID, MESH_RS_OVERFLOW, MESH_RS_FLOAT_INVALID, MESH_RS_FLOAT_EMPTY };
45int mesh_rs_parse_u64(const char *s, size_t n, uint64_t *out);
46int mesh_rs_parse_u32(const char *s, size_t n, uint32_t *out);
47int mesh_rs_parse_i64(const char *s, size_t n, int64_t *out);
48int mesh_rs_parse_f32(const char *s, size_t n, float *out);
49int mesh_rs_parse_f64(const char *s, size_t n, double *out);
50const char *mesh_rs_parse_error(int code);
51
52#endif