Skip to content

Commit a88ae91

Browse files
authored
Merge pull request #107 from silicon-heaven/journalentry-userid-string
journal: Change JournalEntry::user_id type to String
2 parents afc6aee + 2897a80 commit a88ae91

7 files changed

Lines changed: 24 additions & 31 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "shvrpc"
33
description = "Rust implementation of the SHV-RPC protocol"
44
license = "MIT"
55
repository = "https://github.com/silicon-heaven/libshvrpc-rs"
6-
version = "15.0.4"
6+
version = "16.0.0"
77
edition = "2024"
88

99
[dependencies]

src/framerw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub trait FrameReader {
103103
async fn receive_frame(&mut self) -> Result<RpcFrame, ReceiveFrameError> {
104104
match self.try_receive_frame().await {
105105
Ok(frame) => {
106-
log!(target: "RpcMsg", Level::Debug, "R==> {} {}", format_peer_id(self.peer_id()), &frame);
106+
log!(target: "RpcMsg", Level::Debug, "R==> {} {}", format_peer_id(self.peer_id()), frame);
107107
Ok(frame)
108108
}
109109
Err(err) => Err(err),
@@ -147,7 +147,7 @@ pub trait FrameWriter {
147147
}
148148
async fn send_frame_impl(&mut self, frame: RpcFrame) -> crate::Result<()>;
149149
async fn send_frame(&mut self, frame: RpcFrame) -> crate::Result<()> {
150-
log!(target: "RpcMsg", Level::Debug, "S<== {} {}", format_peer_id(self.peer_id()), &frame.to_rpcmesage().map_or_else(|_| frame.to_string(), |rpc_msg| rpc_msg.to_string()));
150+
log!(target: "RpcMsg", Level::Debug, "S<== {} {}", format_peer_id(self.peer_id()), frame.to_rpcmesage().map_or_else(|_| frame.to_string(), |rpc_msg| rpc_msg.to_string()));
151151
self.send_frame_impl(frame).await
152152
}
153153
async fn send_message(&mut self, msg: RpcMessage) -> crate::Result<()> {

src/journalentry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct JournalEntry {
99
pub value: RpcValue,
1010
pub access_level: i32,
1111
pub short_time: i32,
12-
pub user_id: RpcValue,
12+
pub user_id: String,
1313
pub repeat: bool,
1414
pub provisional: bool,
1515
}

src/journalrw.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,7 @@ fn parse_journal_entry_log2(line: &str) -> Result<JournalEntry, Box<dyn Error +
3232
let short_time = parts_iter.next().unwrap_or_default().parse().unwrap_or(-1);
3333
let domain = parts_iter.next();
3434
let value_flags = ValueFlags::from_bits_retain(parts_iter.next().unwrap_or_default().parse().unwrap_or(0));
35-
let user_id = parts_iter
36-
.next()
37-
.map_or_else(|| Ok(RpcValue::null()), |user_id| if user_id.is_empty() {
38-
Ok(RpcValue::null())
39-
} else {
40-
RpcValue::from_cpon(user_id)
41-
.map_err(|err| format!("Cannot parse user_id from CPON: `{user_id}` on line: {line}, error: {err}"))
42-
})?;
35+
let user_id = parts_iter.next().map_or_else(String::new, String::from);
4336

4437
Ok(JournalEntry {
4538
epoch_msec,
@@ -128,7 +121,7 @@ where
128121
}
129122
value_flags.bits().to_string()
130123
},
131-
if entry.user_id.is_null() { "".into() } else { entry.user_id.to_cpon() },
124+
entry.user_id.clone(),
132125
].join(JOURNAL_ENTRIES_SEPARATOR) + "\n";
133126
self.writer.write_all(line.as_bytes()).await?;
134127
self.writer.flush().await
@@ -179,8 +172,8 @@ pub fn journal_entry_to_log3_imap(entry: JournalEntry) -> shvproto::IMap {
179172
if entry.access_level != AccessLevel::Read as _ {
180173
imap.insert(Log3Key::AccessLevel as _, entry.access_level.into());
181174
}
182-
if !entry.user_id.is_null() {
183-
imap.insert(Log3Key::UserId as _, entry.user_id);
175+
if !entry.user_id.is_empty() {
176+
imap.insert(Log3Key::UserId as _, entry.user_id.into());
184177
}
185178
if entry.repeat {
186179
imap.insert(Log3Key::Repeat as _, entry.repeat.into());
@@ -263,7 +256,7 @@ fn parse_journal_entry_log3(line: impl AsRef<str>) -> Result<JournalEntry, Box<d
263256
.map_or(AccessLevel::Read as _, RpcValue::as_i32);
264257
let user_id = entry
265258
.get(&(Log3Key::UserId as _))
266-
.map_or_else(RpcValue::null, RpcValue::clone);
259+
.map_or_else(String::new, |rv| rv.as_str().into());
267260
let repeat = entry
268261
.get(&(Log3Key::Repeat as _))
269262
.is_some_and(RpcValue::as_bool);
@@ -409,7 +402,7 @@ fn rpcvalue_to_journal_entry(entry: &RpcValue, paths_dict: &BTreeMap<i32, String
409402
};
410403
let value_flags = ValueFlags::from_bits_retain(value_flags);
411404

412-
let user_id = row.next().map_or_else(RpcValue::null, RpcValue::clone);
405+
let user_id = row.next().map_or_else(String::new, |rv| rv.as_str().into());
413406

414407
Ok(JournalEntry {
415408
epoch_msec: timestamp.epoch_msec(),
@@ -864,7 +857,7 @@ mod tests {
864857
value: shvproto::make_map!("a" => 1, "b" => 2).into(),
865858
access_level: AccessLevel::Read as _,
866859
short_time: 123,
867-
user_id: ().into(),
860+
user_id: String::default(),
868861
repeat: true,
869862
provisional: true,
870863
},
@@ -876,7 +869,7 @@ mod tests {
876869
value: shvproto::make_map!("a" => 1, "b" => 2).into(),
877870
access_level: AccessLevel::Read as _,
878871
short_time: -1,
879-
user_id: shvproto::make_map!("userName" => "abc", "agent" => "xyz").into(),
872+
user_id: shvproto::RpcValue::from(shvproto::make_map!("userName" => "abc", "agent" => "xyz")).to_cpon(),
880873
repeat: false,
881874
provisional: true,
882875
},
@@ -923,7 +916,7 @@ mod tests {
923916
value: shvproto::make_map!("a" => 1, "b" => 2).into(),
924917
access_level: AccessLevel::Read as _,
925918
short_time: -1,
926-
user_id: ().into(),
919+
user_id: String::default(),
927920
repeat: true,
928921
provisional: true,
929922
},
@@ -935,7 +928,7 @@ mod tests {
935928
value: shvproto::make_map!("a" => 1, "b" => 2).into(),
936929
access_level: AccessLevel::Read as _,
937930
short_time: -1,
938-
user_id: shvproto::make_map!("userName" => "abc", "agent" => "xyz").into(),
931+
user_id: shvproto::RpcValue::from(shvproto::make_map!("userName" => "abc", "agent" => "xyz")).to_cpon(),
939932
repeat: false,
940933
provisional: true,
941934
},
@@ -1048,7 +1041,7 @@ mod tests {
10481041
fn parse_journal_entry_log2_variants() {
10491042
// All fields present
10501043
assert_eq!(parse_journal_entry_log2(
1051-
"2025-04-28T11:51:14.300Z\t\tsystem/status\t2u\t\tchng\t1").unwrap(),
1044+
"2025-04-28T11:51:14.300Z\t\tsystem/status\t2u\t\tchng\t1\tuser").unwrap(),
10521045
JournalEntry {
10531046
epoch_msec: shvproto::DateTime::from_iso_str("2025-04-28T11:51:14.300Z").unwrap().epoch_msec(),
10541047
path: "system/status".into(),
@@ -1057,7 +1050,7 @@ mod tests {
10571050
value: 2u64.into(),
10581051
access_level: AccessLevel::Read as i32,
10591052
short_time: -1,
1060-
user_id: ().into(),
1053+
user_id: "user".into(),
10611054
repeat: true,
10621055
provisional: false,
10631056
}
@@ -1073,7 +1066,7 @@ mod tests {
10731066
value: 2u64.into(),
10741067
access_level: AccessLevel::Read as i32,
10751068
short_time: -1,
1076-
user_id: ().into(),
1069+
user_id: String::default(),
10771070
repeat: true,
10781071
provisional: false,
10791072
}

src/rpcframe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl RpcFrame {
7575
return Ok(RpcFrame::new_reset_session())
7676
}
7777
if *proto != Protocol::ChainPack as u8 {
78-
return Err(anyhow!("Invalid protocol type received {proto:#02x}."));
78+
return Err(anyhow!("Invalid protocol type received {proto:#x}."));
7979
}
8080
let (meta, meta_len) = {
8181
let mut buffrd = BufReader::new(rest);

src/serialrw.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl<R: AsyncRead + Unpin + Send> SerialFrameReader<R> {
7878
EESC => ESC,
7979
b => {
8080
return Err(ReceiveFrameError::FramingError(format!(
81-
"Framing error, invalid escape byte {b:#02x}"
81+
"Framing error, invalid escape byte {b:#x}"
8282
)));
8383
}
8484
};
@@ -484,7 +484,7 @@ mod test {
484484
debug!("hex: {chunks:?}");
485485
let mut rd = SerialFrameReader::new(crate::framerw::test::Chunks { chunks }).with_crc_check(true);
486486
let frame = rd.receive_frame().await;
487-
debug!("frame: {:?}", &frame);
487+
debug!("frame: {frame:?}");
488488
assert!(frame.is_ok());
489489
};
490490
}
@@ -514,7 +514,7 @@ mod test {
514514
let mut rd = SerialFrameReader::new(buffrd).with_crc_check(with_crc);
515515
for _ in 0 .. 3 {
516516
let frame = rd.receive_frame().await;
517-
debug!("frame: {:?}", &frame);
517+
debug!("frame: {frame:?}");
518518
assert!(frame.is_ok());
519519
}
520520
}
@@ -543,7 +543,7 @@ mod test {
543543
{
544544
// should read first message with size equal to frame size limit
545545
let frame = rd.receive_frame().await.unwrap();
546-
debug!("frame: {:?}", &frame);
546+
debug!("frame: {frame:?}");
547547
assert_eq!(frame.to_rpcmesage().unwrap().param().unwrap().as_int(), 161);
548548
}
549549
}
@@ -555,7 +555,7 @@ mod test {
555555
{
556556
assert!(matches!(rd.receive_frame().await.unwrap_err(), ReceiveFrameError::FrameTooLarge(..)));
557557
let frame = rd.receive_frame().await.unwrap();
558-
debug!("frame: {:?}", &frame);
558+
debug!("frame: {frame:?}");
559559
assert_eq!(frame.to_rpcmesage().unwrap().param().unwrap().as_int(), 1);
560560
}
561561
}

src/streamrw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ mod test {
208208
RpcMessage::new_request("foo/bar", "baz2").with_param(&[0_u8; 128][..]),
209209
] {
210210
let frame = msg.to_frame().unwrap();
211-
debug!("frame: {}", &frame);
211+
debug!("frame: {frame}");
212212

213213
let buff = send_frame_to_vector(&frame).await;
214214
debug!("msg: {msg}");

0 commit comments

Comments
 (0)