Skip to content

Commit

Permalink
Merge pull request #13 from github/no-caps-please
Browse files Browse the repository at this point in the history
Don't special-case the spelling of `Api`.
  • Loading branch information
jorendorff authored Feb 5, 2024
2 parents 544e525 + b804067 commit ee6d782
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ use haberdash::{MakeHatRequest, MakeHatResponse};

#[tokio::main]
pub async fn main() {
let api_impl = Arc::new(HaberdasherAPIServer {});
let api_impl = Arc::new(HaberdasherApiServer {});
let twirp_routes = Router::new()
.nest(haberdash::SERVICE_FQN, haberdash::router(api_impl));
let app = Router::new()
Expand All @@ -78,10 +78,10 @@ pub async fn main() {
}

// Define the server and implement the trait.
struct HaberdasherAPIServer;
struct HaberdasherApiServer;

#[async_trait]
impl haberdash::HaberdasherAPI for HaberdasherAPIServer {
impl haberdash::HaberdasherApi for HaberdasherApiServer {
async fn make_hat(&self, req: MakeHatRequest) -> Result<MakeHatResponse, TwirpErrorResponse> {
todo!()
}
Expand All @@ -101,7 +101,7 @@ mod haberdash {
include!(concat!(env!("OUT_DIR"), "/service.haberdash.v1.rs"));
}

use haberdash::{HaberdasherAPIClient, MakeHatRequest, MakeHatResponse};
use haberdash::{HaberdasherApiClient, MakeHatRequest, MakeHatResponse};

#[tokio::main]
pub async fn main() {
Expand Down
2 changes: 1 addition & 1 deletion crates/twirp-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct ServiceGenerator;

impl prost_build::ServiceGenerator for ServiceGenerator {
fn generate(&mut self, service: prost_build::Service, buf: &mut String) {
let service_name = service.name.replace("Api", "API");
let service_name = service.name;
let service_fqn = format!("{}.{}", service.package, service_name);
writeln!(buf).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions crates/twirp/src/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{server, TwirpErrorResponse};

/// Builder object used by generated code to build a Twirp service.
///
/// The type `S` is something like `Arc<MyExampleAPIServer>`, which can be cheaply cloned for each
/// The type `S` is something like `Arc<MyExampleApiServer>`, which can be cheaply cloned for each
/// incoming request, providing access to the Rust value that actually implements the RPCs.
pub struct TwirpRouterBuilder<S> {
service: S,
Expand All @@ -30,7 +30,7 @@ where
/// Add a handler for an `rpc` to the router.
///
/// The generated code passes a closure that calls the method, like
/// `|api: Arc<HaberdasherAPIServer>, req: MakeHatRequest| async move { api.make_hat(req) }`.
/// `|api: Arc<HaberdasherApiServer>, req: MakeHatRequest| async move { api.make_hat(req) }`.
pub fn route<F, Fut, Req, Res>(self, url: &str, f: F) -> Self
where
F: Fn(S, Req) -> Fut + Clone + Sync + Send + 'static,
Expand Down
16 changes: 8 additions & 8 deletions crates/twirp/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ pub async fn run_test_server(port: u16) -> JoinHandle<Result<(), std::io::Error>
}

pub fn test_api_router() -> Router {
let api = Arc::new(TestAPIServer {});
let api = Arc::new(TestApiServer {});

// NB: This part would be generated
let test_router = TwirpRouterBuilder::new(api)
.route(
"/Ping",
|api: Arc<TestAPIServer>, req: PingRequest| async move { api.ping(req).await },
|api: Arc<TestApiServer>, req: PingRequest| async move { api.ping(req).await },
)
.route(
"/Boom",
|api: Arc<TestAPIServer>, req: PingRequest| async move { api.boom(req).await },
|api: Arc<TestApiServer>, req: PingRequest| async move { api.boom(req).await },
)
.build();

Expand Down Expand Up @@ -75,10 +75,10 @@ pub async fn read_err_body(body: Body) -> TwirpErrorResponse {

// Hand written sample test server and client

pub struct TestAPIServer;
pub struct TestApiServer;

#[async_trait]
impl TestAPI for TestAPIServer {
impl TestApi for TestApiServer {
async fn ping(&self, req: PingRequest) -> Result<PingResponse, TwirpErrorResponse> {
Ok(PingResponse { name: req.name })
}
Expand All @@ -90,13 +90,13 @@ impl TestAPI for TestAPIServer {

// Small test twirp services (this would usually be generated with twirp-build)
#[async_trait]
pub trait TestAPIClient {
pub trait TestApiClient {
async fn ping(&self, req: PingRequest) -> Result<PingResponse>;
async fn boom(&self, req: PingRequest) -> Result<PingResponse>;
}

#[async_trait]
impl TestAPIClient for Client {
impl TestApiClient for Client {
async fn ping(&self, req: PingRequest) -> Result<PingResponse> {
let url = self.base_url.join("test.TestAPI/Ping")?;
self.request(url, req).await
Expand All @@ -108,7 +108,7 @@ impl TestAPIClient for Client {
}

#[async_trait]
pub trait TestAPI {
pub trait TestApi {
async fn ping(&self, req: PingRequest) -> Result<PingResponse, TwirpErrorResponse>;
async fn boom(&self, req: PingRequest) -> Result<PingResponse, TwirpErrorResponse>;
}
Expand Down
8 changes: 4 additions & 4 deletions example/src/bin/example-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ pub mod service {
}
}

use service::haberdash::v1::{HaberdasherAPIClient, MakeHatRequest, MakeHatResponse};
use service::haberdash::v1::{HaberdasherApiClient, MakeHatRequest, MakeHatResponse};

#[tokio::main]
pub async fn main() -> Result<(), GenericError> {
// basic client
use service::haberdash::v1::HaberdasherAPIClient;
use service::haberdash::v1::HaberdasherApiClient;
let client = Client::from_base_url(Url::parse("http://localhost:3000/twirp/")?)?;
let resp = client.make_hat(MakeHatRequest { inches: 1 }).await;
eprintln!("{:?}", resp);
Expand Down Expand Up @@ -70,10 +70,10 @@ impl Middleware for RequestHeaders {
}

#[derive(Debug)]
struct MockHaberdasherAPIClient;
struct MockHaberdasherApiClient;

#[async_trait]
impl HaberdasherAPIClient for MockHaberdasherAPIClient {
impl HaberdasherApiClient for MockHaberdasherApiClient {
async fn make_hat(
&self,
_req: MakeHatRequest,
Expand Down
18 changes: 9 additions & 9 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn ping() -> &'static str {

#[tokio::main]
pub async fn main() {
let api_impl = Arc::new(HaberdasherAPIServer {});
let api_impl = Arc::new(HaberdasherApiServer {});
let twirp_routes = Router::new().nest(haberdash::SERVICE_FQN, haberdash::router(api_impl));
let app = Router::new()
.nest("/twirp", twirp_routes)
Expand All @@ -38,10 +38,10 @@ pub async fn main() {
}
}

struct HaberdasherAPIServer;
struct HaberdasherApiServer;

#[async_trait]
impl haberdash::HaberdasherAPI for HaberdasherAPIServer {
impl haberdash::HaberdasherApi for HaberdasherApiServer {
async fn make_hat(&self, req: MakeHatRequest) -> Result<MakeHatResponse, TwirpErrorResponse> {
if req.inches == 0 {
return Err(invalid_argument("inches"));
Expand All @@ -65,18 +65,18 @@ impl haberdash::HaberdasherAPI for HaberdasherAPIServer {

#[cfg(test)]
mod test {
use service::haberdash::v1::HaberdasherAPIClient;
use service::haberdash::v1::HaberdasherApiClient;
use twirp::client::Client;
use twirp::url::Url;
use twirp::TwirpErrorCode;

use crate::service::haberdash::v1::HaberdasherAPI;
use crate::service::haberdash::v1::HaberdasherApi;

use super::*;

#[tokio::test]
async fn success() {
let api = HaberdasherAPIServer {};
let api = HaberdasherApiServer {};
let res = api.make_hat(MakeHatRequest { inches: 1 }).await;
assert!(res.is_ok());
let res = res.unwrap();
Expand All @@ -85,7 +85,7 @@ mod test {

#[tokio::test]
async fn invalid_request() {
let api = HaberdasherAPIServer {};
let api = HaberdasherApiServer {};
let res = api.make_hat(MakeHatRequest { inches: 0 }).await;
assert!(res.is_err());
let err = res.unwrap_err();
Expand All @@ -100,7 +100,7 @@ mod test {
}

impl NetServer {
async fn start(api_impl: Arc<HaberdasherAPIServer>) -> Self {
async fn start(api_impl: Arc<HaberdasherApiServer>) -> Self {
let twirp_routes =
Router::new().nest(haberdash::SERVICE_FQN, haberdash::router(api_impl));
let app = Router::new()
Expand Down Expand Up @@ -143,7 +143,7 @@ mod test {

#[tokio::test]
async fn test_net() {
let api_impl = Arc::new(HaberdasherAPIServer {});
let api_impl = Arc::new(HaberdasherApiServer {});
let server = NetServer::start(api_impl).await;

let url = Url::parse(&format!("http://localhost:{}/twirp/", server.port)).unwrap();
Expand Down

0 comments on commit ee6d782

Please sign in to comment.