Skip to main content

sea_query/
utils.rs

1use std::fmt;
2
3// A helper to make write separator easier and faster
4pub(crate) struct JoinWrite<'a, T, I, B, F1, F2, F3, F4>
5where
6    I: IntoIterator<Item = T>,
7    B: fmt::Write,
8    F1: FnMut(&mut B) -> fmt::Result,
9    F2: FnMut(&mut B, T) -> fmt::Result,
10    F3: FnMut(&mut B) -> fmt::Result,
11    F4: FnMut(&mut B) -> fmt::Result,
12{
13    pub buf: &'a mut B,
14    pub items: I,
15    pub at_first: F1,
16    pub r#do: F2,
17    pub join: F3,
18    pub at_last: F4,
19}
20
21impl<T, I, B, F1, F2, F3, F4> JoinWrite<'_, T, I, B, F1, F2, F3, F4>
22where
23    I: IntoIterator<Item = T>,
24    B: fmt::Write,
25    F1: FnMut(&mut B) -> fmt::Result,
26    F2: FnMut(&mut B, T) -> fmt::Result,
27    F3: FnMut(&mut B) -> fmt::Result,
28    F4: FnMut(&mut B) -> fmt::Result,
29{
30    pub fn exec(mut self) -> fmt::Result {
31        let mut iter = self.items.into_iter();
32        if let Some(first) = iter.next() {
33            (self.at_first)(self.buf)?;
34            (self.r#do)(self.buf, first)?;
35            for item in iter {
36                (self.join)(self.buf)?;
37                (self.r#do)(self.buf, item)?;
38            }
39            (self.at_last)(self.buf)?
40        }
41
42        Ok(())
43    }
44}