Skip to content

Commit 1e80109

Browse files
committed
refactor: mutually exclusive features are a bit of an antipattern
1 parent a86f394 commit 1e80109

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

sntpc/src/lib.rs

+31-36
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,9 @@ pub mod net {
167167
pub use std::net::UdpSocket;
168168
}
169169

170-
#[cfg(all(feature = "log", feature = "defmt"))]
171-
compile_error!(
172-
"Having both `defmt` and `log` features enabled is not supported"
173-
);
174-
175170
#[cfg(feature = "defmt")]
176171
use defmt::debug;
177-
#[cfg(feature = "log")]
172+
#[cfg(all(feature = "log", not(feature = "defmt")))]
178173
use log::debug;
179174

180175
/// Retrieves the current time from an NTP server.
@@ -618,9 +613,9 @@ pub mod sync {
618613

619614
use miniloop::executor::Executor;
620615

621-
#[cfg(all(feature = "defmt", not(feature = "log")))]
616+
#[cfg(feature = "defmt")]
622617
use defmt::debug;
623-
#[cfg(feature = "log")]
618+
#[cfg(all(feature = "log", not(feature = "defmt")))]
624619
use log::debug;
625620

626621
/// Send request to a NTP server with the given address and process the response in a single call
@@ -818,7 +813,7 @@ pub mod sync {
818813
/// // "time.google.com:123" string here used for the sake of simplicity. In the real app
819814
/// // you would want to fix destination address, since string hostname may resolve to
820815
/// // different IP addresses
821-
/// let addr = "time.google.com:123".to_socket_addrs().unwrap().next().unwrap();
816+
/// let addr = "time.google.com:123".to_socket_addrs().unwrap().filter(SocketAddr::is_ipv4).next().unwrap();
822817
///
823818
/// let send_request_result = sntpc::sync::sntp_send_request(addr, &socket, context).unwrap();
824819
/// let result = sntpc::sync::sntp_process_response(addr, &socket, context, send_request_result);
@@ -999,7 +994,33 @@ fn offset_calculate(t1: u64, t2: u64, t3: u64, t4: u64, units: Units) -> i64 {
999994
}
1000995
}
1001996

1002-
#[cfg(feature = "log")]
997+
#[cfg(feature = "defmt")]
998+
fn debug_ntp_packet(packet: &NtpPacket, recv_timestamp: u64) {
999+
let mode = shifter(packet.li_vn_mode, MODE_MASK, MODE_SHIFT);
1000+
let version = shifter(packet.li_vn_mode, VERSION_MASK, VERSION_SHIFT);
1001+
let li = shifter(packet.li_vn_mode, LI_MASK, LI_SHIFT);
1002+
1003+
debug!("NTP Packet:");
1004+
debug!("Mode: {}", mode);
1005+
debug!("Version: {}", version);
1006+
debug!("Leap: {}", li);
1007+
debug!("Stratum: {}", packet.stratum);
1008+
debug!("Poll: {}", packet.poll);
1009+
debug!("Precision: {}", packet.precision);
1010+
debug!("Root delay: {}", packet.root_delay);
1011+
debug!("Root dispersion: {}", packet.root_dispersion);
1012+
debug!(
1013+
"Reference ID: {}",
1014+
str::from_utf8(&packet.ref_id.to_be_bytes()).unwrap_or("")
1015+
);
1016+
debug!("Origin timestamp (client): {}", packet.origin_timestamp);
1017+
debug!("Receive timestamp (server): {}", packet.recv_timestamp);
1018+
debug!("Transmit timestamp (server): {}", packet.tx_timestamp);
1019+
debug!("Receive timestamp (client): {}", recv_timestamp);
1020+
debug!("Reference timestamp (server): {}", packet.ref_timestamp);
1021+
}
1022+
1023+
#[cfg(all(feature = "log", not(feature = "defmt")))]
10031024
fn debug_ntp_packet(packet: &NtpPacket, recv_timestamp: u64) {
10041025
let mode = shifter(packet.li_vn_mode, MODE_MASK, MODE_SHIFT);
10051026
let version = shifter(packet.li_vn_mode, VERSION_MASK, VERSION_SHIFT);
@@ -1039,32 +1060,6 @@ fn debug_ntp_packet(packet: &NtpPacket, recv_timestamp: u64) {
10391060
debug!("{}", delimiter_gen());
10401061
}
10411062

1042-
#[cfg(all(feature = "defmt", not(feature = "log")))]
1043-
fn debug_ntp_packet(packet: &NtpPacket, recv_timestamp: u64) {
1044-
let mode = shifter(packet.li_vn_mode, MODE_MASK, MODE_SHIFT);
1045-
let version = shifter(packet.li_vn_mode, VERSION_MASK, VERSION_SHIFT);
1046-
let li = shifter(packet.li_vn_mode, LI_MASK, LI_SHIFT);
1047-
1048-
debug!("NTP Packet:");
1049-
debug!("Mode: {}", mode);
1050-
debug!("Version: {}", version);
1051-
debug!("Leap: {}", li);
1052-
debug!("Stratum: {}", packet.stratum);
1053-
debug!("Poll: {}", packet.poll);
1054-
debug!("Precision: {}", packet.precision);
1055-
debug!("Root delay: {}", packet.root_delay);
1056-
debug!("Root dispersion: {}", packet.root_dispersion);
1057-
debug!(
1058-
"Reference ID: {}",
1059-
str::from_utf8(&packet.ref_id.to_be_bytes()).unwrap_or("")
1060-
);
1061-
debug!("Origin timestamp (client): {}", packet.origin_timestamp);
1062-
debug!("Receive timestamp (server): {}", packet.recv_timestamp);
1063-
debug!("Transmit timestamp (server): {}", packet.tx_timestamp);
1064-
debug!("Receive timestamp (client): {}", recv_timestamp);
1065-
debug!("Reference timestamp (server): {}", packet.ref_timestamp);
1066-
}
1067-
10681063
fn get_ntp_timestamp<T: NtpTimestampGenerator>(timestamp_gen: &T) -> u64 {
10691064
((timestamp_gen.timestamp_sec()
10701065
+ (u64::from(NtpPacket::NTP_TIMESTAMP_DELTA)))

sntpc/src/socket/embassy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::net::IpAddr;
55

66
#[cfg(feature = "defmt")]
77
use defmt::error;
8-
#[cfg(feature = "log")]
8+
#[cfg(all(feature = "log", not(feature = "defmt")))]
99
use log::error;
1010

1111
impl NtpUdpSocket for UdpSocket<'_> {

sntpc/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::mem;
55
use core::future::Future;
66
#[cfg(feature = "defmt")]
77
use defmt::debug;
8-
#[cfg(feature = "log")]
8+
#[cfg(all(feature = "log", not(feature = "defmt")))]
99
use log::debug;
1010

1111
use crate::get_ntp_timestamp;

sntpc/src/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use chrono::Timelike;
66
use chrono::{Local, TimeZone, Utc};
77
#[cfg(feature = "defmt")]
88
use defmt::debug;
9-
#[cfg(feature = "log")]
9+
#[cfg(all(feature = "log", not(feature = "defmt")))]
1010
use log::debug;
1111

1212
#[cfg(unix)]

0 commit comments

Comments
 (0)