ruma_identifiers_validation/space_child_order.rs
1use crate::Error;
2
3/// Validate the `order` of an [`m.space.child`] event.
4///
5/// According to the specification, the order:
6///
7/// > Must consist of ASCII characters within the range `\x20` (space) and `\x7E` (~),
8/// > inclusive. Must not exceed 50 characters.
9///
10/// Returns `Ok(())` if the order passes validation, or an error if the order doesn't respect
11/// the rules from the spec, as it cannot be used for ordering.
12///
13/// [`m.space.child`]: https://spec.matrix.org/latest/client-server-api/#mspacechild
14pub fn validate(s: &str) -> Result<(), Error> {
15 if s.len() > 50 {
16 return Err(Error::MaximumLengthExceeded);
17 }
18
19 if !s.bytes().all(|byte| (b'\x20'..=b'\x7E').contains(&byte)) {
20 return Err(Error::InvalidCharacters);
21 }
22
23 Ok(())
24}