Skip to content

Commit

Permalink
worker(bindings): add new(&worker::Env) -> Result<Self> method gen (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kanarus authored Feb 15, 2025
1 parent f8e327d commit 659b550
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 65 deletions.
28 changes: 21 additions & 7 deletions ohkami_macros/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,40 @@ pub fn bindings(env_name: TokenStream, bindings_struct: TokenStream) -> Result<T
}
};

let impl_from_request = {
let impl_new = {
let extract = bindings.iter()
.filter(|(name, _)| match &named_fields {
None => true,
Some(n) => n.iter().any(|field_name| name == *field_name)
})
.map(|(name, binding)| {
binding.tokens_extract_as_field(name)
binding.tokens_extract_from_env(name)
});

quote! {
impl #name {
#[allow(unused)]
#vis fn new(env: &::worker::Env) -> ::worker::Result<Self> {
Ok(Self { #( #extract ),* })
}
}
}
};

let impl_from_request = {
quote! {
impl<'req> ::ohkami::FromRequest<'req> for #name {
type Error = ::ohkami::Response;
fn from_request(
req: &'req ::ohkami::Request
) -> ::std::option::Option<::std::result::Result<Self, Self::Error>> {
::std::option::Option::Some(::std::result::Result::Ok(
Self {
#( #extract ),*
}
))
::std::option::Option::Some(
Self::new(req.context.env())
.map_err(|e| {
::worker::console_error!("FromRequest failed: {e}");
e.into()
})
)
}
}
}
Expand All @@ -247,6 +260,7 @@ pub fn bindings(env_name: TokenStream, bindings_struct: TokenStream) -> Result<T
Ok(quote! {
#declare_struct
#const_vars
#impl_new
#impl_from_request
#impl_send_sync
})
Expand Down
14 changes: 6 additions & 8 deletions ohkami_macros/src/worker/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ impl Binding {
}
}

pub fn tokens_extract_as_field(&self, name: &Ident) -> TokenStream {
pub fn tokens_extract_from_env(&self, name: &Ident) -> TokenStream {
let name_str = LitStr::new(&name.to_string(), name.span());

let from_env = |getter: TokenStream| quote! {
#name: match req.context.env().#getter {
Ok(binding) => binding,
Err(e) => {
::worker::console_error!("{e}");
return ::std::option::Option::Some(::std::result::Result::Err(::ohkami::Response::InternalServerError()));
}
}
#name: env.#getter?
// {
// ::worker::console_error!("{e}");
// return ::std::option::Option::Some(::std::result::Result::Err(::ohkami::Response::InternalServerError()));
// }
};

match self {
Expand Down
100 changes: 50 additions & 50 deletions samples/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,56 @@ set -Ceu

SAMPLES=$(pwd)

cd $SAMPLES/openapi-schema-enums && \
cargo run && \
diff openapi.json openapi.json.sample
test $? -ne 0 && exit 150 || :

cd $SAMPLES/openapi-schema-from-into && \
cargo run && \
diff openapi.json openapi.json.sample
test $? -ne 0 && exit 151 || :

cd $SAMPLES/openapi-tags && \
cargo run && \
diff openapi.json openapi.json.sample
test $? -ne 0 && exit 152 || :

cd $SAMPLES/petstore && \
cargo build && \
cd client && \
npm install && \
cd .. && \
(timeout -sKILL 5 cargo run &) && \
sleep 1 && \
cd client && \
npm run gen && \
npm run main
# FIXME
# this is a little flaky; sometimes cause connection refused
test $? -ne 0 && exit 153 || :

cd $SAMPLES/readme-openapi && \
cargo build && \
(timeout -sKILL 1 cargo run &) && \
sleep 1 && \
diff openapi.json openapi.json.sample
test $? -ne 0 && exit 154 || :

cd $SAMPLES/realworld && \
docker compose up -d && \
sleep 5 && \
sqlx migrate run && \
cargo test && \
docker compose down
test $? -ne 0 && exit 155 || :

cd $SAMPLES/streaming && \
cargo build && \
(timeout -sKILL 1 cargo run &) && \
sleep 1 && \
diff openapi.json openapi.json.sample
test $? -ne 0 && exit 156 || :
#cd $SAMPLES/openapi-schema-enums && \
# cargo run && \
# diff openapi.json openapi.json.sample
#test $? -ne 0 && exit 150 || :
#
#cd $SAMPLES/openapi-schema-from-into && \
# cargo run && \
# diff openapi.json openapi.json.sample
#test $? -ne 0 && exit 151 || :
#
#cd $SAMPLES/openapi-tags && \
# cargo run && \
# diff openapi.json openapi.json.sample
#test $? -ne 0 && exit 152 || :
#
#cd $SAMPLES/petstore && \
# cargo build && \
# cd client && \
# npm install && \
# cd .. && \
# (timeout -sKILL 5 cargo run &) && \
# sleep 1 && \
# cd client && \
# npm run gen && \
# npm run main
## FIXME
## this is a little flaky; sometimes cause connection refused
#test $? -ne 0 && exit 153 || :
#
#cd $SAMPLES/readme-openapi && \
# cargo build && \
# (timeout -sKILL 1 cargo run &) && \
# sleep 1 && \
# diff openapi.json openapi.json.sample
#test $? -ne 0 && exit 154 || :
#
#cd $SAMPLES/realworld && \
# docker compose up -d && \
# sleep 5 && \
# sqlx migrate run && \
# cargo test && \
# docker compose down
#test $? -ne 0 && exit 155 || :
#
#cd $SAMPLES/streaming && \
# cargo build && \
# (timeout -sKILL 1 cargo run &) && \
# sleep 1 && \
# diff openapi.json openapi.json.sample
#test $? -ne 0 && exit 156 || :

cd $SAMPLES/worker-bindings && \
cargo check
Expand Down
6 changes: 6 additions & 0 deletions samples/worker-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,9 @@ fn __test_manual_bindings__(bindings: ManualBindings) {

let _: worker::kv::KvStore = bindings.MY_KVSTORE;
}

fn __test_bindings_new__(env: &worker::Env) -> Result<(), worker::Error> {
let _: AutoBindings = AutoBindings::new(env)?;
let _: ManualBindings = ManualBindings::new(env)?;
Ok(())
}

0 comments on commit 659b550

Please sign in to comment.