br-serial 0.0.1

This is an COM and Serial Port
Documentation
use json::{array, JsonValue, object};

pub struct Protocol {}

impl Protocol {
    pub fn new(data: Vec<u8>) -> JsonValue {
        let res = mode1(data.clone());
        if !res.is_empty() {
            return res;
        }
        let res = mode2(data.clone());
        if !res.is_empty() {
            return res;
        }
        return res;
    }
}

fn mode1(data: Vec<u8>) -> JsonValue {
    let key = format!("{}{}", data[0], data[1]);
    if key == "" {
        return json::Null;
    }
    let text = data[2..data.len() - 1].escape_ascii().to_string();
    let value = match text.parse::<f64>() {
        Ok(e) => e,
        Err(_) => {
            return json::Null;
        }
    };
    return object! {"id":key,data_type:"value",data_mode:"mode1".to_string(),value:value};
}

fn mode2(data: Vec<u8>) -> JsonValue {
    let mut num = 0;
    let index = data.len() / 11;
    let mut list = array![];
    for _ in 0..index {
        let t = &data[num..num + 11];
        let res = mode1(t.to_vec().clone());
        if res.is_empty() {
            return json::Null;
        }
        list.push(res.clone()).unwrap();
        num += 11;
    }
    if list.len() > 0 {
        return object! {"id":list[0]["id"].clone(),data_type:"values",data_mode:"mode2".to_string(),value:list.clone()};
    }
    return json::Null;
}