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
//! A whole SML file: the unit a client sends and a meter answers with.
use crate;
use crateWriter;
use crate::;
/// The most messages a file can hold: each gets a one-byte message number.
const MAX_MESSAGES: usize = 256;
/// The longest file-ID prefix of a transaction ID.
const PREFIX: usize = 7;
/// An SML file: an open, the requests (or responses) and a close, sharing one file ID.
///
/// [`write`](Self::write) gives every message a transaction ID unique in the file (the
/// file ID's first seven bytes and the message number) and its own CRC. A meter repeats a
/// request's transaction ID in its answer, which is how a client matches the two.
///
/// ```
/// use sml_kit::{Body, CloseRequest, GetListRequest, OpenRequest, SmlFile, encode_frame};
///
/// let bodies = [
/// Body::OpenRequest(OpenRequest {
/// codepage: None,
/// client_id: b"gw-1",
/// req_file_id: b"F42",
/// server_id: None,
/// username: None,
/// password: None,
/// sml_version: None,
/// }),
/// Body::GetListRequest(GetListRequest {
/// client_id: b"gw-1",
/// server_id: None,
/// username: None,
/// password: None,
/// list_name: None,
/// }),
/// Body::CloseRequest(CloseRequest { global_signature: None }),
/// ];
/// let mut payload = [0u8; 128];
/// let n = SmlFile { file_id: b"F42", messages: &bodies }.write(&mut payload)?;
///
/// let mut wire = [0u8; 300];
/// let len = encode_frame(&payload[..n], &mut wire)?; // send &wire[..len]
/// # assert!(len > n);
/// # Ok::<(), sml_kit::Error>(())
/// ```
///
/// The shape is checked, not the direction: a file must start with an `OpenRequest` or
/// `OpenResponse`, end with a `CloseRequest` or `CloseResponse`, and have at most 256
/// messages; anything else is [`Error::InvalidFile`]. Every message is written with
/// `group_no` 0 and `abort_on_error` 0; for other values write [`Message`]s one by one.
///
/// `Debug` shows each body through its own `Debug`, which prints passwords as
/// `<redacted>`.