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 ) ]
77pub 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-
3317impl 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