Repository navigation
refactor: Add the Clock trait and EnrEntry #443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,28 +105,26 @@ pub struct Record { | |
| kvs: HashMap<String, Vec<u8>>, | ||
| } | ||
|
|
||
| /// OptionFn is a function that sets an option in the record. | ||
| pub type OptionFn = Box<dyn Fn(&mut HashMap<String, Vec<u8>>)>; | ||
|
|
||
| /// with_ip_impl is a function that sets the IP address in the record. | ||
| pub fn with_ip_impl(ip: Ipv4Addr) -> OptionFn { | ||
| Box::new(move |kvs: &mut HashMap<String, Vec<u8>>| { | ||
| kvs.insert(KEY_IP.to_string(), ip.octets().to_vec()); | ||
| }) | ||
| /// A single entry to set when creating a record. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum EnrEntry { | ||
| /// Sets the IP address. | ||
| Ip(Ipv4Addr), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| /// Sets the TCP port. | ||
| Tcp(u16), | ||
| /// Sets the UDP port. | ||
| Udp(u16), | ||
| } | ||
|
|
||
| /// with_tcp_impl is a function that sets the TCP port in the record. | ||
| pub fn with_tcp_impl(tcp: u16) -> OptionFn { | ||
| Box::new(move |kvs: &mut HashMap<String, Vec<u8>>| { | ||
| kvs.insert(KEY_TCP.to_string(), tcp.to_be_bytes().to_vec()); | ||
| }) | ||
| } | ||
|
|
||
| /// with_udp_impl is a function that sets the UDP port in the record. | ||
| pub fn with_udp_impl(udp: u16) -> OptionFn { | ||
| Box::new(move |kvs: &mut HashMap<String, Vec<u8>>| { | ||
| kvs.insert(KEY_UDP.to_string(), udp.to_be_bytes().to_vec()); | ||
| }) | ||
| impl EnrEntry { | ||
| /// Writes this entry into the record's key-value pairs. | ||
| fn apply(self, kvs: &mut HashMap<String, Vec<u8>>) { | ||
| match self { | ||
| Self::Ip(ip) => kvs.insert(KEY_IP.to_string(), ip.octets().to_vec()), | ||
| Self::Tcp(tcp) => kvs.insert(KEY_TCP.to_string(), tcp.to_be_bytes().to_vec()), | ||
| Self::Udp(udp) => kvs.insert(KEY_UDP.to_string(), udp.to_be_bytes().to_vec()), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| impl Record { | ||
|
|
@@ -137,7 +135,7 @@ impl Record { | |
| } | ||
|
|
||
| /// Creates a new record. | ||
| pub fn new(secret_key: &SecretKey, opts: Vec<OptionFn>) -> Result<Self, RecordError> { | ||
| pub fn new(secret_key: &SecretKey, opts: Vec<EnrEntry>) -> Result<Self, RecordError> { | ||
| let mut kvs: HashMap<String, Vec<u8>> = HashMap::new(); | ||
|
|
||
| kvs.insert(KEY_ID.to_string(), VAL_ID.as_bytes().to_vec()); | ||
|
|
@@ -147,7 +145,7 @@ impl Record { | |
| ); | ||
|
|
||
| for opt in opts { | ||
| opt(&mut kvs); | ||
| opt.apply(&mut kvs); | ||
| } | ||
|
|
||
| let signature = sign(secret_key, &encode_elements(&[], &kvs))?; | ||
|
|
@@ -394,9 +392,9 @@ mod tests { | |
| let r1 = Record::new( | ||
| &secret_key, | ||
| vec![ | ||
| with_ip_impl(expect_ip), | ||
| with_tcp_impl(expect_tcp), | ||
| with_udp_impl(expect_udp), | ||
| EnrEntry::Ip(expect_ip), | ||
| EnrEntry::Tcp(expect_tcp), | ||
| EnrEntry::Udp(expect_udp), | ||
| ], | ||
| ) | ||
| .expect("Failed to create record"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better use
ChronoClockto distinguish fromstd::time::SystemTime