From 20256f5c0cc6d35b5200ac8a79aaee0184895b20 Mon Sep 17 00:00:00 2001 From: H1rono Date: Thu, 23 Jan 2025 00:22:51 +0900 Subject: [PATCH] :sparkles: Build `WorldServiceServer` --- server/src/world.rs | 17 +++++++- server/src/world/grpc.rs | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 server/src/world/grpc.rs diff --git a/server/src/world.rs b/server/src/world.rs index 40a95bd4..d995d69f 100644 --- a/server/src/world.rs +++ b/server/src/world.rs @@ -1,8 +1,11 @@ //! `world.proto` pub mod error; +pub mod grpc; mod r#impl; +use std::sync::Arc; + use futures::future::BoxFuture; use serde::{Deserialize, Serialize}; @@ -83,5 +86,17 @@ pub trait ProvideWorldService: Send + Sync + 'static { self.world_service().check_coordinate(ctx, req) } - // TODO: build_server(this: Arc) -> WorldServiceServer<...> + fn build_server(this: Arc) -> WorldServiceServer + where + Self: Sized, + { + let service = grpc::ServiceImpl::new(this); + WorldServiceServer::new(service) + } } + +#[derive(Debug, Clone, Copy)] +pub struct WorldServiceImpl; + +pub type WorldServiceServer = + schema::world::world_service_server::WorldServiceServer>; diff --git a/server/src/world/grpc.rs b/server/src/world/grpc.rs new file mode 100644 index 00000000..203072b4 --- /dev/null +++ b/server/src/world/grpc.rs @@ -0,0 +1,84 @@ +use std::sync::Arc; + +use schema::world as schema; + +use crate::prelude::IntoStatus; + +// MARK: type conversions + +impl From for schema::Coordinate { + fn from(value: super::Coordinate) -> Self { + let super::Coordinate { x, y } = value; + Self { x, y } + } +} + +impl From for super::Coordinate { + fn from(value: schema::Coordinate) -> Self { + let schema::Coordinate { x, y } = value; + Self { x, y } + } +} + +impl From for schema::Size { + fn from(value: super::Size) -> Self { + let super::Size { width, height } = value; + Self { width, height } + } +} + +impl From for super::Size { + fn from(value: schema::Size) -> Self { + let schema::Size { width, height } = value; + Self { width, height } + } +} + +// MARK: ServiceImpl + +pub struct ServiceImpl { + state: Arc, +} + +impl Clone for ServiceImpl +where + State: super::ProvideWorldService, +{ + fn clone(&self) -> Self { + Self { + state: Arc::clone(&self.state), + } + } +} + +impl ServiceImpl +where + State: super::ProvideWorldService, +{ + pub(super) fn new(state: Arc) -> Self { + Self { state } + } +} + +#[async_trait::async_trait] +impl schema::world_service_server::WorldService for ServiceImpl +where + State: super::ProvideWorldService, +{ + async fn get_world( + &self, + request: tonic::Request, + ) -> tonic::Result> { + let (_, _, schema::GetWorldRequest {}) = request.into_parts(); + let req = super::GetWorldSize {}; + let size = self + .state + .get_world_size(req) + .await + .map_err(IntoStatus::into_status)? + .into(); + let world = schema::World { size: Some(size) }; + let res = schema::GetWorldResponse { world: Some(world) }; + Ok(tonic::Response::new(res)) + } +}