Fix include_proto and proto::common

This commit is contained in:
fluo10 2025-10-01 12:50:25 +09:00
parent ee68c5be0e
commit 97a3f86ef9
5 changed files with 53 additions and 53 deletions

View file

@ -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 {

View file

@ -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<iroh::PublicKey> for common::PublicKey {
impl From<iroh::PublicKey> 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<common::PublicKey> for iroh::PublicKey {
impl TryFrom<PublicKeyMessage> for iroh::PublicKey {
type Error = ProtoDeserializeError;
fn try_from(value: common::PublicKey) -> Result<Self, Self::Error> {
fn try_from(value: PublicKeyMessage) -> Result<Self, Self::Error> {
let slice: [u8; 32] = value.key[0..32].try_into()?;
Ok(iroh::PublicKey::from_bytes(&slice)?)
}
}
impl From<uuid::Uuid> for common::Uuid {
impl From<uuid::Uuid> for UuidMessage {
fn from(value: uuid::Uuid) -> Self {
let (first_half, second_half) = value.as_u64_pair();
Self {
@ -26,48 +26,48 @@ impl From<uuid::Uuid> for common::Uuid {
}
}
impl From<common::Uuid> for uuid::Uuid {
fn from(value: common::Uuid) -> Self {
impl From<UuidMessage> for uuid::Uuid {
fn from(value: UuidMessage) -> Self {
uuid::Uuid::from_u64_pair(value.high_bits, value.low_bits)
}
}
impl From<url::Url> for common::Url {
impl From<url::Url> for UrlMessage {
fn from(value: url::Url) -> Self {
todo!()
}
}
impl TryFrom<common::Url> for url::Url {
impl TryFrom<UrlMessage> for url::Url {
type Error = ProtoDeserializeError;
fn try_from(value: common::Url) -> Result<Self, Self::Error> {
fn try_from(value: UrlMessage) -> Result<Self, Self::Error> {
todo!()
}
}
impl From<std::net::SocketAddr> for common::SocketAddr {
impl From<std::net::SocketAddr> 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<common::SocketAddr> for std::net::SocketAddr {
impl TryFrom<SocketAddrMessage> for std::net::SocketAddr {
type Error = ProtoDeserializeError;
fn try_from(value: common::SocketAddr) -> Result<Self, Self::Error> {
fn try_from(value: SocketAddrMessage) -> Result<Self, Self::Error> {
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<std::net::SocketAddrV4> for common::SocketAddrV4 {
impl From<std::net::SocketAddrV4> for SocketAddrV4Message {
fn from(value: std::net::SocketAddrV4) -> Self {
Self {
ip : Some(value.ip().clone().into()),
@ -76,27 +76,27 @@ impl From<std::net::SocketAddrV4> for common::SocketAddrV4 {
}
}
impl TryFrom<common::SocketAddrV4> for std::net::SocketAddrV4 {
impl TryFrom<SocketAddrV4Message> for std::net::SocketAddrV4 {
type Error = ProtoDeserializeError;
fn try_from(value: common::SocketAddrV4) -> Result<Self, Self::Error> {
fn try_from(value: SocketAddrV4Message) -> Result<Self, Self::Error> {
Ok(Self::new(value.ip.ok_or(ProtoDeserializeError::MissingField("SocketAddrV4.ip"))?.into(), value.port.try_into()?))
}
}
impl From<std::net::Ipv4Addr> for common::Ipv4Addr {
impl From<std::net::Ipv4Addr> for Ipv4AddrMessage {
fn from(value: std::net::Ipv4Addr) -> Self {
Self{
bits: value.to_bits()
}
}
}
impl From<common::Ipv4Addr> for std::net::Ipv4Addr {
fn from(value: common::Ipv4Addr) -> Self{
impl From<Ipv4AddrMessage> for std::net::Ipv4Addr {
fn from(value: Ipv4AddrMessage) -> Self{
Self::from_bits(value.bits)
}
}
impl From<std::net::SocketAddrV6> for common::SocketAddrV6 {
impl From<std::net::SocketAddrV6> for SocketAddrV6Message {
fn from(value: std::net::SocketAddrV6) -> Self {
Self{
ip: Some(value.ip().clone().into()),
@ -105,9 +105,9 @@ impl From<std::net::SocketAddrV6> for common::SocketAddrV6 {
}
}
impl TryFrom<common::SocketAddrV6> for std::net::SocketAddrV6 {
impl TryFrom<SocketAddrV6Message> for std::net::SocketAddrV6 {
type Error = ProtoDeserializeError;
fn try_from(value: common::SocketAddrV6) -> Result<Self, Self::Error> {
fn try_from(value: SocketAddrV6Message) -> Result<Self, Self::Error> {
Ok(Self::new(
value.ip.ok_or(ProtoDeserializeError::MissingField("SocketAddrV6.ip"))?.into(),
value.port.try_into()?,
@ -117,7 +117,7 @@ impl TryFrom<common::SocketAddrV6> for std::net::SocketAddrV6 {
}
}
impl From<std::net::Ipv6Addr> for common::Ipv6Addr {
impl From<std::net::Ipv6Addr> for Ipv6AddrMessage {
fn from(value: std::net::Ipv6Addr) -> Self {
let bits = value.to_bits();
@ -127,9 +127,9 @@ impl From<std::net::Ipv6Addr> for common::Ipv6Addr {
}
}
}
impl From<common::Ipv6Addr> for std::net::Ipv6Addr{
impl From<Ipv6AddrMessage> 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<bool, ProtoDeserializeError> {
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());
}
}

View file

@ -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");
}

View file

@ -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::*;

View file

@ -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<Self, Self::Error> {
let (source, duration )= src;