feat(proto): add RepeatedMessageModel and MessageModel for repeated conversions (#2793)

Introduces CollectionExt, RepeatedMessageModel and MessageModel in easytier-proto to simplify conversions between protobuf repeated fields and internal model vector types.
This commit is contained in:
Luna Yao
2026-11-07 18:27:42 +08:00
committed by GitHub
parent 6e872199f6
commit 412d251cba
6 changed files with 263 additions and 47 deletions
Generated
+64 -18
View File
@@ -216,7 +216,7 @@ version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -227,7 +227,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -2214,6 +2214,17 @@ dependencies = [
"thiserror 2.0.20",
]
[[package]]
name = "delegate"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "780eb241654bf097afb00fc5f054a09b687dad862e485fdcf8399bb056565370"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "der"
version = "0.7.10"
@@ -2365,7 +2376,7 @@ dependencies = [
"libc",
"option-ext",
"redox_users 0.5.3",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -2844,6 +2855,9 @@ dependencies = [
"bytes",
"chrono",
"cidr",
"delegate",
"derive_more",
"educe",
"hmac 0.12.1",
"indoc",
"optionize",
@@ -2992,6 +3006,18 @@ dependencies = [
"zeroize",
]
[[package]]
name = "educe"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ebdb1f8f8d2815bbe7b369a7da10343c82026a4503b68bd1279ba557611e96d"
dependencies = [
"enum-ordinalize",
"proc-macro2",
"quote",
"syn 3.0.6",
]
[[package]]
name = "either"
version = "1.18.0"
@@ -3165,6 +3191,26 @@ dependencies = [
"syn 2.0.119",
]
[[package]]
name = "enum-ordinalize"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89dd01549b09589510cf0647475075d12071456586d70f5c75c98ae2a5537677"
dependencies = [
"enum-ordinalize-derive",
]
[[package]]
name = "enum-ordinalize-derive"
version = "4.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a65863d15a4ce2888bd2f0f543cc963d3879c3a022c8ee43f6141d479a3ac815"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.6",
]
[[package]]
name = "enumflags2"
version = "0.7.12"
@@ -3220,7 +3266,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -5508,7 +5554,7 @@ dependencies = [
"png 0.18.1",
"serde",
"thiserror 2.0.20",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -5910,7 +5956,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -6477,7 +6523,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.45.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -7467,7 +7513,7 @@ dependencies = [
"once_cell",
"socket2",
"tracing",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -8059,7 +8105,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -8117,7 +8163,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -8970,7 +9016,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4"
dependencies = [
"libc",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -9902,10 +9948,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
dependencies = [
"fastrand",
"getrandom 0.3.4",
"getrandom 0.4.3",
"once_cell",
"rustix 1.1.5",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -9924,7 +9970,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874"
dependencies = [
"rustix 1.1.5",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -10608,7 +10654,7 @@ dependencies = [
"png 0.18.1",
"serde",
"thiserror 2.0.20",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -10715,7 +10761,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e"
dependencies = [
"memoffset",
"tempfile",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
@@ -11361,7 +11407,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.48.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -11986,7 +12032,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d6f32a0ff4a9f6f01231eb2059cc85479330739333e0e58cadf03b6af2cca10"
dependencies = [
"cfg-if",
"windows-sys 0.60.2",
"windows-sys 0.61.2",
]
[[package]]
+3
View File
@@ -41,6 +41,9 @@ clap = "4.6.7"
crossbeam = "0.8.5"
crossbeam-utils = "0.8.23"
dashmap = "6.2.1"
delegate = "0.13.5"
derive_more = { version = "2.1.1", features = ["full"] }
educe = "0.8.1"
easytier = { version = "2.7.0", path = "easytier", default-features = false }
easytier-core = { version = "2.7.0", path = "easytier-core", default-features = false }
easytier-ffi = { version = "0.1.0", path = "easytier-contrib/easytier-ffi", default-features = false }
+33 -29
View File
@@ -50,7 +50,7 @@ use std::{
use anyhow::Context as _;
use base64::{Engine as _, prelude::BASE64_STANDARD};
use easytier_proto::{common as common_pb, core_config as pb};
use easytier_proto::{CollectionExt, common as common_pb, core_config as pb};
use optionize::Optionizable;
use serde::{Deserialize, Serialize};
use url::Url;
@@ -420,21 +420,9 @@ impl TryFrom<pb::RouteConfig> for RouteConfig {
Ok(Self {
ipv4: value.ipv4.map(TryInto::try_into).transpose()?,
ipv6: value.ipv6.map(TryInto::try_into).transpose()?,
advertised_routes: value
.advertised_routes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
proxy_networks: value
.proxy_networks
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
foreign_networks: value
.foreign_networks
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
advertised_routes: value.advertised_routes.try_convert()?,
proxy_networks: value.proxy_networks.try_convert()?,
foreign_networks: value.foreign_networks.try_convert()?,
})
}
}
@@ -444,13 +432,9 @@ impl From<RouteConfig> for pb::RouteConfig {
Self {
ipv4: value.ipv4.map(Into::into),
ipv6: value.ipv6.map(Into::into),
advertised_routes: value
.advertised_routes
.into_iter()
.map(Into::into)
.collect(),
proxy_networks: value.proxy_networks.into_iter().map(Into::into).collect(),
foreign_networks: value.foreign_networks.into_iter().map(Into::into).collect(),
advertised_routes: value.advertised_routes.convert(),
proxy_networks: value.proxy_networks.convert(),
foreign_networks: value.foreign_networks.convert(),
}
}
}
@@ -470,6 +454,14 @@ impl TryFrom<pb::IpPrefix> for IpPrefix {
}
}
impl<'m> TryFrom<&'m pb::IpPrefix> for IpPrefix {
type Error = ConfigError;
fn try_from(value: &'m pb::IpPrefix) -> Result<Self, Self::Error> {
(*value).try_into()
}
}
impl From<IpPrefix> for pb::IpPrefix {
fn from(value: IpPrefix) -> Self {
Self {
@@ -493,6 +485,14 @@ impl TryFrom<pb::ProxyNetworkConfig> for ProxyNetworkConfig {
}
}
impl<'m> TryFrom<&'m pb::ProxyNetworkConfig> for ProxyNetworkConfig {
type Error = ConfigError;
fn try_from(value: &'m pb::ProxyNetworkConfig) -> Result<Self, Self::Error> {
(*value).try_into()
}
}
impl From<ProxyNetworkConfig> for pb::ProxyNetworkConfig {
fn from(value: ProxyNetworkConfig) -> Self {
Self {
@@ -508,20 +508,24 @@ impl TryFrom<pb::ForeignNetworkConfig> for ForeignNetworkConfig {
fn try_from(value: pb::ForeignNetworkConfig) -> Result<Self, Self::Error> {
Ok(Self {
name: value.name,
cidrs: value
.cidrs
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
cidrs: value.cidrs.try_convert()?,
})
}
}
impl<'m> TryFrom<&'m pb::ForeignNetworkConfig> for ForeignNetworkConfig {
type Error = ConfigError;
fn try_from(value: &'m pb::ForeignNetworkConfig) -> Result<Self, Self::Error> {
value.clone().try_into()
}
}
impl From<ForeignNetworkConfig> for pb::ForeignNetworkConfig {
fn from(value: ForeignNetworkConfig) -> Self {
Self {
name: value.name,
cidrs: value.cidrs.into_iter().map(Into::into).collect(),
cidrs: value.cidrs.convert(),
}
}
}
+6
View File
@@ -21,6 +21,9 @@ base64 = { workspace = true, optional = true }
bytes = { workspace = true, optional = true }
chrono = { workspace = true, features = ["serde"], optional = true }
cidr = { workspace = true, features = ["serde"], optional = true }
delegate = { workspace = true, optional = true }
derive_more = { workspace = true, optional = true }
educe = { workspace = true, optional = true }
hmac = { workspace = true, optional = true }
prost = { workspace = true, optional = true }
prost-types = { workspace = true, optional = true }
@@ -86,6 +89,9 @@ core = [
"dep:bytes",
"dep:chrono",
"dep:cidr",
"dep:delegate",
"dep:derive_more",
"dep:educe",
"dep:hmac",
"dep:pbjson",
"dep:prost",
+5
View File
@@ -19,9 +19,14 @@ pub mod magic_dns;
pub mod peer_rpc;
#[cfg(feature = "api")]
pub mod tests;
#[cfg(feature = "core")]
pub mod utils;
#[cfg(feature = "api")]
pub mod web;
#[cfg(feature = "core")]
pub use utils::{CollectionExt, MessageModel, RepeatedMessageModel};
pub const DESCRIPTOR_POOL_BYTES: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/file_descriptor_set.bin"));
+152
View File
@@ -0,0 +1,152 @@
use delegate::delegate;
use derive_more::{AsMut, AsRef, Deref, DerefMut, From, IntoIterator};
use educe::Educe;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub trait CollectionExt: IntoIterator + Sized {
fn convert<T, C>(self) -> C
where
Self::Item: Into<T>,
C: FromIterator<T>,
{
self.into_iter().map(Into::into).collect()
}
fn try_convert<T, C, E>(self) -> Result<C, E>
where
Self::Item: TryInto<T, Error = E>,
C: FromIterator<T>,
{
self.into_iter().map(TryInto::try_into).collect()
}
}
impl<I: IntoIterator> CollectionExt for I {}
pub trait MessageModel<Message: prost::Message>:
Into<Message> + TryFrom<Message> + for<'m> TryFrom<&'m Message>
{
}
impl<Message, Model> MessageModel<Message> for Model
where
Message: prost::Message,
Model: Into<Message> + TryFrom<Message> + for<'m> TryFrom<&'m Message>,
{
}
#[derive(
Educe, Debug, Clone, PartialEq, Eq, Hash, From, Deref, DerefMut, AsRef, AsMut, IntoIterator,
)]
#[educe(Default)]
#[as_ref(forward)]
#[as_mut(forward)]
#[into_iterator(owned, ref, ref_mut)]
pub struct RepeatedMessageModel<Model> {
pub models: Vec<Model>,
}
impl<Model> FromIterator<Model> for RepeatedMessageModel<Model> {
fn from_iter<I: IntoIterator<Item = Model>>(iter: I) -> Self {
Self {
models: iter.convert(),
}
}
}
impl<Model> Extend<Model> for RepeatedMessageModel<Model> {
delegate! {
to self.models {
fn extend<T: IntoIterator<Item = Model>>(&mut self, iter: T);
}
}
}
impl<'m, Message, Model> TryFrom<&'m [Message]> for RepeatedMessageModel<Model>
where
Message: prost::Message,
Model: MessageModel<Message>,
{
type Error = <Model as TryFrom<&'m Message>>::Error;
fn try_from(value: &'m [Message]) -> Result<Self, Self::Error> {
value.try_convert()
}
}
impl<Message, Model> From<RepeatedMessageModel<Model>> for Vec<Message>
where
Message: prost::Message,
Model: MessageModel<Message>,
{
fn from(value: RepeatedMessageModel<Model>) -> Self {
value.convert()
}
}
impl<Model> RepeatedMessageModel<Model> {
pub fn try_from_iter<Message>(
messages: impl IntoIterator<Item = Message>,
) -> Result<Self, <Model as TryFrom<Message>>::Error>
where
Message: prost::Message,
Model: MessageModel<Message>,
{
messages.try_convert()
}
}
pub trait RepeatedSerialize: Serialize + Sized {
fn serialize<S>(models: &RepeatedMessageModel<Self>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Serialize::serialize(&**models, serializer)
}
}
pub trait RepeatedDeserialize<'de>: Deserialize<'de> {
fn deserialize<D>(deserializer: D) -> Result<RepeatedMessageModel<Self>, D::Error>
where
D: Deserializer<'de>,
{
Vec::deserialize(deserializer).map(Into::into)
}
#[doc(hidden)]
fn deserialize_in_place<D>(
deserializer: D,
place: &mut RepeatedMessageModel<Self>,
) -> Result<(), D::Error>
where
D: Deserializer<'de>,
{
Deserialize::deserialize_in_place(deserializer, &mut place.models)
}
}
impl<Model: RepeatedSerialize> Serialize for RepeatedMessageModel<Model> {
delegate! {
#[through(RepeatedSerialize)]
to self {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer;
}
}
}
impl<'de, Model: RepeatedDeserialize<'de>> Deserialize<'de> for RepeatedMessageModel<Model> {
delegate! {
to RepeatedDeserialize {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>;
#[doc(hidden)]
fn deserialize_in_place<D>(deserializer: D, place: &mut Self) -> Result<(), D::Error>
where
D: Deserializer<'de>;
}
}
}