bus-contract 0.5.0

A contract for a hook bus: libraries declare typed hook points and listen with named order (after, before, last), owners and deadlines; the application supplies the implementation and decides on one bus or several. No dependencies.
Documentation
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Validated ids: [`PointId`] and [`ListenerId`], checked when they are
//! made, in `const` as well as at run time.
//!
//! The idea of ids as newtypes checked at construction comes from
//! eventbus-contract's `Topic::new` and `ConsumerGroup::new`
//! (<https://github.com/mapseekai/eventbus-contract>), and the `const`
//! form that fails at compile time from qubit-event-bus's
//! `Topic::new_static` (<https://github.com/qubit-ltd/rs-event-bus>).

use std::borrow::Cow;
use std::fmt;
use std::ops::Deref;

/// Why an id was refused ([`check_id`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IdError {
    /// The id is empty.
    Empty,
    /// The id has no `<crate>.` prefix: it has one segment only.
    NoPrefix,
    /// A segment between two dots, or at either end, is empty
    /// (`a..b`, `a.`, `.a`); `at` is the byte where it is.
    EmptySegment {
        /// The byte offset of the empty segment.
        at: usize,
    },
    /// The byte at `at` is not allowed there.
    BadChar {
        /// The byte offset of the character.
        at: usize,
    },
}

impl fmt::Display for IdError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IdError::Empty => f.write_str("the id is empty"),
            IdError::NoPrefix => {
                f.write_str("the id has no `<crate>.` prefix (`<crate>.<subject>.<moment>`)")
            }
            IdError::EmptySegment { at } => write!(f, "the id has an empty segment at byte {at}"),
            IdError::BadChar { at } => write!(
                f,
                "the id has a character not allowed at byte {at}: the prefix is `[a-z][a-z0-9_-]*`, the other segments `[A-Za-z0-9_-]+`"
            ),
        }
    }
}

impl std::error::Error for IdError {}

/// Checks `id` against the naming rule of point and listener ids,
/// `<crate>.<subject>.<moment>` (the README's "Naming points"):
///
/// - at least two segments separated by dots, none empty;
/// - the first segment, the crate's prefix, is a lowercase ASCII letter
///   followed by lowercase ASCII letters, digits, `_` or `-`;
/// - the other segments are ASCII letters (either case), digits, `_` or
///   `-`.
///
/// A `const fn`, so a table checks its ids at compile time.
pub const fn check_id(id: &str) -> Result<(), IdError> {
    let b = id.as_bytes();
    if b.is_empty() {
        return Err(IdError::Empty);
    }
    let mut i = 0;
    let mut start = 0;
    let mut segments = 1;
    while i < b.len() {
        let c = b[i];
        if c == b'.' {
            if i == start {
                return Err(IdError::EmptySegment { at: i });
            }
            segments += 1;
            start = i + 1;
        } else if segments == 1 {
            let ok =
                c.is_ascii_lowercase() || (i > 0 && (c.is_ascii_digit() || c == b'_' || c == b'-'));
            if !ok {
                return Err(IdError::BadChar { at: i });
            }
        } else if !(c.is_ascii_alphanumeric() || c == b'_' || c == b'-') {
            return Err(IdError::BadChar { at: i });
        }
        i += 1;
    }
    if segments < 2 {
        return Err(IdError::NoPrefix);
    }
    if start == b.len() {
        return Err(IdError::EmptySegment { at: b.len() });
    }
    Ok(())
}

/// Panics with the error's message: a `const` caller turns it into a
/// compile error.
const fn refuse(error: IdError) -> ! {
    match error {
        IdError::Empty => panic!("invalid bus id: the id is empty"),
        IdError::NoPrefix => {
            panic!("invalid bus id: no `<crate>.` prefix (`<crate>.<subject>.<moment>`)")
        }
        IdError::EmptySegment { .. } => panic!("invalid bus id: an empty segment"),
        IdError::BadChar { .. } => panic!(
            "invalid bus id: a character not allowed (prefix `[a-z][a-z0-9_-]*`, then `[A-Za-z0-9_-]+`)"
        ),
    }
}

/// A point's id ([`Point::ID`](crate::Point::ID)), checked when made
/// against the naming rule ([`check_id`]): `<crate>.<subject>.<moment>`.
///
/// Made in a `const`, an invalid id is a compile error:
///
/// ```
/// use bus_contract::PointId;
/// const ID: PointId = PointId::new("doc.close");
/// assert_eq!(ID, "doc.close");
/// ```
///
/// ```compile_fail
/// use bus_contract::PointId;
/// const ID: PointId = PointId::new("close"); // no `<crate>.` prefix
/// ```
///
/// It reads as a `&str` ([`Deref`], [`PointId::as_str`], comparisons
/// with `str`). Newtype ids checked at construction follow
/// eventbus-contract (<https://github.com/mapseekai/eventbus-contract>);
/// the `const` constructor follows qubit-event-bus's `Topic::new_static`
/// (<https://github.com/qubit-ltd/rs-event-bus>).
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PointId(&'static str);

impl PointId {
    /// The id `id`, checked; panics when it breaks the naming rule, so a
    /// `const` with a bad id does not compile. [`PointId::try_new`]
    /// returns the error instead.
    pub const fn new(id: &'static str) -> PointId {
        match check_id(id) {
            Ok(()) => PointId(id),
            Err(e) => refuse(e),
        }
    }

    /// The id `id`, or why it breaks the naming rule.
    pub const fn try_new(id: &'static str) -> Result<PointId, IdError> {
        match check_id(id) {
            Ok(()) => Ok(PointId(id)),
            Err(e) => Err(e),
        }
    }

    /// The id as a string.
    pub const fn as_str(&self) -> &'static str {
        self.0
    }
}

impl Deref for PointId {
    type Target = str;
    fn deref(&self) -> &str {
        self.0
    }
}

impl AsRef<str> for PointId {
    fn as_ref(&self) -> &str {
        self.0
    }
}

impl fmt::Display for PointId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.0)
    }
}

impl fmt::Debug for PointId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(self.0, f)
    }
}

impl TryFrom<&'static str> for PointId {
    type Error = IdError;
    fn try_from(id: &'static str) -> Result<PointId, IdError> {
        PointId::try_new(id)
    }
}

impl From<PointId> for &'static str {
    fn from(id: PointId) -> &'static str {
        id.0
    }
}

impl From<PointId> for String {
    fn from(id: PointId) -> String {
        id.0.to_owned()
    }
}

impl PartialEq<str> for PointId {
    fn eq(&self, other: &str) -> bool {
        self.0 == other
    }
}

impl PartialEq<&str> for PointId {
    fn eq(&self, other: &&str) -> bool {
        self.0 == *other
    }
}

impl PartialEq<PointId> for str {
    fn eq(&self, other: &PointId) -> bool {
        self == other.0
    }
}

impl PartialEq<PointId> for &str {
    fn eq(&self, other: &PointId) -> bool {
        *self == other.0
    }
}

impl PartialEq<PointId> for String {
    fn eq(&self, other: &PointId) -> bool {
        self == other.0
    }
}

/// A listener's id, checked when made against the naming rule
/// ([`check_id`]): `<crate>.<what>` (`docs.autosave`), so the order
/// dump of a point shows who each listener belongs to.
///
/// [`ListenerId::new`] is `const` and panics on a bad id, so a `const`
/// with one does not compile; [`ListenerId::try_new`] checks an id made
/// at run time. The typed `listen` calls still take any
/// `impl Into<String>`, a `ListenerId` or a `&str`: the bus checks the id
/// when the listener registers and refuses a bad one
/// ([`Refused::BadId`](crate::Refused::BadId)).
///
/// ```
/// use bus_contract::ListenerId;
/// const SAVE: ListenerId = ListenerId::new("docs.autosave");
/// assert!(ListenerId::try_new(format!("docs.{}", "draft")).is_ok());
/// assert!(ListenerId::try_new("autosave").is_err());
/// assert_eq!(SAVE, "docs.autosave");
/// ```
///
/// Newtype ids checked at construction follow eventbus-contract
/// (<https://github.com/mapseekai/eventbus-contract>); the `const`
/// constructor follows qubit-event-bus's `Topic::new_static`
/// (<https://github.com/qubit-ltd/rs-event-bus>).
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ListenerId(Cow<'static, str>);

impl ListenerId {
    /// The id `id`, checked; panics when it breaks the naming rule, so a
    /// `const` with a bad id does not compile.
    pub const fn new(id: &'static str) -> ListenerId {
        match check_id(id) {
            Ok(()) => ListenerId(Cow::Borrowed(id)),
            Err(e) => refuse(e),
        }
    }

    /// The id `id`, made at run time, or why it breaks the naming rule.
    pub fn try_new(id: impl Into<String>) -> Result<ListenerId, IdError> {
        let id = id.into();
        check_id(&id)?;
        Ok(ListenerId(Cow::Owned(id)))
    }

    /// The id as a string.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Deref for ListenerId {
    type Target = str;
    fn deref(&self) -> &str {
        &self.0
    }
}

impl AsRef<str> for ListenerId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for ListenerId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl fmt::Debug for ListenerId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&*self.0, f)
    }
}

impl TryFrom<&str> for ListenerId {
    type Error = IdError;
    fn try_from(id: &str) -> Result<ListenerId, IdError> {
        ListenerId::try_new(id)
    }
}

impl TryFrom<String> for ListenerId {
    type Error = IdError;
    fn try_from(id: String) -> Result<ListenerId, IdError> {
        ListenerId::try_new(id)
    }
}

impl From<ListenerId> for String {
    fn from(id: ListenerId) -> String {
        id.0.into_owned()
    }
}

impl From<&ListenerId> for String {
    fn from(id: &ListenerId) -> String {
        id.0.clone().into_owned()
    }
}

impl PartialEq<str> for ListenerId {
    fn eq(&self, other: &str) -> bool {
        *self.0 == *other
    }
}

impl PartialEq<&str> for ListenerId {
    fn eq(&self, other: &&str) -> bool {
        *self.0 == **other
    }
}

impl PartialEq<ListenerId> for str {
    fn eq(&self, other: &ListenerId) -> bool {
        *self == *other.0
    }
}

impl PartialEq<ListenerId> for String {
    fn eq(&self, other: &ListenerId) -> bool {
        **self == *other.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_naming_rule() {
        for ok in [
            "doc.close",
            "stack.layout.changed",
            "bus.overBudget",
            "egui_pop.a-b.c_d9",
            "a1.x",
        ] {
            assert_eq!(check_id(ok), Ok(()), "{ok}");
        }
        assert_eq!(check_id(""), Err(IdError::Empty));
        assert_eq!(check_id("close"), Err(IdError::NoPrefix));
        assert_eq!(check_id("a..b"), Err(IdError::EmptySegment { at: 2 }));
        assert_eq!(check_id(".a"), Err(IdError::EmptySegment { at: 0 }));
        assert_eq!(check_id("a."), Err(IdError::EmptySegment { at: 2 }));
        assert_eq!(check_id("Doc.close"), Err(IdError::BadChar { at: 0 }));
        assert_eq!(check_id("1doc.close"), Err(IdError::BadChar { at: 0 }));
        assert_eq!(check_id("doc.cl ose"), Err(IdError::BadChar { at: 6 }));
        assert_eq!(check_id("doc.*"), Err(IdError::BadChar { at: 4 }));
        assert_eq!(check_id("doc.é"), Err(IdError::BadChar { at: 4 }));
    }

    #[test]
    fn newtypes_read_as_str() {
        const P: PointId = PointId::new("doc.close");
        assert_eq!(P, "doc.close");
        assert_eq!(&*P, "doc.close");
        assert_eq!(P.to_string(), "doc.close");
        assert!(PointId::try_new("close").is_err());
        let l = ListenerId::try_new("doc.save").unwrap();
        assert_eq!(String::from(l.clone()), "doc.save");
        assert_eq!(l, "doc.save");
    }

    #[test]
    #[should_panic(expected = "invalid bus id")]
    fn new_panics_at_run_time() {
        let id = String::from("x");
        let _ = ListenerId::new(Box::leak(id.into_boxed_str()));
    }
}