Skip to main content

coqui_access_kit/
record.rs

1//! Bounds on the fields of a record a provider keeps.
2
3use coqui_access::Limit;
4use coqui_builder::Describe;
5
6use crate::table::check_field;
7
8/// How large a record's fields may be, each a [`Limit`] the caller can change or lift.
9///
10/// By default at most 256 groups, 256 bytes in an id, a name or a group, 320 in an email
11/// address and 1 024 in a display name. They bound memory and what a log line can carry, not
12/// trust. Every field is also refused when empty or holding a control character
13/// ([`check_field`]).
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Describe)]
15#[non_exhaustive]
16pub struct RecordLimits {
17    /// Groups one record holds.
18    #[map(default = Limit::at(256))]
19    pub groups: Limit,
20    /// Bytes in an id, a name or a group.
21    #[map(default = Limit::at(256))]
22    pub field_bytes: Limit,
23    /// Bytes in an email address.
24    #[map(default = Limit::at(320))]
25    pub email_bytes: Limit,
26    /// Bytes in a display name.
27    #[map(default = Limit::at(1024))]
28    pub display_name_bytes: Limit,
29}
30
31impl Default for RecordLimits {
32    fn default() -> Self {
33        Self::DEFAULT
34    }
35}
36
37impl RecordLimits {
38    /// The defaults: 256 groups, 256-byte fields, 320-byte email addresses, 1 024-byte
39    /// display names.
40    pub const DEFAULT: Self = Self {
41        groups: Limit::at(256),
42        field_bytes: Limit::at(256),
43        email_bytes: Limit::at(320),
44        display_name_bytes: Limit::at(1024),
45    };
46
47    /// No bound at all.
48    pub const UNBOUNDED: Self = Self {
49        groups: Limit::UNBOUNDED,
50        field_bytes: Limit::UNBOUNDED,
51        email_bytes: Limit::UNBOUNDED,
52        display_name_bytes: Limit::UNBOUNDED,
53    };
54
55    /// These limits with `groups`.
56    #[must_use]
57    pub const fn groups(mut self, groups: Limit) -> Self {
58        self.groups = groups;
59        self
60    }
61
62    /// These limits with `field_bytes`.
63    #[must_use]
64    pub const fn field_bytes(mut self, bytes: Limit) -> Self {
65        self.field_bytes = bytes;
66        self
67    }
68
69    /// These limits with `email_bytes`.
70    #[must_use]
71    pub const fn email_bytes(mut self, bytes: Limit) -> Self {
72        self.email_bytes = bytes;
73        self
74    }
75
76    /// These limits with `display_name_bytes`.
77    #[must_use]
78    pub const fn display_name_bytes(mut self, bytes: Limit) -> Self {
79        self.display_name_bytes = bytes;
80        self
81    }
82
83    /// Whether `value` may be an id, a name or a group.
84    pub fn allows_field(&self, value: &str) -> bool {
85        check_field(value, self.field_bytes.or_max())
86    }
87
88    /// Whether a record may hold these groups: not too many, each a field.
89    pub fn allows_groups(&self, groups: &[String]) -> bool {
90        self.groups.allows(groups.len()) && groups.iter().all(|group| self.allows_field(group))
91    }
92
93    /// Whether `value` may be an email address.
94    pub fn allows_email(&self, value: &str) -> bool {
95        check_field(value, self.email_bytes.or_max())
96    }
97
98    /// Whether `value` may be a display name.
99    pub fn allows_display_name(&self, value: &str) -> bool {
100        check_field(value, self.display_name_bytes.or_max())
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use coqui_builder::Partial;
107
108    use super::*;
109
110    #[test]
111    fn record_limits_default_bound_each_field_and_lift_with_unbounded() {
112        let limits = RecordLimits::default();
113        assert!(limits.allows_field(&"x".repeat(256)) && !limits.allows_field(&"x".repeat(257)));
114        assert!(!limits.allows_field("") && !limits.allows_field("a\nb"));
115        let many: Vec<String> = (0..257).map(|n| format!("g{n}")).collect();
116        assert!(!limits.allows_groups(&many) && limits.allows_groups(&many[..256]));
117        assert!(limits.allows_email(&"e".repeat(320)) && !limits.allows_email(&"e".repeat(321)));
118        assert!(limits.allows_display_name(&"d".repeat(1024)));
119        let lifted = RecordLimits::UNBOUNDED;
120        assert!(lifted.allows_groups(&many) && lifted.allows_field(&"x".repeat(100_000)));
121        assert!(!lifted.allows_field(""), "an empty field is never one");
122        assert!(
123            !RecordLimits::DEFAULT
124                .groups(Limit::at(1))
125                .allows_groups(&many[..2])
126        );
127    }
128
129    #[test]
130    fn record_limits_read_from_configuration_with_defaults() {
131        assert_eq!(
132            Partial::<RecordLimits>::new().build().unwrap(),
133            RecordLimits::DEFAULT
134        );
135        let mut tuned = Partial::<RecordLimits>::new();
136        tuned.set("groups", "4").unwrap();
137        tuned.set("field_bytes", "none").unwrap();
138        let read = tuned.build().unwrap();
139        assert_eq!(
140            (read.groups, read.field_bytes),
141            (Limit::at(4), Limit::UNBOUNDED)
142        );
143        assert_eq!(read.email_bytes, Limit::at(320));
144    }
145}