Remove the need for async_trait for data source

This commit is contained in:
nora 2024-04-29 19:57:16 +02:00
parent bd90f5c978
commit edf7b7dac3
9 changed files with 30 additions and 12 deletions

View file

@ -20,7 +20,6 @@ struct ClassDataSourceModel {
discord_id: StringValue, discord_id: StringValue,
} }
#[terustform::async_trait]
impl DataSource for ClassDataSource { impl DataSource for ClassDataSource {
type ProviderData = CorsClient; type ProviderData = CorsClient;

View file

@ -0,0 +1,7 @@
use terustform::{datasource::DataSource, DResult, Value};
use crate::client::CorsClient;
pub struct ClassResource {
client: CorsClient,
}

View file

@ -17,7 +17,6 @@ struct HugoDataSourceModel {
hugo: StringValue, hugo: StringValue,
} }
#[terustform::async_trait]
impl DataSource for HugoDataSource { impl DataSource for HugoDataSource {
type ProviderData = CorsClient; type ProviderData = CorsClient;

View file

@ -16,7 +16,6 @@ struct ExampleDataSourceModel {
id: StringValue, id: StringValue,
} }
#[terustform::async_trait]
impl DataSource for ExampleDataSource { impl DataSource for ExampleDataSource {
type ProviderData = CorsClient; type ProviderData = CorsClient;

View file

@ -1,3 +1,4 @@
pub mod class_data_source; pub mod class_data_source;
pub mod hugo; pub mod hugo;
pub mod kitty; pub mod kitty;
pub mod class_resource;

View file

@ -1,3 +1,5 @@
use std::future::Future;
use crate::{ use crate::{
provider::{MkDataSource, ProviderData}, provider::{MkDataSource, ProviderData},
values::Value, values::Value,
@ -6,12 +8,11 @@ use crate::{
use super::DResult; use super::DResult;
#[crate::async_trait]
pub trait DataSource: Send + Sync + 'static { pub trait DataSource: Send + Sync + 'static {
type ProviderData: ProviderData; type ProviderData: ProviderData;
// todo: probably want some kind of Value+Schema thing like tfsdk? whatever. // todo: probably want some kind of Value+Schema thing like tfsdk? whatever.
async fn read(&self, config: Value) -> DResult<Value>; fn read(&self, config: Value) -> impl Future<Output = DResult<Value>> + Send + Sync;
fn name(provider_name: &str) -> String fn name(provider_name: &str) -> String
where where

View file

@ -18,7 +18,6 @@ pub use values::*;
pub use terustform_macros::Model; pub use terustform_macros::Model;
pub use async_trait::async_trait;
pub use eyre; pub use eyre;
// -------- // --------

View file

@ -1,4 +1,4 @@
use std::{future::Future, sync::Arc}; use std::{any::Any, future::Future, marker::PhantomData, pin::Pin, sync::Arc};
use crate::{datasource::DataSource, resource::Resource, DResult, Schema, Value}; use crate::{datasource::DataSource, resource::Resource, DResult, Schema, Value};
@ -21,15 +21,22 @@ pub struct MkDataSource<D: ProviderData> {
} }
pub(crate) struct StoredDataSource<D: ProviderData> { pub(crate) struct StoredDataSource<D: ProviderData> {
pub(crate) ds: Arc<dyn DataSource<ProviderData = D>>, pub(crate) object: Arc<dyn Any + Send + Sync>,
pub(crate) read: fn(
s: &(dyn Any + Send + Sync),
config: Value,
) -> Pin<Box<dyn Future<Output = DResult<Value>> + Send + Sync + '_>>,
pub(crate) schema: Schema, pub(crate) schema: Schema,
p: PhantomData<D>,
} }
impl<D: ProviderData> Clone for StoredDataSource<D> { impl<D: ProviderData> Clone for StoredDataSource<D> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
ds: self.ds.clone(), object: self.object.clone(),
read: self.read,
schema: self.schema.clone(), schema: self.schema.clone(),
p: PhantomData,
} }
} }
} }
@ -41,15 +48,21 @@ impl<D: ProviderData> MkDataSource<D> {
schema: Ds::schema(), schema: Ds::schema(),
mk: |data| { mk: |data| {
Ok(StoredDataSource { Ok(StoredDataSource {
ds: Arc::new(Ds::new(data)?), object: Arc::new(Ds::new(data)?),
read: |s, config| {
Box::pin(async {
let ds = s.downcast_ref::<Ds>().unwrap();
ds.read(config).await
})
},
schema: Ds::schema(), schema: Ds::schema(),
p: PhantomData,
}) })
}, },
} }
} }
} }
pub struct MkResource<D: ProviderData> { pub struct MkResource<D: ProviderData> {
pub(crate) name: fn(&str) -> String, pub(crate) name: fn(&str) -> String,
pub(crate) schema: Schema, pub(crate) schema: Schema,

View file

@ -205,7 +205,7 @@ impl<P: Provider> ProviderHandler<P> {
} }
}; };
let state = ds.ds.read(config).await; let state = (ds.read)(&*ds.object, config).await;
let (state, diagnostics) = match state { let (state, diagnostics) = match state {
Ok(s) => ( Ok(s) => (
Some(tfplugin6::DynamicValue { Some(tfplugin6::DynamicValue {