|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::Bound; |
| 19 | +use std::ops::RangeBounds; |
| 20 | + |
| 21 | +use crate::error::Error; |
| 22 | + |
| 23 | +pub(crate) fn ensure_serial_version_is(expected: u8, actual: u8) -> Result<(), Error> { |
| 24 | + if expected == actual { |
| 25 | + Ok(()) |
| 26 | + } else { |
| 27 | + Err(Error::deserial(format!( |
| 28 | + "unsupported serial version: expected {expected}, got {actual}" |
| 29 | + ))) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub(crate) fn ensure_preamble_longs_in(expected: &[u8], actual: u8) -> Result<(), Error> { |
| 34 | + if expected.contains(&actual) { |
| 35 | + Ok(()) |
| 36 | + } else { |
| 37 | + Err(Error::invalid_preamble_longs(expected, actual)) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +pub(crate) fn ensure_preamble_longs_in_range( |
| 42 | + expected: impl RangeBounds<u8>, |
| 43 | + actual: u8, |
| 44 | +) -> Result<(), Error> { |
| 45 | + let start = expected.start_bound(); |
| 46 | + let end = expected.end_bound(); |
| 47 | + if expected.contains(&actual) { |
| 48 | + Ok(()) |
| 49 | + } else { |
| 50 | + Err(Error::deserial(format!( |
| 51 | + "invalid preamble longs: expected {}, got {actual}", |
| 52 | + match (start, end) { |
| 53 | + (Bound::Included(a), Bound::Included(b)) => format!("[{a}, {b}]"), |
| 54 | + (Bound::Included(a), Bound::Excluded(b)) => format!("[{a}, {b})"), |
| 55 | + (Bound::Excluded(a), Bound::Included(b)) => format!("({a}, {b}]"), |
| 56 | + (Bound::Excluded(a), Bound::Excluded(b)) => format!("({a}, {b})"), |
| 57 | + (Bound::Unbounded, Bound::Included(b)) => format!("at most {b}"), |
| 58 | + (Bound::Unbounded, Bound::Excluded(b)) => format!("less than {b}"), |
| 59 | + (Bound::Included(a), Bound::Unbounded) => format!("at least {a}"), |
| 60 | + (Bound::Excluded(a), Bound::Unbounded) => format!("greater than {a}"), |
| 61 | + (Bound::Unbounded, Bound::Unbounded) => unreachable!("unbounded range"), |
| 62 | + } |
| 63 | + ))) |
| 64 | + } |
| 65 | +} |
0 commit comments