Skip to content

Commit

Permalink
Merge branch 'main' into chore/covector
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 authored Sep 28, 2024
2 parents f68c7d2 + c5954d7 commit f1a7168
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
39 changes: 19 additions & 20 deletions crates/oblivion-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ pub fn async_route(_: TokenStream, item: TokenStream) -> TokenStream {

let func_name = &input.sig.ident;
let func_args = &input.sig.inputs;
let func_return = match &input.sig.output {
syn::ReturnType::Default => {
return TokenStream::from(
quote! { compile_error!("Handler function must have a return type"); },
)
}
syn::ReturnType::Type(_, ty) => ty,
};

let return_type = match &input.sig.output {
syn::ReturnType::Default => {
Expand Down Expand Up @@ -91,38 +99,29 @@ pub fn async_route(_: TokenStream, item: TokenStream) -> TokenStream {
#input_block
})
},
ReturnType::String => quote! {
Box::pin(async move {
let result = async move {
#input_block
}.await;
Ok(BaseResponse::TextResponse(result))
})
},
ReturnType::Json => quote! {
ReturnType::String | ReturnType::Json => quote! {
Box::pin(async move {
let result = async move {
#input_block
}.await;
Ok(BaseResponse::JsonResponse(result))
Ok(result.into())
})
},
ReturnType::Result(return_type) => match *return_type {
ReturnType::String | ReturnType::Json => quote! {
Box::pin(async move {
let result: #func_return = async move {
#input_block
}.await;
Ok(result?.into())
})
},
ReturnType::ServerResponse => {
return TokenStream::from(
quote! { compile_error!("Unsupported [ServerResponse] in [Result] return type")},
)
}
ReturnType::String => quote! {
Box::pin(async move {
let result = async move {
#input_block
}.await?;
Ok(BaseResponse::TextResponse(result))
})
},
ReturnType::Json => todo!(),
_ => {
ReturnType::Result(_) => {
return TokenStream::from(
quote! { compile_error!("Unsupported complex [Result] return type")},
)
Expand Down
8 changes: 3 additions & 5 deletions crates/oblivion/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oblivion::models::session::Session;
use oblivion::path_route;
use oblivion::types::Response;
use oblivion_codegen::async_route;
use serde_json::json;
use serde_json::{json, Value};
use std::env::args;
use std::sync::Arc;
use tokio::time::Instant;
Expand Down Expand Up @@ -40,12 +40,10 @@ fn json(_session: Session) -> ServerResponse {
}

#[async_route]
async fn alive(session: Session) -> ServerResponse {
async fn alive(session: Session) -> Result<Value> {
session.send("test".into()).await?;
assert_eq!(session.recv().await?.text()?, "test");
Ok(BaseResponse::JsonResponse(
json!({"status": true, "msg": "结束"}),
))
Ok(json!({"status": true, "msg": "结束"}))
}

fn server_callback(res: Response, session: Arc<Session>) -> BoxFuture<'static, bool> {
Expand Down
34 changes: 28 additions & 6 deletions crates/oblivion/src/models/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,34 @@ impl BaseResponse {
Self::FileResponse(_) => Err(Exception::UnsupportedMethod {
method: "FileResponse".to_string(),
}),
Self::TextResponse(text) => {
Ok(text.as_bytes().to_vec())
}
Self::JsonResponse(data) => {
Ok(data.to_string().as_bytes().to_vec())
}
Self::TextResponse(text) => Ok(text.as_bytes().to_vec()),
Self::JsonResponse(data) => Ok(data.to_string().as_bytes().to_vec()),
}
}
}

impl TryInto<Vec<u8>> for BaseResponse {
type Error = Exception;

fn try_into(self) -> Result<Vec<u8>, Self::Error> {
self.as_bytes().map(|bytes| bytes.to_vec())
}
}

impl From<&str> for BaseResponse {
fn from(text: &str) -> Self {
Self::TextResponse(text.to_string())
}
}

impl From<String> for BaseResponse {
fn from(text: String) -> Self {
Self::TextResponse(text)
}
}

impl From<Value> for BaseResponse {
fn from(data: Value) -> Self {
Self::JsonResponse(data)
}
}
1 change: 0 additions & 1 deletion crates/oblivion/src/models/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ impl Session {
.from_bytes(data)?
.to_stream(socket)
.await?;
// OSC::from_u32(status_code).to_stream(socket).await?;
Ok(())
}

Expand Down

0 comments on commit f1a7168

Please sign in to comment.