Skip to main content

ps_ecc/reed_solomon/methods/
new.rs

1use crate::{RSConstructorError, ReedSolomon, MAX_PARITY};
2
3impl ReedSolomon {
4    /// Creates a new Reed-Solomon codec with the given error-correction
5    /// capability.
6    /// # Errors
7    /// - [`RSConstructorError::ParityTooHigh`] is returned if `parity`
8    ///   exceeds [`MAX_PARITY`].
9    pub const fn new(parity: u8) -> Result<Self, RSConstructorError> {
10        use RSConstructorError::ParityTooHigh;
11
12        if parity > MAX_PARITY {
13            return Err(ParityTooHigh);
14        }
15
16        let codec = Self { parity };
17
18        Ok(codec)
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use crate::{ReedSolomon, MAX_PARITY};
25
26    #[test]
27    fn test_new() {
28        assert!(ReedSolomon::new(0).is_ok());
29        assert!(ReedSolomon::new(10).is_ok());
30        assert!(ReedSolomon::new(MAX_PARITY).is_ok());
31        assert!(ReedSolomon::new(MAX_PARITY + 1).is_err());
32    }
33}