Skip to content

Commit 8a7321a

Browse files
committed
Refactor Confirmation: replace custom FromStr implementation with strum
1 parent 52cfd04 commit 8a7321a

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

configs/server.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ message_expiry = "none"
464464
# Controls how the system waits for file write operations to complete.
465465
# Possible values:
466466
# - "wait": waits for the file operation to complete before proceeding.
467-
# - "nowait": proceeds without waiting for the file operation to finish, potentially increasing performance but at the cost of durability.
467+
# - "no_wait": proceeds without waiting for the file operation to finish, potentially increasing performance but at the cost of durability.
468468
server_confirmation = "wait"
469469

470470
# Configures whether expired segments are archived (boolean) or just deleted without archiving.

integration/tests/streaming/segment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ async fn should_persist_and_load_segment_with_messages_with_nowait_confirmation(
264264
.await
265265
.unwrap();
266266
segment
267-
.persist_messages(Some(Confirmation::Nowait))
267+
.persist_messages(Some(Confirmation::NoWait))
268268
.await
269269
.unwrap();
270270
sleep(Duration::from_millis(200)).await;

sdk/src/confirmation.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1-
use std::{fmt, str::FromStr};
2-
31
use serde::{Deserialize, Serialize};
2+
use strum::{Display, EnumString};
43

5-
#[derive(Clone, Default, Deserialize, Serialize, Debug)]
4+
#[derive(Clone, Copy, Debug, Default, Display, Serialize, Deserialize, EnumString, PartialEq)]
5+
#[strum(serialize_all = "snake_case")]
66
pub enum Confirmation {
77
#[default]
88
Wait,
9-
Nowait,
9+
NoWait,
1010
}
1111

12-
impl FromStr for Confirmation {
13-
type Err = String;
12+
#[cfg(test)]
13+
mod tests {
14+
use super::*;
15+
use std::str::FromStr;
1416

15-
fn from_str(s: &str) -> Result<Self, Self::Err> {
16-
match s.to_lowercase().as_str() {
17-
"wait" => Ok(Confirmation::Wait),
18-
"nowait" => Ok(Confirmation::Nowait),
19-
_ => Err(format!("Invalid confirmation type: {}", s)),
20-
}
17+
#[test]
18+
fn test_to_string() {
19+
assert_eq!(Confirmation::Wait.to_string(), "wait");
20+
assert_eq!(Confirmation::NoWait.to_string(), "no_wait");
21+
}
22+
23+
#[test]
24+
fn test_from_str() {
25+
assert_eq!(Confirmation::from_str("wait").unwrap(), Confirmation::Wait);
26+
assert_eq!(
27+
Confirmation::from_str("no_wait").unwrap(),
28+
Confirmation::NoWait
29+
);
2130
}
22-
}
2331

24-
impl fmt::Display for Confirmation {
25-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26-
let s = match self {
27-
Confirmation::Wait => "wait",
28-
Confirmation::Nowait => "nowait",
29-
};
30-
write!(f, "{}", s)
32+
#[test]
33+
fn test_default() {
34+
assert_eq!(Confirmation::default(), Confirmation::Wait);
3135
}
3236
}

server/src/streaming/segments/messages.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ impl Segment {
288288
}
289289
let confirmation = match confirmation {
290290
Some(val) => val,
291-
None => self.config.segment.server_confirmation.clone(),
291+
None => self.config.segment.server_confirmation,
292292
};
293293
let saved_bytes = storage.save_batches(self, batch, confirmation).await?;
294294
storage.save_index(&self.index_path, index).await.with_error_context(|_| format!(

server/src/streaming/segments/storage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl SegmentStorage for FileSegmentStorage {
267267

268268
let save_result = match confirmation {
269269
Confirmation::Wait => self.persister.append(&segment.log_path, &bytes).await,
270-
Confirmation::Nowait => segment.persister_task.send(bytes.into()).await,
270+
Confirmation::NoWait => segment.persister_task.send(bytes.into()).await,
271271
};
272272

273273
save_result

0 commit comments

Comments
 (0)