]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
rust: Add macro for `impl ToString for {Unvalidated}ProtoEntry`.
authorIsis Lovecruft <isis@torproject.org>
Wed, 21 Mar 2018 01:44:59 +0000 (01:44 +0000)
committerIsis Lovecruft <isis@torproject.org>
Mon, 2 Apr 2018 19:34:24 +0000 (19:34 +0000)
This implements conversions from either a ProtoEntry or an UnvalidatedProtoEntry
into a String, for use in replacing such functions as
`protover::write_vote_to_string()`.

 * ADD macro for implementing ToString trait for ProtoEntry and
   UnvalidatedProtoEntry.
 * FIXES part of #24031: https://bugs.torproject.org/24031

src/rust/protover/protover.rs

index 847406ca2d1cb20175c8abf6ab4b662df24b6065..2a1a5df9e9265f664ef6abf2a75a61b71b675bf2 100644 (file)
@@ -226,6 +226,27 @@ impl FromStr for ProtoEntry {
     }
 }
 
+/// Generate an implementation of `ToString` for either a `ProtoEntry` or an
+/// `UnvalidatedProtoEntry`.
+macro_rules! impl_to_string_for_proto_entry {
+    ($t:ty) => (
+        impl ToString for $t {
+            fn to_string(&self) -> String {
+                let mut parts: Vec<String> = Vec::new();
+
+                for (protocol, versions) in self.iter() {
+                    parts.push(format!("{}={}", protocol.to_string(), versions.to_string()));
+                }
+                parts.sort_unstable();
+                parts.join(" ")
+            }
+        }
+    )
+}
+
+impl_to_string_for_proto_entry!(ProtoEntry);
+impl_to_string_for_proto_entry!(UnvalidatedProtoEntry);
+
 /// A `ProtoEntry`, but whose `Protocols` can be any `UnknownProtocol`, not just
 /// the supported ones enumerated in `Protocols`.  The protocol versions are
 /// validated, however.