Skip to content

Commit fcf2ab5

Browse files
authored
Merge pull request #106 from silicon-heaven/fix-find-path
Fix find_path, add --find-path to cp2cp
2 parents 2eca270 + 7ffefc8 commit fcf2ab5

4 files changed

Lines changed: 72 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name = "shvproto"
55
description = "Rust implementation of the SHV protocol"
66
license = "MIT"
77
repository = "https://github.com/silicon-heaven/libshvproto-rs"
8-
version = "6.1.1"
8+
version = "6.1.2"
99
edition = "2024"
1010

1111
[dependencies]

src/bin/cp2cp.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,32 @@ use jaq_all::{jaq_core::{Ctx, Vars, data::JustLut}, jaq_std};
2121
struct Cli {
2222
#[arg(short, long, help = "Cpon indentation string")]
2323
indent: Option<String>,
24+
2425
#[arg(short, long, help = "Do not create oneliners in Cpon indented string output")]
2526
no_oneliners: bool,
27+
2628
#[arg(long = "ip", help = "Cpon input")]
2729
cpon_input: bool,
30+
2831
#[arg(long = "oc", help = "ChainPack output")]
2932
chainpack_output: bool,
33+
3034
/// Expect input data in RPC block transport format, https://silicon-heaven.github.io/shv-doc/rpctransportlayer/stream.html
3135
#[arg(long)]
3236
chainpack_rpc_block: bool,
37+
38+
/// Find this path in the input before processing
39+
#[arg(long)]
40+
find_path: Option<String>,
41+
3342
/// Verbose mode (module, .)
3443
#[arg(short = 'v', long = "verbose")]
3544
verbose: Option<String>,
45+
3646
/// File to process
3747
#[arg(value_name = "FILE")]
3848
file: Option<PathBuf>,
49+
3950
/// Run cq with this filter.
4051
#[cfg(feature = "cq")]
4152
#[arg(long = "cq")]
@@ -152,6 +163,19 @@ fn main() {
152163
process_chainpack_rpc_block_and_exit(reader)
153164
}
154165

166+
if let Some(find_path) = opts.find_path {
167+
let mut rd: Box<dyn Reader + '_> = if opts.cpon_input {
168+
Box::new(CponReader::new(&mut reader))
169+
} else {
170+
Box::new(ChainPackReader::new(&mut reader))
171+
};
172+
let path = find_path.split('/').collect::<Vec<_>>();
173+
if let Err(e) = rd.find_path(&path) {
174+
eprintln!("Path not found: {e:?}");
175+
process::exit(CODE_READ_ERROR);
176+
}
177+
}
178+
155179
let cpon_output = !opts.chainpack_output;
156180
let use_in_memory_parser = cpon_output && !opts.no_oneliners;
157181
#[cfg(feature = "cq")]

src/chainpack.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,3 +1126,49 @@ fn test_find_path_imap_nested() {
11261126
rd.find_path(&["0", "1", "2"]).unwrap();
11271127
assert_eq!(rd.read().unwrap(), "baz".into());
11281128
}
1129+
1130+
#[test]
1131+
fn test_find_path_chainpack() {
1132+
let cpon = r#"
1133+
i{
1134+
10:2,
1135+
20:{"timeZone":"Europe/Prague"},
1136+
30:i{
1137+
10:i{
1138+
0:20708,
1139+
1:141,
1140+
26:0.,
1141+
35:5.4
1142+
},
1143+
20:{"comment":"", "commissioner":"", "tram_ids":""},
1144+
30:{
1145+
".app:version":<1:5, 8:d"2026-02-19T22:08:33.130Z">"0.18.0",
1146+
".broker/currentClient:info":<1:5, 8:d"2026-02-19T22:08:33.057Z">{
1147+
"clientId":9,
1148+
"role":"default",
1149+
"subscriptions":{"**:*:chng":null},
1150+
"userName":"test"
1151+
},
1152+
".device:name":<1:5, 8:d"2026-02-19T22:08:33.269Z">"BRCg2",
1153+
".device:serialNumber":<1:5, 8:d"2026-02-19T22:08:33.329Z">"IDG24027",
1154+
".device:version":<1:5, 8:d"2026-02-19T22:08:33.396Z">"M2D1",
1155+
"fwStable":<1:5, 8:d"2026-02-19T22:08:34.680Z">true,
1156+
"location":<1:5, 8:d"2026-02-19T22:08:33.521Z">i{
1157+
0:"CZE",
1158+
1:"Prague",
1159+
2:"Kobylisy",
1160+
3:"Obratiště RTC",
1161+
4:[0, 42],
1162+
5:"Europe/Prague"
1163+
}
1164+
}
1165+
}
1166+
}
1167+
"#;
1168+
let rv = RpcValue::from_cpon(cpon).unwrap();
1169+
let chpk = rv.to_chainpack();
1170+
let mut data_slice = &chpk[..];
1171+
let mut rd = ChainPackReader::new(&mut data_slice);
1172+
rd.find_path(&["30", "30", "location", "4", "1"]).unwrap();
1173+
assert_eq!(rd.read().unwrap(), 42.into());
1174+
}

src/reader.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ pub trait Reader {
193193
}
194194
break;
195195
}
196+
self.skip()?;
196197
}
197198
}
198199
_ => return Err(make_error("Not container".into()))

0 commit comments

Comments
 (0)