clickhouse_data_type/
fixed_string.rs

1use core::num::ParseIntError;
2
3use pest::iterators::Pairs;
4
5use crate::{type_name_parser::Rule, ParseError};
6
7const N_MIN: usize = 1;
8
9#[derive(PartialEq, Eq, Debug, Clone)]
10pub struct FixedStringN(pub usize);
11impl TryFrom<&str> for FixedStringN {
12    type Error = ParseError;
13    fn try_from(s: &str) -> Result<Self, Self::Error> {
14        let n: usize = s
15            .parse()
16            .map_err(|err: ParseIntError| ParseError::ValueInvalid(err.to_string()))?;
17
18        if n < N_MIN {
19            return Err(ParseError::ValueInvalid(
20                "invalid fixedstring n".to_string(),
21            ));
22        }
23
24        Ok(Self(n))
25    }
26}
27
28pub(crate) fn get_n(mut fixed_string_pairs: Pairs<'_, Rule>) -> Result<FixedStringN, ParseError> {
29    let n_pair = fixed_string_pairs.next().ok_or(ParseError::Unknown)?;
30
31    let n = FixedStringN::try_from(n_pair.as_str())?;
32
33    Ok(n)
34}