Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions codegen/src/shuttle_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl ToTokens for LoaderAndRunner {
let loader_runner = quote! {
async fn __loader(
#factory_ident: ::shuttle_runtime::ResourceFactory,
) -> ::std::result::Result<::std::vec::Vec<::std::vec::Vec<::core::primitive::u8>>, ::shuttle_runtime::Error> {
) -> ::std::result::Result<::std::vec::Vec<::std::vec::Vec<::core::primitive::u8>>, ::shuttle_runtime::BoxDynError> {
use ::shuttle_runtime::__internals::Context;
#extra_imports

Expand Down Expand Up @@ -447,7 +447,7 @@ mod tests {
let expected = quote! {
async fn __loader(
_factory: ::shuttle_runtime::ResourceFactory,
) -> ::std::result::Result<::std::vec::Vec<::std::vec::Vec<::core::primitive::u8>>, ::shuttle_runtime::Error> {
) -> ::std::result::Result<::std::vec::Vec<::std::vec::Vec<::core::primitive::u8>>, ::shuttle_runtime::BoxDynError> {
use ::shuttle_runtime::__internals::Context;
let mut inputs = Vec::new();
Ok(inputs)
Expand Down Expand Up @@ -494,7 +494,7 @@ mod tests {
let expected = quote! {
async fn __loader(
factory: ::shuttle_runtime::ResourceFactory,
) -> ::std::result::Result<::std::vec::Vec<::std::vec::Vec<::core::primitive::u8>>, ::shuttle_runtime::Error> {
) -> ::std::result::Result<::std::vec::Vec<::std::vec::Vec<::core::primitive::u8>>, ::shuttle_runtime::BoxDynError> {
use ::shuttle_runtime::__internals::Context;
use ::shuttle_runtime::{ResourceFactory, IntoResource, ResourceInputBuilder};
let mut inputs = Vec::new();
Expand Down Expand Up @@ -578,7 +578,7 @@ mod tests {
let expected = quote! {
async fn __loader(
factory: ::shuttle_runtime::ResourceFactory,
) -> ::std::result::Result<::std::vec::Vec<::std::vec::Vec<::core::primitive::u8>>, ::shuttle_runtime::Error> {
) -> ::std::result::Result<::std::vec::Vec<::std::vec::Vec<::core::primitive::u8>>, ::shuttle_runtime::BoxDynError> {
use ::shuttle_runtime::__internals::Context;
use ::shuttle_runtime::{ResourceFactory, IntoResource, ResourceInputBuilder};
let __vars = ::std::collections::HashMap::from_iter(
Expand Down
2 changes: 1 addition & 1 deletion examples
46 changes: 19 additions & 27 deletions resources/aws-rds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use shuttle_service::{
resource::{ProvisionResourceRequest, ResourceType},
DatabaseResource, DbInput, Environment, Error, IntoResource, ResourceFactory,
DatabaseResource, DbInput, Environment, IntoResource, ResourceFactory,
ResourceInputBuilder,
};

Expand Down Expand Up @@ -61,7 +61,7 @@ macro_rules! aws_engine {
type Input = MaybeRequest;
type Output = OutputWrapper;

async fn build(self, factory: &ResourceFactory) -> Result<Self::Input, Error> {
async fn build(self, factory: &ResourceFactory) -> Result<Self::Input, shuttle_service::BoxDynError> {
let md = factory.get_metadata();
Ok(match md.env {
Environment::Deployment => MaybeRequest::Request(ProvisionResourceRequest {
Expand Down Expand Up @@ -94,7 +94,7 @@ pub struct OutputWrapper(DatabaseResource);

#[async_trait]
impl IntoResource<String> for OutputWrapper {
async fn into_resource(self) -> Result<String, Error> {
async fn into_resource(self) -> Result<String, shuttle_service::BoxDynError> {
Ok(match self.0 {
DatabaseResource::ConnectionString(s) => s,
DatabaseResource::Info(info) => info.connection_string(true),
Expand All @@ -111,26 +111,24 @@ mod _diesel_async {
#[cfg(feature = "postgres")]
#[async_trait]
impl IntoResource<diesel_async::AsyncPgConnection> for OutputWrapper {
async fn into_resource(self) -> Result<diesel_async::AsyncPgConnection, Error> {
async fn into_resource(self) -> Result<diesel_async::AsyncPgConnection, shuttle_service::BoxDynError> {
use diesel_async::{AsyncConnection, AsyncPgConnection};

let connection_string: String = self.into_resource().await.unwrap();
Ok(AsyncPgConnection::establish(&connection_string)
.await
.map_err(shuttle_service::error::CustomError::new)?)
.await?)
}
}

#[cfg(any(feature = "mysql", feature = "mariadb"))]
#[async_trait]
impl IntoResource<diesel_async::AsyncMysqlConnection> for OutputWrapper {
async fn into_resource(self) -> Result<diesel_async::AsyncMysqlConnection, Error> {
async fn into_resource(self) -> Result<diesel_async::AsyncMysqlConnection, shuttle_service::BoxDynError> {
use diesel_async::{AsyncConnection, AsyncMysqlConnection};

let connection_string: String = self.into_resource().await.unwrap();
Ok(AsyncMysqlConnection::establish(&connection_string)
.await
.map_err(shuttle_service::error::CustomError::new)?)
.await?)
}
}
}
Expand All @@ -144,15 +142,14 @@ mod _diesel_async_bb8 {
impl IntoResource<diesel_bb8::Pool<diesel_async::AsyncPgConnection>> for OutputWrapper {
async fn into_resource(
self,
) -> Result<diesel_bb8::Pool<diesel_async::AsyncPgConnection>, Error> {
) -> Result<diesel_bb8::Pool<diesel_async::AsyncPgConnection>, shuttle_service::BoxDynError> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(diesel_bb8::Pool::builder()
.min_idle(Some(MIN_CONNECTIONS))
.max_size(MAX_CONNECTIONS)
.build(AsyncDieselConnectionManager::new(connection_string))
.await
.map_err(shuttle_service::error::CustomError::new)?)
.await?)
}
}

Expand All @@ -161,15 +158,14 @@ mod _diesel_async_bb8 {
impl IntoResource<diesel_bb8::Pool<diesel_async::AsyncMysqlConnection>> for OutputWrapper {
async fn into_resource(
self,
) -> Result<diesel_bb8::Pool<diesel_async::AsyncMysqlConnection>, Error> {
) -> Result<diesel_bb8::Pool<diesel_async::AsyncMysqlConnection>, shuttle_service::BoxDynError> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(diesel_bb8::Pool::builder()
.min_idle(Some(MIN_CONNECTIONS))
.max_size(MAX_CONNECTIONS)
.build(AsyncDieselConnectionManager::new(connection_string))
.await
.map_err(shuttle_service::error::CustomError::new)?)
.await?)
}
}
}
Expand All @@ -183,16 +179,15 @@ mod _diesel_async_deadpool {
impl IntoResource<diesel_deadpool::Pool<diesel_async::AsyncPgConnection>> for OutputWrapper {
async fn into_resource(
self,
) -> Result<diesel_deadpool::Pool<diesel_async::AsyncPgConnection>, Error> {
) -> Result<diesel_deadpool::Pool<diesel_async::AsyncPgConnection>, shuttle_service::BoxDynError> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(
diesel_deadpool::Pool::builder(AsyncDieselConnectionManager::new(
connection_string,
))
.max_size(MAX_CONNECTIONS as usize)
.build()
.map_err(shuttle_service::error::CustomError::new)?,
.build()?,
)
}
}
Expand All @@ -202,16 +197,15 @@ mod _diesel_async_deadpool {
impl IntoResource<diesel_deadpool::Pool<diesel_async::AsyncMysqlConnection>> for OutputWrapper {
async fn into_resource(
self,
) -> Result<diesel_deadpool::Pool<diesel_async::AsyncMysqlConnection>, Error> {
) -> Result<diesel_deadpool::Pool<diesel_async::AsyncMysqlConnection>, shuttle_service::BoxDynError> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(
diesel_deadpool::Pool::builder(AsyncDieselConnectionManager::new(
connection_string,
))
.max_size(MAX_CONNECTIONS as usize)
.build()
.map_err(shuttle_service::error::CustomError::new)?,
.build()?,
)
}
}
Expand All @@ -224,30 +218,28 @@ mod _sqlx {
#[cfg(feature = "postgres")]
#[async_trait]
impl IntoResource<sqlx::PgPool> for OutputWrapper {
async fn into_resource(self) -> Result<sqlx::PgPool, Error> {
async fn into_resource(self) -> Result<sqlx::PgPool, shuttle_service::BoxDynError> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(sqlx::postgres::PgPoolOptions::new()
.min_connections(MIN_CONNECTIONS)
.max_connections(MAX_CONNECTIONS)
.connect(&connection_string)
.await
.map_err(shuttle_service::error::CustomError::new)?)
.await?)
}
}

#[cfg(any(feature = "mysql", feature = "mariadb"))]
#[async_trait]
impl IntoResource<sqlx::MySqlPool> for OutputWrapper {
async fn into_resource(self) -> Result<sqlx::MySqlPool, Error> {
async fn into_resource(self) -> Result<sqlx::MySqlPool, shuttle_service::BoxDynError> {
let connection_string: String = self.into_resource().await.unwrap();

Ok(sqlx::mysql::MySqlPoolOptions::new()
.min_connections(MIN_CONNECTIONS)
.max_connections(MAX_CONNECTIONS)
.connect(&connection_string)
.await
.map_err(shuttle_service::error::CustomError::new)?)
.await?)
}
}
}
8 changes: 4 additions & 4 deletions resources/openai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_openai::config::OpenAIConfig;
use async_openai::Client;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use shuttle_service::{CustomError, Error, IntoResource, ResourceFactory, ResourceInputBuilder};
use shuttle_service::{IntoResource, ResourceFactory, ResourceInputBuilder};

pub use async_openai;

Expand Down Expand Up @@ -46,10 +46,10 @@ impl ResourceInputBuilder for OpenAI {
type Input = Config;
type Output = Config;

async fn build(self, _factory: &ResourceFactory) -> Result<Self::Input, Error> {
async fn build(self, _factory: &ResourceFactory) -> Result<Self::Input, shuttle_service::BoxDynError> {
let api_key = self
.api_key
.ok_or(Error::Custom(CustomError::msg("Open AI API key required")))?;
.ok_or(CustomError::msg("Open AI API key required"))?;
let config = Config {
api_base: self.api_base,
api_key,
Expand All @@ -62,7 +62,7 @@ impl ResourceInputBuilder for OpenAI {

#[async_trait]
impl IntoResource<Client<OpenAIConfig>> for Config {
async fn into_resource(self) -> Result<Client<OpenAIConfig>, Error> {
async fn into_resource(self) -> Result<Client<OpenAIConfig>, shuttle_service::BoxDynError> {
let mut openai_config = OpenAIConfig::new().with_api_key(self.api_key);
if let Some(api_base) = self.api_base {
openai_config = openai_config.with_api_base(api_base)
Expand Down
22 changes: 5 additions & 17 deletions resources/opendal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use std::str::FromStr;
use async_trait::async_trait;
use opendal::{Operator, Scheme};
use serde::{Deserialize, Serialize};
use shuttle_service::{
error::{CustomError, Error as ShuttleError},
IntoResource, ResourceFactory, ResourceInputBuilder,
};
use shuttle_service::{IntoResource, ResourceFactory, ResourceInputBuilder};

#[derive(Serialize)]
pub struct Opendal {
Expand Down Expand Up @@ -35,21 +32,12 @@ pub struct OpendalOutput {
cfg: HashMap<String, String>,
}

pub struct Error(opendal::Error);

impl From<Error> for shuttle_service::Error {
fn from(error: Error) -> Self {
let msg = format!("Failed to build opendal resource: {:?}", error.0);
ShuttleError::Custom(CustomError::msg(msg))
}
}

#[async_trait]
impl ResourceInputBuilder for Opendal {
type Input = OpendalOutput;
type Output = OpendalOutput;

async fn build(self, factory: &ResourceFactory) -> Result<Self::Input, ShuttleError> {
async fn build(self, factory: &ResourceFactory) -> Result<Self::Input, shuttle_service::BoxDynError> {
Ok(OpendalOutput {
scheme: self.scheme,
cfg: factory
Expand All @@ -63,10 +51,10 @@ impl ResourceInputBuilder for Opendal {

#[async_trait]
impl IntoResource<Operator> for OpendalOutput {
async fn into_resource(self) -> Result<Operator, shuttle_service::Error> {
let scheme = Scheme::from_str(&self.scheme).map_err(Error)?;
async fn into_resource(self) -> Result<Operator, shuttle_service::BoxDynError> {
let scheme = Scheme::from_str(&self.scheme)?;

Ok(Operator::via_iter(scheme, self.cfg).map_err(Error)?)
Ok(Operator::via_iter(scheme, self.cfg)?)
}
}

Expand Down
12 changes: 5 additions & 7 deletions resources/qdrant/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use shuttle_service::{
error::{CustomError, Error},
resource::{ProvisionResourceRequest, ResourceType},
ContainerRequest, ContainerResponse, Environment, IntoResource, ResourceFactory,
ResourceInputBuilder,
Expand Down Expand Up @@ -47,17 +46,17 @@ impl ResourceInputBuilder for Qdrant {
// The response can be a provisioned container, depending on local/deployment and config.
type Output = OutputWrapper;

async fn build(self, factory: &ResourceFactory) -> Result<Self::Input, Error> {
async fn build(self, factory: &ResourceFactory) -> Result<Self::Input, shuttle_service::BoxDynError> {
let md = factory.get_metadata();
match md.env {
Environment::Deployment => match self.cloud_url {
Some(cloud_url) => Ok(MaybeRequest::NotRequest(QdrantClientConfigWrap {
url: cloud_url,
api_key: self.api_key,
})),
None => Err(Error::Custom(CustomError::msg(
None => Err(CustomError::msg(
"missing `cloud_url` parameter",
))),
)),
},
Environment::Local => match self.local_url {
Some(local_url) => Ok(MaybeRequest::NotRequest(QdrantClientConfigWrap {
Expand Down Expand Up @@ -96,7 +95,7 @@ pub struct QdrantClientConfigWrap {

#[async_trait]
impl IntoResource<qdrant_client::Qdrant> for OutputWrapper {
async fn into_resource(self) -> Result<qdrant_client::Qdrant, Error> {
async fn into_resource(self) -> Result<qdrant_client::Qdrant, shuttle_service::BoxDynError> {
let config = match self {
Self::Container(output) => QdrantClientConfigWrap {
url: format!("http://localhost:{}", output.host_port),
Expand All @@ -106,7 +105,6 @@ impl IntoResource<qdrant_client::Qdrant> for OutputWrapper {
};
Ok(qdrant_client::config::QdrantConfig::from_url(&config.url)
.api_key(config.api_key)
.build()
.map_err(|err| Error::Custom(err.into()))?)
.build()?)
}
}
Loading