pipe_it/ext.rs
1use crate::{Context, handler::Handler, Pipeline};
2use std::future::Future;
3use std::marker::PhantomData;
4
5/// Extension trait for any type that implements the Handler trait.
6/// Provides a way to convert a Handler into a Pipeline and combinators.
7pub trait HandlerExt<I, O, Args>: Handler<I, O, Args> {
8 fn pipe(self) -> Pipe<Self, Args>
9 where
10 Self: Sized,
11 {
12 Pipe {
13 p: self,
14 _marker: PhantomData,
15 }
16 }
17
18 /// Connects two pipelines together. Output of the first becomes the input of the second.
19 ///
20 /// # Example
21 ///
22 /// ```rust
23 /// use pipe_it::{Context, Pipeline, Input, ext::HandlerExt};
24 ///
25 /// async fn add_one(n: Input<i32>) -> i32 { *n + 1 }
26 /// async fn to_string(n: Input<i32>) -> String { n.to_string() }
27 ///
28 /// # #[tokio::main]
29 /// # async fn main() {
30 /// let pipe = add_one.pipe().connect(to_string);
31 /// let result = pipe.apply(Context::empty(1)).await;
32 /// assert_eq!(result, "2");
33 /// # }
34 /// ```
35 fn connect<O2, G, Args2>(self, g: G) -> Connect<Pipe<Self, Args>, Pipe<G, Args2>, I, O, O2>
36 where
37 G: Handler<O, O2, Args2>,
38 Self: Sized,
39 {
40 Connect {
41 f: self.pipe(),
42 g: g.pipe(),
43 _marker: PhantomData,
44 }
45 }
46
47 /// Pulls back the domain of the pipeline.
48 /// This allows a pipeline defined on `I` to be used for input `I2` given a mapping `I2 -> I`.
49 /// The mapping function is now a full Handler (async, supports DI).
50 ///
51 /// # Example
52 ///
53 /// ```rust
54 /// use pipe_it::{Context, Pipeline, Input, ext::HandlerExt};
55 ///
56 /// #[derive(Clone)]
57 /// struct User { age: i32 }
58 ///
59 /// // Pre-processing step: extract age from User.
60 /// // Supports DI and ownership extraction.
61 /// async fn get_age(user: Input<User>) -> i32 {
62 /// // We can try_unwrap to get ownership if needed (optimization)
63 /// user.try_unwrap().map(|u| u.age).unwrap_or_else(|u| u.age)
64 /// }
65 ///
66 /// async fn check_adult(age: Input<i32>) -> bool { *age >= 18 }
67 ///
68 /// # #[tokio::main]
69 /// # async fn main() {
70 /// // Input: User -> (get_age) -> i32 -> (check_adult) -> bool
71 /// let pipe = check_adult.pipe().pullback(get_age);
72 /// let result = pipe.apply(Context::empty(User { age: 20 })).await;
73 /// assert_eq!(result, true);
74 /// # }
75 /// ```
76 fn pullback<I2, H, Args2>(self, handler: H) -> Pullback<Pipe<Self, Args>, Pipe<H, Args2>, I2, I, O>
77 where
78 H: Handler<I2, I, Args2>,
79 Self: Sized,
80 {
81 Pullback {
82 p: self.pipe(),
83 map: handler.pipe(),
84 _marker: PhantomData,
85 }
86 }
87
88 /// Lifts the domain requirement to anything that can be converted into the original input.
89 /// Uses `From` / `Into` trait.
90 ///
91 /// # Example
92 ///
93 /// ```rust
94 /// use pipe_it::{Context, Pipeline, Input, ext::HandlerExt};
95 ///
96 /// async fn process_string(s: Input<String>) -> usize { s.len() }
97 ///
98 /// # #[tokio::main]
99 /// # async fn main() {
100 /// // Accepts &str because String implements From<&str>
101 /// let pipe = process_string.pipe().lift::<&str>();
102 /// let result = pipe.apply(Context::empty("hello")).await;
103 /// assert_eq!(result, 5);
104 /// # }
105 /// ```
106 fn lift<I2>(self) -> Pullback<Pipe<Self, Args>, Pipe<LiftHandler<I, I2>, (crate::Input<I2>,)>, I2, I, O>
107 where
108 I: From<I2> + Send + Sync + 'static,
109 I2: Clone + Send + Sync + 'static,
110 Self: Sized,
111 {
112 self.pullback(LiftHandler(PhantomData))
113 }
114
115 /// Extends the output of the pipeline by applying a transformation.
116 ///
117 /// # Example
118 ///
119 /// ```rust
120 /// use pipe_it::{Context, Pipeline, Input, ext::HandlerExt};
121 ///
122 /// async fn compute(n: Input<i32>) -> i32 { *n * 2 }
123 ///
124 /// # #[tokio::main]
125 /// # async fn main() {
126 /// // Changes output from i32 to String
127 /// let pipe = compute.pipe().extend(|n| format!("Result: {}", n));
128 /// let result = pipe.apply(Context::empty(10)).await;
129 /// assert_eq!(result, "Result: 20");
130 /// # }
131 /// ```
132 fn extend<O2, F>(self, map: F) -> Extend<Pipe<Self, Args>, F, I, O, O2>
133 where
134 F: Fn(O) -> O2 + Send + Sync + 'static,
135 Self: Sized,
136 {
137 Extend {
138 p: self.pipe(),
139 map,
140 _marker: PhantomData,
141 }
142 }
143
144 /// Repeats the pipeline operation n times.
145 /// Input and output types must be the same.
146 ///
147 /// # Example
148 ///
149 /// ```rust
150 /// use pipe_it::{Context, Pipeline, Input, ext::HandlerExt};
151 ///
152 /// async fn add_one(n: Input<i32>) -> i32 { *n + 1 }
153 ///
154 /// # #[tokio::main]
155 /// # async fn main() {
156 /// // Input: 0 -> 1 -> 2 -> 3
157 /// let pipe = add_one.pipe().repeat(3);
158 /// let result = pipe.apply(Context::empty(0)).await;
159 /// assert_eq!(result, 3);
160 /// # }
161 /// ```
162 fn repeat(self, times: usize) -> Repeat<Pipe<Self, Args>, I>
163 where
164 Self: Handler<I, I, Args> + Sized,
165 I: Clone + Send + Sync + 'static,
166 {
167 Repeat {
168 p: self.pipe(),
169 times,
170 _marker: PhantomData,
171 }
172 }
173
174 /// Caches the results of the pipeline using an LRU strategy.
175 /// Requires the input to implement `Hash + Eq + Clone` and output to implement `Clone`.
176 ///
177 /// # Example
178 ///
179 /// ```rust
180 /// use pipe_it::{Context, Pipeline, Input, ext::HandlerExt};
181 /// use std::sync::atomic::{AtomicUsize, Ordering};
182 /// use std::sync::Arc;
183 ///
184 /// static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);
185 ///
186 /// async fn heavy_calc(n: Input<i32>) -> i32 {
187 /// CALL_COUNT.fetch_add(1, Ordering::SeqCst);
188 /// *n * *n
189 /// }
190 ///
191 /// # #[tokio::main]
192 /// # async fn main() {
193 /// let pipe = heavy_calc.pipe().cache(100);
194 /// let result1 = pipe.apply(Context::empty(10)).await;
195 /// assert_eq!(result1, 100);
196 /// let result2 = pipe.apply(Context::empty(10)).await; // Should hit cache
197 /// assert_eq!(result2, 100);
198 /// assert_eq!(CALL_COUNT.load(Ordering::SeqCst), 1);
199 /// # }
200 /// ```
201 #[cfg(feature = "cache")]
202 fn cache(self, capacity: usize) -> Cache<Pipe<Self, Args>, I, O>
203 where
204 Self: Sized,
205 I: std::hash::Hash + Eq + Clone + Send + Sync + 'static,
206 O: Clone + Send + Sync + 'static,
207 {
208 Cache {
209 p: self.pipe(),
210 cache: quick_cache::sync::Cache::new(capacity),
211 }
212 }
213
214
215}
216
217impl<F, I, O, Args> HandlerExt<I, O, Args> for F where F: Handler<I, O, Args> {}
218
219/// Wrapper struct to adapt a Handler into a Pipeline.
220#[derive(Clone, Copy)]
221pub struct Pipe<P, Args> {
222 p: P,
223 _marker: PhantomData<Args>,
224}
225
226impl<P, Args, I, O> Pipeline<I, O> for Pipe<P, Args>
227where
228 P: Handler<I, O, Args>,
229 I: Clone + Send + Sync + 'static,
230 O: Send + 'static,
231 Args: Send + Sync + 'static,
232{
233 fn apply(&self, ctx: Context<I>) -> impl Future<Output = O> + Send {
234 self.p.call(ctx)
235 }
236}
237
238// PipelineExt removed to avoid ambiguity with HandlerExt
239// pub trait PipelineExt<I, O>: Pipeline<I, O> { ... }
240// impl<P, I, O> PipelineExt<I, O> for P where P: Pipeline<I, O> {}
241
242// --- Connect ---
243
244#[derive(Clone, Copy)]
245pub struct Connect<F, G, I, O1, O2> {
246 f: F,
247 g: G,
248 _marker: PhantomData<(I, O1, O2)>,
249}
250
251impl<F, G, I, O1, O2> Pipeline<I, O2> for Connect<F, G, I, O1, O2>
252where
253 F: Pipeline<I, O1>,
254 G: Pipeline<O1, O2>,
255 I: Clone + Send + Sync + 'static,
256 O1: Send + Sync + 'static,
257 O2: Send + Sync + 'static,
258{
259 fn apply(&self, ctx: Context<I>) -> impl Future<Output = O2> + Send {
260 async move {
261 // Optimized to release input ownership for the second stage
262 let (input, shared) = ctx.into_parts();
263 let ctx_f = Context::from_parts(input, shared.clone());
264 let res = self.f.apply(ctx_f).await;
265 self.g.apply(Context::from_parts(std::sync::Arc::new(res), shared)).await
266 }
267 }
268}
269
270// --- Pullback ---
271
272pub struct Pullback<P, H, I2, I, O> {
273 p: P,
274 map: H,
275 _marker: PhantomData<(I2, I, O)>,
276}
277
278impl<P, H, I2, I, O> Pipeline<I2, O> for Pullback<P, H, I2, I, O>
279where
280 P: Pipeline<I, O>,
281 H: Pipeline<I2, I>,
282 I2: Clone + Send + Sync + 'static,
283 I: Send + Sync + 'static,
284 O: Send + Sync + 'static,
285{
286 fn apply(&self, ctx: Context<I2>) -> impl Future<Output = O> + Send {
287 async move {
288 // Effectively H.connect(P)
289 let (input, shared) = ctx.into_parts();
290 // Run Mapper (H) on I2
291 let ctx_h = Context::from_parts(input, shared.clone());
292 let mapped_input = self.map.apply(ctx_h).await;
293 // Run P on mapped input I
294 self.p.apply(Context::from_parts(std::sync::Arc::new(mapped_input), shared)).await
295 }
296 }
297}
298
299/// Helper handler for Lift operation
300pub struct LiftHandler<I, I2>(PhantomData<(I, I2)>);
301
302impl<I, I2> Handler<I2, I, (crate::Input<I2>,)> for LiftHandler<I, I2>
303where
304 I: From<I2> + Send + Sync + 'static,
305 I2: Clone + Send + Sync + 'static,
306{
307 fn call(&self, ctx: Context<I2>) -> impl Future<Output = I> + Send {
308 async move {
309 let (input, _) = ctx.into_parts();
310 let val = match crate::Input::<I2>(input).try_unwrap() {
311 Ok(v) => v,
312 Err(arc) => (*arc).clone(),
313 };
314 val.into()
315 }
316 }
317}
318
319// --- Extend ---
320
321pub struct Extend<P, F, I, O1, O2> {
322 p: P,
323 map: F,
324 _marker: PhantomData<(I, O1, O2)>,
325}
326
327impl<P, F, I, O1, O2> Pipeline<I, O2> for Extend<P, F, I, O1, O2>
328where
329 P: Pipeline<I, O1>,
330 F: Fn(O1) -> O2 + Send + Sync + 'static,
331 I: Clone + Send + Sync + 'static,
332 O1: Send + Sync + 'static,
333 O2: Send + Sync + 'static,
334{
335 fn apply(&self, ctx: Context<I>) -> impl Future<Output = O2> + Send {
336 async move {
337 let res = self.p.apply(ctx).await;
338 (self.map)(res)
339 }
340 }
341}
342
343// --- Repeat ---
344
345pub struct Repeat<P, I> {
346 p: P,
347 times: usize,
348 _marker: PhantomData<I>,
349}
350
351impl<P, I> Pipeline<I, I> for Repeat<P, I>
352where
353 P: Pipeline<I, I>,
354 I: Clone + Send + Sync + 'static,
355{
356 fn apply(&self, ctx: Context<I>) -> impl Future<Output = I> + Send {
357 async move {
358 let (input_arc, shared) = ctx.into_parts();
359 let mut val = match std::sync::Arc::try_unwrap(input_arc) {
360 Ok(v) => v,
361 Err(arc) => (*arc).clone(),
362 };
363
364 for _ in 0..self.times {
365 let iter_ctx = Context::from_parts(std::sync::Arc::new(val), shared.clone());
366 val = self.p.apply(iter_ctx).await;
367 }
368 val
369 }
370 }
371}
372
373// --- Cache ---
374#[cfg(feature = "cache")]
375pub struct Cache<P, I, O> {
376 p: P,
377 cache: quick_cache::sync::Cache<I, O>,
378}
379#[cfg(feature = "cache")]
380impl<P, I, O> Pipeline<I, O> for Cache<P, I, O>
381where
382 P: Pipeline<I, O>,
383 I: std::hash::Hash + Eq + Clone + Send + Sync + 'static,
384 O: Clone + Send + Sync + 'static,
385{
386 fn apply(&self, ctx: Context<I>) -> impl Future<Output = O> + Send {
387 async move {
388 let input_arc = ctx.input();
389 if let Some(val) = self.cache.get(&*input_arc) {
390 return val;
391 }
392
393 let res = self.p.apply(ctx).await;
394 self.cache.insert((*input_arc).clone(), res.clone());
395 res
396 }
397 }
398}
399
400impl<P, I, O> Handler<I, O, ()> for P
401where
402 P: Pipeline<I, O>,
403 I: Clone + Send + Sync + 'static,
404 O: Send + 'static,
405{
406 fn call(&self, ctx: Context<I>) -> impl Future<Output = O> + Send {
407 self.apply(ctx)
408 }
409}