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
//! # uftp
//!
//! UFTP 3.1, the USEF Flex Trading Protocol, in Rust. Distribution system operators (DSO),
//! aggregators (AGR) and the Common Reference Operator (CRO) use it to trade grid
//! flexibility. Since version 3 it is maintained by LF Energy as *Shapeshifter*. Not the
//! UDP multicast file-transfer tool of the same name.
//!
//! The crate covers the 26 messages, their XML, their CS1 (Ed25519) signatures, the ISP
//! arithmetic, and the rules a receiver applies. It is *sans I/O*: it reads no clock,
//! random source, file or network. Ids, timestamps, keys and the key lookup come from you.
//!
//! The [documentation site](https://hupe1980.github.io/uftp/) explains the protocol and each
//! part of the crate, and defines the specification notes (`SS-…`) cited in these docs.
//!
//! ```
//! use uftp::jiff::{SignedDuration, Timestamp};
//! use uftp::*;
//! # #[cfg(feature = "cs1")]
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//!
//! let dso_key = SigningKey::from_seed(&[1; 32]);
//! let directory = Directory::new()
//! .with(Participant::new("dso.example".parse()?, Role::Dso, dso_key.public_keys([0; 32])));
//! let id = |n: u8| MessageUuid::from_bytes([n; 16]);
//! let now: Timestamp = "2026-11-30T12:00:00Z".parse()?;
//!
//! // A DSO asks for 500 kW less consumption from 17:00 to 19:00 local time (ISPs 69–76).
//! // A conversation starts with its first message; ISPs are PT15M unless set otherwise.
//! let meta = Metadata::new("dso.example".parse()?, "agr.example".parse()?, now, id(1));
//! let header = FlexHeader::new(
//! meta,
//! "Europe/Amsterdam".parse()?,
//! "2026-12-01".parse()?,
//! "ean.871685900012636543".parse()?,
//! );
//! let isps = vec![FlexRequestIsp::requested(IspSpan::new(69, 8)?, -500_000, 0)];
//! let request = FlexRequest::new(header, "2026-11-30T18:00:00Z".parse()?, isps);
//! let document = seal(&request.clone().into(), Role::Dso, &dso_key)?.to_xml();
//!
//! // The aggregator receives it: every check, in the order the protocol needs.
//! let mut endpoint = Endpoint::new(
//! OpenOptions::for_recipient("agr.example".parse()?),
//! SignedDuration::from_mins(15),
//! );
//! let opened = endpoint.receive(document.as_bytes(), now, directory.lookup())?;
//! let received: FlexRequest = opened.into_message().try_into()?;
//! assert_eq!(received, request);
//!
//! // It answers in the same conversation.
//! let response = received.reply(Reply::accepted(id(2), now));
//! assert_eq!(response.header().meta().conversation_id, id(1));
//! # Ok(()) }
//! # #[cfg(not(feature = "cs1"))]
//! # fn main() {}
//! ```
//!
//! ## Receiving
//!
//! [`Endpoint::receive`] runs, in this order:
//! 1. [`open`]: a strict signature check over exactly the received bytes, then the schema,
//! the sender binding, the sender's role, the type's rules and the recipient;
//! 2. the [`Market`]'s ISP duration and time zone, if set;
//! 3. [`Message::check_time`];
//! 4. the [`Outbox`], if set: a response must answer a request this endpoint
//! [sent](Endpoint::sent);
//! 5. the [`ReplayGuard`], last, so it remembers only messages it accepted.
//!
//! Each step is public on its own. With the `http` feature, `Endpoint::receive_request`
//! checks the headers before the body, and `uftp::http::response` writes the answer.
//!
//! ## Two ways to say no
//!
//! UFTP 3.1 (Appendix 4 §4) answers a refusal in one of two ways, and a [`Rejection`] knows
//! which:
//!
//! | When | [`Rejection::http_status`] | Then |
//! |---|---|---|
//! | not UFTP XML, too large, a bad header | 400 (411 without a length) | nothing else |
//! | the signature does not verify | 401 | nothing else |
//! | the sender's key cannot be looked up | 419, retry | nothing else |
//! | the sender is over its quota | 429, [`Rejection::retry_after`] | nothing else |
//! | valid, verified, but it breaks a rule | 200 | seal and send [`Rejection::response`] |
//!
//! The Rejected response goes to the sender whose signature verified, never to a domain the
//! message merely names. Its [`RejectionReason`] is one of the specification's 38;
//! [`RejectionReason::source`] cites where each is defined. Reasons that depend on your
//! state, such as a revoked offer, are yours to raise with [`Rejection::because`].
//!
//! A refusal's text is one line you can log and send back: received values are escaped
//! and cut after 64 characters or 128 escaped bytes, and no refusal is longer than 512
//! bytes.
//!
//! ## Sending
//!
//! [`seal`] refuses a message that breaks a rule, so what you send is valid. Build
//! messages with the constructors ([`FlexHeader::new`], [`FlexRequest::new`],
//! [`FlexOffer::unsolicited`], [`FlexOrder::unsolicited`]) and the flow helpers
//! ([`FlexRequest::offer`], [`FlexOffer::order`], [`FlexOffer::revoke`]). [`transport`] has
//! the content type, the paths and [`transport::Delivery`] for the status you get back.
//!
//! ## Settling
//!
//! Each month the DSO settles its orders. [`FlexOrder::settlement`] turns an order into
//! an item at its price × ActivationFactor (SS-26), and [`FlexSettlement::new`] wraps the
//! items. The aggregator calls [`FlexSettlement::check_against`] with the orders it
//! accepted. It gets back one Accepted or Disputed status per item, or *Missing Settlement
//! Items* if an order of the period is not settled `[UFTP §3.10.1]`. The figures the DSO
//! computes (delivered power, deficiency, penalty) are transported, not computed;
//! [`SettlementIsp`] says how their signs are read.
//!
//! ## Power
//!
//! Every power is an `i64` in watts, positive for consumption `[UFTP §4.2.1]`: a request
//! for 500 kW less is `-500_000`. The XSD's `xs:integer` is unbounded; a power outside the
//! `i64` range is refused when parsed, never clamped.
//!
//! ## Features
//!
//! `cs1` (default) adds keys, [`seal`] and [`open`], the [`Directory`], [`Endpoint`],
//! [`Outbox`] and [`ReplayGuard`]. Without it the crate is the messages, their rules and XML,
//! the ISP grid and the flow and settle helpers. `http` adds `uftp::http` and turns on `cs1`.
//!
//! ## Keys
//!
//! [`Directory`] holds participants for configured deployments, with two keys during a
//! rollover. [`dns`] builds the record names of Appendix 4 and parses their keys. Query them
//! with a DNSSEC-validating resolver of your choice.
// Without `cs1` there is no way from bytes to a message: these must not compile.
// Without `cs1` nothing reads XML: the only way in is `open`, which verifies first.
// docs.rs labels feature-gated items (`http`) on its nightly toolchain.
pub use ;
pub use ;
pub use Endpoint;
pub use ;
pub use ;
pub use Market;
pub use ;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
pub use ;
pub use Outbox;
pub use RejectionReason;
pub use ReplayGuard;
pub use ;
pub use Limits;
/// The `jiff` crate, whose `Timestamp` and `civil::Date` appear in this crate's API.
pub use jiff;
/// The `rust_decimal` crate, whose `Decimal` appears in amounts and metering values.
pub use rust_decimal;