Skip to content

Commit a8a3ab1

Browse files
committed
add parser prototype for IoText message in RUST
1 parent 92e26ee commit a8a3ab1

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

src/test_parse.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ struct MetricDataTypes {}
1414

1515
impl MetricDataTypes {
1616
const INTEGER: &str = "i";
17-
//const BOOL: &str = "b";
17+
const BOOL: &str = "b";
1818
//const DECIMAL: &str = "d";
19-
//const TEXT: &str = "t";
19+
const TEXT: &str = "t";
2020
}
2121

2222

2323
struct ItemType {}
2424
impl ItemType {
2525
const TIME_UNIX_MILIS: &str = "t";
2626
const DEVICE_ID: &str = "d";
27-
//const METRIC: &str = "m";
27+
const METRIC: &str = "m";
2828
}
2929

3030
#[derive(Debug)]
3131
enum MetricValueType {
3232
IntegerItemType(i64),
33-
//BoolItemType(bool),
33+
BoolItemType(bool),
3434
//DecimalItemType(i64),
35-
//TextItemType(String),
35+
TextItemType(String),
3636
}
3737

3838
#[derive(Debug)]
@@ -41,13 +41,10 @@ enum ItemTypeEnum {
4141
DeviceId(String),
4242
}
4343

44-
//#![feature(core_intrinsics)]
45-
//fn print_type_of<T>(_: &T) {
46-
// println!("{}", unsafe { std::intrinsics::type_name::<T>() });
47-
//}
44+
const MSG_EXAMPLE: &str = "t|3900237526042,d|device_name_001,m|val_water_level1=i:42,m|light_on=b:1,m|bulb_on=b:0,m|msg_machine_01=t:hello";
4845

4946
fn main() {
50-
let item_parts: Vec<&str> = "t|3900237526042,d|device_name_001,m|val_water_level1=i:42"
47+
let item_parts: Vec<&str> = MSG_EXAMPLE
5148
.split(',')
5249
.collect();
5350

@@ -67,9 +64,6 @@ fn main() {
6764
println!("\t\tmetric_parts_values: {:?}", metric_parts_values);
6865
match metric_parts_values[0] {
6966
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>();
7367
let value = match metric_parts_values[1].parse::<i64>() {
7468
Ok(number) => number,
7569
Err(_) => todo!(),
@@ -78,7 +72,23 @@ fn main() {
7872
"\t\t\tIntegerItemType: {:?}",
7973
MetricValueType::IntegerItemType(value)
8074
)
81-
//MetricValueType::IntegerItemType(value);
75+
}
76+
MetricDataTypes::BOOL => {
77+
let value = match metric_parts_values[1] {
78+
"1" => true,
79+
"0" => false,
80+
_ => todo!(),
81+
};
82+
println!(
83+
"\t\t\tBoolItemType: {:?}",
84+
MetricValueType::BoolItemType(value)
85+
)
86+
}
87+
MetricDataTypes::TEXT => {
88+
println!(
89+
"\t\t\tBoolItemType: {:?}",
90+
MetricValueType::TextItemType(metric_parts_values[1].to_string())
91+
)
8292
}
8393
_ => println!("\t\t\tother"),
8494
}
@@ -95,9 +105,11 @@ fn main() {
95105
)
96106
}
97107
ItemType::DEVICE_ID => {
98-
99108
println!("\t\t\tDEVICE_ID: {:?}", ItemTypeEnum::DeviceId(String::from(item_part[1])))
100109
}
110+
ItemType::METRIC => {
111+
println!("\t\t\tMETRIC: {}", String::from(item_part[1]))
112+
}
101113
val => {
102114
println!("\t\t\t OTHER: {:?}", val);
103115
//print_type_of(val)
@@ -136,5 +148,22 @@ item_part: ["m", "val_water_level1=i:42"]
136148
metric_parts: ["val_water_level1", "i:42"]
137149
metric_parts_values: ["i", "42"]
138150
IntegerItemType: IntegerItemType(42)
139-
151+
part: m|light_on=b:1
152+
item_part: ["m", "light_on=b:1"]
153+
metric: light_on=b:1
154+
metric_parts: ["light_on", "b:1"]
155+
metric_parts_values: ["b", "1"]
156+
BoolItemType: BoolItemType(true)
157+
part: m|bulb_on=b:0
158+
item_part: ["m", "bulb_on=b:0"]
159+
metric: bulb_on=b:0
160+
metric_parts: ["bulb_on", "b:0"]
161+
metric_parts_values: ["b", "0"]
162+
BoolItemType: BoolItemType(false)
163+
part: m|msg_machine_01=t:hello
164+
item_part: ["m", "msg_machine_01=t:hello"]
165+
metric: msg_machine_01=t:hello
166+
metric_parts: ["msg_machine_01", "t:hello"]
167+
metric_parts_values: ["t", "hello"]
168+
BoolItemType: TextItemType("hello")
140169
*/

0 commit comments

Comments
 (0)