Skip to main content

flv_types/
partition.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum PartitionError {
5    InvalidSyntax(String)
6}
7
8
9impl fmt::Display for PartitionError {
10    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11        match self {
12            Self::InvalidSyntax(msg) => write!(f, "invalid partition syntax: {}", msg),
13        }
14    }
15}
16
17
18// returns a tuple (topic_name, idx)
19pub fn decompose_partition_name(partition_name: &str) -> Result<(String, i32), PartitionError> {
20    let dash_pos = partition_name.rfind('-');
21    if dash_pos.is_none() {
22        return Err(PartitionError::InvalidSyntax(partition_name.to_owned()));
23    }
24
25    let pos = dash_pos.unwrap();
26    if (pos + 1) >= partition_name.len() {
27        return Err(PartitionError::InvalidSyntax(partition_name.to_owned()));
28    }
29
30    let topic_name = &partition_name[..pos];
31    let idx_string = &partition_name[(pos + 1)..];
32    let idx = match idx_string.parse::<i32>() {
33        Ok(n) => n,
34        Err(_) => {
35            return Err(PartitionError::InvalidSyntax(partition_name.to_owned()));
36        }
37    };
38
39    Ok((topic_name.to_string(), idx))
40}
41
42pub fn create_partition_name(topic_name: &str, idx: &i32) -> String {
43    format!("{}-{}", topic_name.clone(), idx)
44}