From 97a3f86ef9d57b8336af8a53a4a865675ede956a Mon Sep 17 00:00:00 2001 From: fluo10 Date: Wed, 1 Oct 2025 12:50:25 +0900 Subject: [PATCH] Fix include_proto and proto::common --- core/proto/caretta_sync/remote_node.proto | 4 +- core/src/proto/common.rs | 68 +++++++++++------------ core/src/proto/generated.rs | 13 ----- core/src/proto/mod.rs | 16 +++++- core/src/proto/remote_node.rs | 5 +- 5 files changed, 53 insertions(+), 53 deletions(-) delete mode 100644 core/src/proto/generated.rs diff --git a/core/proto/caretta_sync/remote_node.proto b/core/proto/caretta_sync/remote_node.proto index a26e330..c9493f4 100644 --- a/core/proto/caretta_sync/remote_node.proto +++ b/core/proto/caretta_sync/remote_node.proto @@ -60,7 +60,7 @@ message RemoteNodeInfo { google.protobuf.Duration duration = 1; ControlMsg control_msg = 2; } - message Source { + message SourceDuration { oneof source { google.protobuf.Empty saved = 1; google.protobuf.Empty udp = 2; @@ -76,7 +76,7 @@ message RemoteNodeInfo { LastControl last_control = 3; google.protobuf.Duration last_payload = 4; google.protobuf.Duration last_alive = 5; - repeated Source sources = 6; + repeated SourceDuration sources = 6; } message RelayUrlInfo { diff --git a/core/src/proto/common.rs b/core/src/proto/common.rs index 66e4c3d..c957982 100644 --- a/core/src/proto/common.rs +++ b/core/src/proto/common.rs @@ -1,22 +1,22 @@ -pub use crate::proto::generated::common::{self, PublicKey as PublicKeyMessage, Uuid as UuidMessage, Url as UrlMessage}; +use super::*; -use crate::proto::{error::{ProtoDeserializeError, ProtoSerializeError}}; +use crate::proto::{error::{ProtoDeserializeError, ProtoSerializeError}, socket_addr}; -impl From for common::PublicKey { +impl From for PublicKeyMessage { fn from(value: iroh::PublicKey) -> Self { - common::PublicKey{ key: Vec::from(value.as_bytes()) } + Self{ key: Vec::from(value.as_bytes()) } } } -impl TryFrom for iroh::PublicKey { +impl TryFrom for iroh::PublicKey { type Error = ProtoDeserializeError; - fn try_from(value: common::PublicKey) -> Result { + fn try_from(value: PublicKeyMessage) -> Result { let slice: [u8; 32] = value.key[0..32].try_into()?; Ok(iroh::PublicKey::from_bytes(&slice)?) } } -impl From for common::Uuid { +impl From for UuidMessage { fn from(value: uuid::Uuid) -> Self { let (first_half, second_half) = value.as_u64_pair(); Self { @@ -26,48 +26,48 @@ impl From for common::Uuid { } } -impl From for uuid::Uuid { - fn from(value: common::Uuid) -> Self { +impl From for uuid::Uuid { + fn from(value: UuidMessage) -> Self { uuid::Uuid::from_u64_pair(value.high_bits, value.low_bits) } } -impl From for common::Url { +impl From for UrlMessage { fn from(value: url::Url) -> Self { todo!() } } -impl TryFrom for url::Url { +impl TryFrom for url::Url { type Error = ProtoDeserializeError; - fn try_from(value: common::Url) -> Result { + fn try_from(value: UrlMessage) -> Result { todo!() } } -impl From for common::SocketAddr { +impl From for SocketAddrMessage { fn from(value: std::net::SocketAddr) -> Self { Self{ socket_addr: Some(match value { - std::net::SocketAddr::V4(x) => common::socket_addr::SocketAddr::V4(common::SocketAddrV4::from(x)), - std::net::SocketAddr::V6(x) => common::socket_addr::SocketAddr::V6(common::SocketAddrV6::from(x)), + std::net::SocketAddr::V4(x) => socket_addr::SocketAddr::V4(SocketAddrV4Message::from(x)), + std::net::SocketAddr::V6(x) => socket_addr::SocketAddr::V6(SocketAddrV6Message::from(x)), })} } } -impl TryFrom for std::net::SocketAddr { +impl TryFrom for std::net::SocketAddr { type Error = ProtoDeserializeError; - fn try_from(value: common::SocketAddr) -> Result { + fn try_from(value: SocketAddrMessage) -> Result { Ok(match value.socket_addr.ok_or(Self::Error::MissingField("SocketAddr.socket_addr"))? { - common::socket_addr::SocketAddr::V4(x) => std::net::SocketAddr::V4(x.try_into()?), - common::socket_addr::SocketAddr::V6(x) => std::net::SocketAddr::V6(x.try_into()?), + socket_addr::SocketAddr::V4(x) => std::net::SocketAddr::V4(x.try_into()?), + socket_addr::SocketAddr::V6(x) => std::net::SocketAddr::V6(x.try_into()?), }) } } -impl From for common::SocketAddrV4 { +impl From for SocketAddrV4Message { fn from(value: std::net::SocketAddrV4) -> Self { Self { ip : Some(value.ip().clone().into()), @@ -76,27 +76,27 @@ impl From for common::SocketAddrV4 { } } -impl TryFrom for std::net::SocketAddrV4 { +impl TryFrom for std::net::SocketAddrV4 { type Error = ProtoDeserializeError; - fn try_from(value: common::SocketAddrV4) -> Result { + fn try_from(value: SocketAddrV4Message) -> Result { Ok(Self::new(value.ip.ok_or(ProtoDeserializeError::MissingField("SocketAddrV4.ip"))?.into(), value.port.try_into()?)) } } -impl From for common::Ipv4Addr { +impl From for Ipv4AddrMessage { fn from(value: std::net::Ipv4Addr) -> Self { Self{ bits: value.to_bits() } } } -impl From for std::net::Ipv4Addr { - fn from(value: common::Ipv4Addr) -> Self{ +impl From for std::net::Ipv4Addr { + fn from(value: Ipv4AddrMessage) -> Self{ Self::from_bits(value.bits) } } -impl From for common::SocketAddrV6 { +impl From for SocketAddrV6Message { fn from(value: std::net::SocketAddrV6) -> Self { Self{ ip: Some(value.ip().clone().into()), @@ -105,9 +105,9 @@ impl From for common::SocketAddrV6 { } } -impl TryFrom for std::net::SocketAddrV6 { +impl TryFrom for std::net::SocketAddrV6 { type Error = ProtoDeserializeError; - fn try_from(value: common::SocketAddrV6) -> Result { + fn try_from(value: SocketAddrV6Message) -> Result { Ok(Self::new( value.ip.ok_or(ProtoDeserializeError::MissingField("SocketAddrV6.ip"))?.into(), value.port.try_into()?, @@ -117,7 +117,7 @@ impl TryFrom for std::net::SocketAddrV6 { } } -impl From for common::Ipv6Addr { +impl From for Ipv6AddrMessage { fn from(value: std::net::Ipv6Addr) -> Self { let bits = value.to_bits(); @@ -127,9 +127,9 @@ impl From for common::Ipv6Addr { } } } -impl From for std::net::Ipv6Addr{ +impl From for std::net::Ipv6Addr{ - fn from(value: common::Ipv6Addr) -> Self { + fn from(value: Ipv6AddrMessage) -> Self { Self::from_bits( ((value.high_bits as u128) << 64) + (value.low_bits as u128) ) @@ -142,7 +142,7 @@ mod tests { use super::*; fn validate_uuid_conversion(uuid: uuid::Uuid) -> bool{ - let message = common::Uuid::from(uuid); + let message = UuidMessage::from(uuid); uuid == uuid::Uuid::from(message) } @@ -154,7 +154,7 @@ mod tests { } fn validate_socket_addr_conversion(socket_addr: net::SocketAddr) -> Result { - let message = common::SocketAddr::from(socket_addr); + let message = SocketAddrMessage::from(socket_addr); Ok(socket_addr == message.try_into()?) } @@ -164,8 +164,6 @@ mod tests { assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V4(net::Ipv4Addr::BROADCAST),u16::MAX)).unwrap()); assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V6(net::Ipv6Addr::new(0,0,0,0,0,0,0,0)), u16::MAX)).unwrap()); assert!(validate_socket_addr_conversion(net::SocketAddr::new(net::IpAddr::V6(net::Ipv6Addr::new(u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX, u16::MAX)), u16::MIN)).unwrap()); - - } } \ No newline at end of file diff --git a/core/src/proto/generated.rs b/core/src/proto/generated.rs deleted file mode 100644 index 162c44d..0000000 --- a/core/src/proto/generated.rs +++ /dev/null @@ -1,13 +0,0 @@ -pub mod authorization_request { - tonic::include_proto!("caretta_sync.authorization_request"); -} -pub mod authorized_node { - tonic::include_proto!("caretta_sync.authorized_node"); -} -pub mod common { - tonic::include_proto!("caretta_sync.common"); -} - -pub mod remote_node { - tonic::include_proto!("caretta_sync.remote_node"); -} \ No newline at end of file diff --git a/core/src/proto/mod.rs b/core/src/proto/mod.rs index 3b45615..1ad2951 100644 --- a/core/src/proto/mod.rs +++ b/core/src/proto/mod.rs @@ -5,7 +5,21 @@ mod common; mod error; //mod server; -mod generated; +mod generated{ + tonic::include_proto!("caretta_sync"); +} + +pub use generated::{ + *, + PublicKey as PublicKeyMessage, + Uuid as UuidMessage, + Url as UrlMessage, + SocketAddr as SocketAddrMessage, + SocketAddrV6 as SocketAddrV6Message, + SocketAddrV4 as SocketAddrV4Message, + Ipv4Addr as Ipv4AddrMessage, + Ipv6Addr as Ipv6AddrMessage, +}; pub use common::*; pub use error::*; diff --git a/core/src/proto/remote_node.rs b/core/src/proto/remote_node.rs index 1edbd2b..d7ed3f7 100644 --- a/core/src/proto/remote_node.rs +++ b/core/src/proto/remote_node.rs @@ -1,3 +1,4 @@ +use super::*; use std::{pin::Pin, time::Duration}; @@ -6,10 +7,10 @@ use iroh::{endpoint::{DirectAddrInfo, RemoteInfo}, PublicKey}; use tonic::{Request, Response, Status, Streaming}; use tripod_id::Double; -use crate::{data::local::{LocalRecordId, RemoteNodeRecord}, global::IROH_ENDPOINT, error::Error, proto::{error::{ProtoDeserializeError, ProtoSerializeError}, generated::remote_node::*}}; +use crate::{data::local::{LocalRecordId, RemoteNodeRecord}, error::Error, global::IROH_ENDPOINT, proto::{error::{ProtoDeserializeError, ProtoSerializeError}}}; -impl TryFrom<(iroh::endpoint::Source, Duration)> for RemoteNodeSource { +impl TryFrom<(iroh::endpoint::Source, Duration)> for remote_node_info::remote_info::direct_addr_info::Source { type Error = ProtoSerializeError; fn try_from(src: (iroh::endpoint::Source, Duration)) -> Result { let (source, duration )= src;