Skip to main content

ferrin_spec/dynamic/
batch.rs

1//! Object-safe batch service.
2
3use super::BoxFuture;
4use super::ServiceRef;
5use super::model_ref::ref_conversions;
6use crate::batch::Batch;
7use crate::batch::BatchCancelResult;
8use crate::batch::BatchListOptions;
9use crate::batch::BatchListResult;
10use crate::batch::BatchOperationOptions;
11use crate::batch::BatchResultStream;
12use crate::batch::BatchStartOptions;
13use crate::batch::BatchStartResult;
14use crate::batch::BatchStatus;
15use crate::error::ProviderError;
16use crate::language_model::SupportedUrls;
17use crate::shared::ProviderId;
18
19/// Object-safe counterpart of [`Batch`].
20pub trait DynBatch: Send + Sync + 'static {
21    /// See [`Batch::provider`].
22    fn provider(&self) -> &ProviderId;
23    /// See [`Batch::supported_urls`].
24    fn supported_urls(&self) -> BoxFuture<'_, SupportedUrls>;
25    /// See [`Batch::do_start_batch`].
26    fn do_start_batch(
27        &self,
28        options: BatchStartOptions,
29    ) -> BoxFuture<'_, Result<BatchStartResult, ProviderError>>;
30    /// See [`Batch::do_get_batch_status`].
31    fn do_get_batch_status(
32        &self,
33        options: BatchOperationOptions,
34    ) -> BoxFuture<'_, Result<BatchStatus, ProviderError>>;
35    /// See [`Batch::do_get_batch_results`].
36    fn do_get_batch_results(
37        &self,
38        options: BatchOperationOptions,
39    ) -> BoxFuture<'_, Result<BatchResultStream, ProviderError>>;
40    /// See [`Batch::supports_cancel_batch`].
41    fn supports_cancel_batch(&self) -> bool;
42    /// See [`Batch::do_cancel_batch`].
43    fn do_cancel_batch(
44        &self,
45        options: BatchOperationOptions,
46    ) -> BoxFuture<'_, Result<BatchCancelResult, ProviderError>>;
47    /// See [`Batch::supports_list_batches`].
48    fn supports_list_batches(&self) -> bool;
49    /// See [`Batch::do_list_batches`].
50    fn do_list_batches(
51        &self,
52        options: BatchListOptions,
53    ) -> BoxFuture<'_, Result<BatchListResult, ProviderError>>;
54}
55
56impl<T: Batch> DynBatch for T {
57    fn provider(&self) -> &ProviderId {
58        Batch::provider(self)
59    }
60
61    fn supported_urls(&self) -> BoxFuture<'_, SupportedUrls> {
62        Box::pin(Batch::supported_urls(self))
63    }
64
65    fn do_start_batch(
66        &self,
67        options: BatchStartOptions,
68    ) -> BoxFuture<'_, Result<BatchStartResult, ProviderError>> {
69        Box::pin(Batch::do_start_batch(self, options))
70    }
71
72    fn do_get_batch_status(
73        &self,
74        options: BatchOperationOptions,
75    ) -> BoxFuture<'_, Result<BatchStatus, ProviderError>> {
76        Box::pin(Batch::do_get_batch_status(self, options))
77    }
78
79    fn do_get_batch_results(
80        &self,
81        options: BatchOperationOptions,
82    ) -> BoxFuture<'_, Result<BatchResultStream, ProviderError>> {
83        Box::pin(Batch::do_get_batch_results(self, options))
84    }
85
86    fn supports_cancel_batch(&self) -> bool {
87        Batch::supports_cancel_batch(self)
88    }
89
90    fn do_cancel_batch(
91        &self,
92        options: BatchOperationOptions,
93    ) -> BoxFuture<'_, Result<BatchCancelResult, ProviderError>> {
94        Box::pin(Batch::do_cancel_batch(self, options))
95    }
96
97    fn supports_list_batches(&self) -> bool {
98        Batch::supports_list_batches(self)
99    }
100
101    fn do_list_batches(
102        &self,
103        options: BatchListOptions,
104    ) -> BoxFuture<'_, Result<BatchListResult, ProviderError>> {
105        Box::pin(Batch::do_list_batches(self, options))
106    }
107}
108
109/// Shared reference to a batch service.
110pub type BatchRef = ServiceRef<dyn DynBatch>;
111
112ref_conversions!(BatchRef, Batch, DynBatch);