|
| 1 | +use std::*; |
| 2 | + |
| 3 | + |
| 4 | +// COPY -> PASTE to https://play.rust-lang.org/ for check parser prototype |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +//use std::intrinsics::type_name::*; |
| 9 | +//use std::num::ParseIntError; |
| 10 | +//use rust_decimal::Decimal; |
| 11 | + |
| 12 | + |
| 13 | +struct MetricDataTypes {} |
| 14 | + |
| 15 | +impl MetricDataTypes { |
| 16 | + const INTEGER: &str = "i"; |
| 17 | + //const BOOL: &str = "b"; |
| 18 | + //const DECIMAL: &str = "d"; |
| 19 | + //const TEXT: &str = "t"; |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +struct ItemType {} |
| 24 | +impl ItemType { |
| 25 | + const TIME_UNIX_MILIS: &str = "t"; |
| 26 | + const DEVICE_ID: &str = "d"; |
| 27 | + //const METRIC: &str = "m"; |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug)] |
| 31 | +enum MetricValueType { |
| 32 | + IntegerItemType(i64), |
| 33 | + //BoolItemType(bool), |
| 34 | + //DecimalItemType(i64), |
| 35 | + //TextItemType(String), |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug)] |
| 39 | +enum ItemTypeEnum { |
| 40 | + TimeUnixMilis(u64), |
| 41 | + DeviceId(String), |
| 42 | +} |
| 43 | + |
| 44 | +//#![feature(core_intrinsics)] |
| 45 | +//fn print_type_of<T>(_: &T) { |
| 46 | +// println!("{}", unsafe { std::intrinsics::type_name::<T>() }); |
| 47 | +//} |
| 48 | + |
| 49 | +fn main() { |
| 50 | + let item_parts: Vec<&str> = "t|3900237526042,d|device_name_001,m|val_water_level1=i:42" |
| 51 | + .split(',') |
| 52 | + .collect(); |
| 53 | + |
| 54 | + for part in item_parts { |
| 55 | + println!("part: {}", part); |
| 56 | + let item_part: Vec<&str> = part.split('|').collect(); |
| 57 | + println!("item_part: {:?}", item_part); |
| 58 | + let item_type_tmp: &str = item_part[0]; |
| 59 | + let item_type_metric: &str = "m"; |
| 60 | + if item_type_tmp.eq(item_type_metric) { |
| 61 | + println!("\tmetric: {}", item_part[1]); |
| 62 | + let metric_parts: Vec<&str> = item_part[1].split('=').collect(); |
| 63 | + println!("\tmetric_parts: {:?}", metric_parts); |
| 64 | + //let metric_name: String = item_parts[0].to_string(); |
| 65 | + //println!("metric_name: {}", metric_name); |
| 66 | + let metric_parts_values: Vec<&str> = metric_parts[1].split(':').collect(); |
| 67 | + println!("\t\tmetric_parts_values: {:?}", metric_parts_values); |
| 68 | + match metric_parts_values[0] { |
| 69 | + MetricDataTypes::INTEGER => { |
| 70 | + //value: Result<i64, _> = metric_parts_values[0].parse(); |
| 71 | + //let value: Option<i64> = metric_parts_values[0].parse(); |
| 72 | + //let value: Result<i64, ParseIntError> = metric_parts_values[0].parse::<i64>(); |
| 73 | + let value = match metric_parts_values[1].parse::<i64>() { |
| 74 | + Ok(number) => number, |
| 75 | + Err(_) => todo!(), |
| 76 | + }; |
| 77 | + println!( |
| 78 | + "\t\t\tIntegerItemType: {:?}", |
| 79 | + MetricValueType::IntegerItemType(value) |
| 80 | + ) |
| 81 | + //MetricValueType::IntegerItemType(value); |
| 82 | + } |
| 83 | + _ => println!("\t\t\tother"), |
| 84 | + } |
| 85 | + } else { |
| 86 | + match item_part[0] { |
| 87 | + ItemType::TIME_UNIX_MILIS => { |
| 88 | + let value = match item_part[1].parse::<u64>() { |
| 89 | + Ok(number) => number, |
| 90 | + Err(_) => todo!(), |
| 91 | + }; |
| 92 | + println!( |
| 93 | + "\t\t\tTIME_UNIX_MILIS: {:?}", |
| 94 | + ItemTypeEnum::TimeUnixMilis(value) |
| 95 | + ) |
| 96 | + } |
| 97 | + ItemType::DEVICE_ID => { |
| 98 | + |
| 99 | + println!("\t\t\tDEVICE_ID: {:?}", ItemTypeEnum::DeviceId(String::from(item_part[1]))) |
| 100 | + } |
| 101 | + val => { |
| 102 | + println!("\t\t\t OTHER: {:?}", val); |
| 103 | + //print_type_of(val) |
| 104 | + } |
| 105 | + } |
| 106 | + println!("\t\tcontext: {}", item_part[1]) |
| 107 | + } |
| 108 | + /*match item_part.split('=').collect() { |
| 109 | + [item, details] => { |
| 110 | + println!("{:?}", item); |
| 111 | + println!("{:?}", details); |
| 112 | + } |
| 113 | + _ => println!("rest"), |
| 114 | + }*/ |
| 115 | + } |
| 116 | + //assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]); |
| 117 | + //println!("{:?}", v); |
| 118 | + //MetricValueType::IntegerItemType(0) |
| 119 | +} |
0 commit comments