Skip to content

Commit 4c3fe59

Browse files
committed
refactor: bunch of optimizations
1 parent 34c96fb commit 4c3fe59

18 files changed

Lines changed: 957 additions & 1034 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.3.1] - 2026-02-08
11+
12+
### Changed
13+
14+
- Multiple optimizations and refactorings.
15+
1016
## [0.3.0] - 2026-02-08
1117

1218
### Added

Cargo.lock

Lines changed: 2 additions & 83 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
authors = ['Piotr Olszewski <asmie@asmie.pl>']
33
name = "stamp-suite"
4-
version = "0.3.0"
4+
version = "0.3.1"
55
edition = "2021"
66
rust-version = "1.93.0"
77

@@ -34,14 +34,15 @@ thiserror = "2.0"
3434
hmac = "0.12"
3535
sha2 = "0.10"
3636
hex = "0.4"
37+
subtle = "2.5"
3738

3839
# TTL backend dependencies (all optional, selected by features)
3940
nix = { version = "0.31", optional = true, default-features = false, features = ["socket", "uio", "net"] }
4041
pnet = { version = "0.35", optional = true }
4142

4243
[dependencies.tokio]
4344
version = "1.49"
44-
features = ["full"]
45+
features = ["rt-multi-thread", "net", "time", "macros"]
4546

4647
# Platform-specific default backends
4748
# Note: These provide defaults when no explicit ttl-* feature is used.

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ Options:
118118
-d, --send-delay <SEND_DELAY> Delay between packets in milliseconds [default: 1000]
119119
-c, --count <COUNT> Number of packets to send [default: 1000]
120120
-L, --timeout <TIMEOUT> Timeout for lost packets in seconds [default: 5]
121-
-4 Force IPv4 addresses
122-
-6 Force IPv6 addresses
123121
-A, --auth-mode <AUTH_MODE> Work mode: A=authenticated, O=open [default: O]
124122
-R Print individual statistics for each packet
125123
-i, --is-reflector Run as Session Reflector instead of Sender

src/clock_format.rs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,22 @@
1-
use std::{fmt, str::FromStr};
1+
use std::fmt;
22

3-
use thiserror::Error;
3+
use clap::ValueEnum;
44

55
/// This enum represents the clock format used in the application.
6-
#[derive(Copy, Clone, PartialEq, Debug)]
6+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default, ValueEnum)]
77
pub enum ClockFormat {
88
/// Network Time Protocol timestamp format (RFC 5905).
9+
#[default]
10+
#[value(name = "NTP")]
911
NTP,
1012
/// Precision Time Protocol timestamp format (IEEE 1588).
13+
#[value(name = "PTP")]
1114
PTP,
1215
}
1316

14-
/// Represents the error that can occur when parsing the clock format.
15-
#[derive(Error, Debug)]
16-
pub enum ClockFormatError {
17-
#[error("Invalid clock source")]
18-
InvalidClockSource,
19-
}
20-
21-
impl FromStr for ClockFormat {
22-
type Err = ClockFormatError;
23-
24-
fn from_str(s: &str) -> Result<Self, Self::Err> {
25-
match s {
26-
"NTP" => Ok(ClockFormat::NTP),
27-
"PTP" => Ok(ClockFormat::PTP),
28-
_ => Err(ClockFormatError::InvalidClockSource),
29-
}
30-
}
31-
}
32-
3317
impl fmt::Display for ClockFormat {
3418
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35-
match *self {
19+
match self {
3620
ClockFormat::NTP => write!(f, "NTP"),
3721
ClockFormat::PTP => write!(f, "PTP"),
3822
}
@@ -45,9 +29,15 @@ mod tests {
4529

4630
#[test]
4731
fn test_clock_format_parsing() {
48-
assert_eq!("NTP".parse::<ClockFormat>().unwrap(), ClockFormat::NTP);
49-
assert_eq!("PTP".parse::<ClockFormat>().unwrap(), ClockFormat::PTP);
50-
assert!("INVALID".parse::<ClockFormat>().is_err());
32+
assert_eq!(
33+
ClockFormat::from_str("NTP", false).unwrap(),
34+
ClockFormat::NTP
35+
);
36+
assert_eq!(
37+
ClockFormat::from_str("PTP", false).unwrap(),
38+
ClockFormat::PTP
39+
);
40+
assert!(ClockFormat::from_str("INVALID", false).is_err());
5141
}
5242

5343
#[test]
@@ -56,38 +46,49 @@ mod tests {
5646
assert_eq!(ClockFormat::PTP.to_string(), "PTP");
5747
}
5848

49+
#[test]
50+
fn test_clock_format_case_insensitive() {
51+
// ValueEnum supports case-insensitive parsing with ignore_case=true
52+
assert_eq!(
53+
ClockFormat::from_str("ntp", true).unwrap(),
54+
ClockFormat::NTP
55+
);
56+
assert_eq!(
57+
ClockFormat::from_str("ptp", true).unwrap(),
58+
ClockFormat::PTP
59+
);
60+
}
61+
5962
#[test]
6063
fn test_clock_format_case_sensitive() {
61-
// Lowercase should fail
62-
assert!("ntp".parse::<ClockFormat>().is_err());
63-
assert!("ptp".parse::<ClockFormat>().is_err());
64-
assert!("Ntp".parse::<ClockFormat>().is_err());
65-
assert!("Ptp".parse::<ClockFormat>().is_err());
64+
// With ignore_case=false, only exact match works
65+
assert!(ClockFormat::from_str("ntp", false).is_err());
66+
assert!(ClockFormat::from_str("ptp", false).is_err());
67+
assert!(ClockFormat::from_str("Ntp", false).is_err());
6668
}
6769

6870
#[test]
6971
fn test_clock_format_empty_string() {
70-
assert!("".parse::<ClockFormat>().is_err());
72+
assert!(ClockFormat::from_str("", false).is_err());
7173
}
7274

7375
#[test]
7476
fn test_clock_format_whitespace() {
75-
assert!(" NTP".parse::<ClockFormat>().is_err());
76-
assert!("NTP ".parse::<ClockFormat>().is_err());
77-
assert!(" NTP ".parse::<ClockFormat>().is_err());
77+
assert!(ClockFormat::from_str(" NTP", false).is_err());
78+
assert!(ClockFormat::from_str("NTP ", false).is_err());
7879
}
7980

8081
#[test]
8182
fn test_clock_format_roundtrip() {
8283
// Parse -> Display -> Parse should give same result
83-
let ntp = "NTP".parse::<ClockFormat>().unwrap();
84+
let ntp = ClockFormat::from_str("NTP", false).unwrap();
8485
let ntp_str = ntp.to_string();
85-
let ntp_again = ntp_str.parse::<ClockFormat>().unwrap();
86+
let ntp_again = ClockFormat::from_str(&ntp_str, false).unwrap();
8687
assert_eq!(ntp, ntp_again);
8788

88-
let ptp = "PTP".parse::<ClockFormat>().unwrap();
89+
let ptp = ClockFormat::from_str("PTP", false).unwrap();
8990
let ptp_str = ptp.to_string();
90-
let ptp_again = ptp_str.parse::<ClockFormat>().unwrap();
91+
let ptp_again = ClockFormat::from_str(&ptp_str, false).unwrap();
9192
assert_eq!(ptp, ptp_again);
9293
}
9394
}

0 commit comments

Comments
 (0)