📄 src/main.rs
mod proto;

use tonic::{Request, Response, Status, transport::Server};

use proto::receipt_printer_server::{ReceiptPrinter, ReceiptPrinterServer};
use proto::{PrintRequest, PrintResponse};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "[::1]:50051".parse()?;
    let receipt_printer_service = ReceiptPrinterService::default();

    Server::builder()
        .add_service(ReceiptPrinterServer::new(receipt_printer_service))
        .serve(addr)
        .await?;

    Ok(())
}

#[derive(Debug, Default)]
pub struct ReceiptPrinterService {}

#[tonic::async_trait]
impl ReceiptPrinter for ReceiptPrinterService {
    async fn print(
        &self,
        request: Request<PrintRequest>, // Accept request of type PrintRequest
    ) -> Result<Response<PrintResponse>, Status> {
        // Return an instance of type PrintResponse
        println!("Got a request: {:?}", request);

        let reply = PrintResponse {};

        Ok(Response::new(reply))
    }
}