tmux-client 0.1.0-alpha.1

Typed asynchronous Rust client for tmux
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Typed, in-memory query expressions and iterator extensions.

use std::cmp::Ordering;
use std::fmt;
use std::sync::Arc;

use regex::RegexBuilder;

use crate::{Error, FormatDescriptor, Result, SnapshotFields, TmuxText};

type Predicate<T> = dyn Fn(&T) -> bool + Send + Sync + 'static;

/// A composable, typed predicate over a tmux object or snapshot.
pub struct FilterExpr<T> {
    description: String,
    predicate: Arc<Predicate<T>>,
}

impl<T> Clone for FilterExpr<T> {
    fn clone(&self) -> Self {
        Self {
            description: self.description.clone(),
            predicate: Arc::clone(&self.predicate),
        }
    }
}

impl<T> fmt::Debug for FilterExpr<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("FilterExpr")
            .field(&self.description)
            .finish()
    }
}

impl<T: 'static> FilterExpr<T> {
    /// Creates an expression from an explicit predicate and diagnostic description.
    #[must_use]
    pub fn new(
        description: impl Into<String>,
        predicate: impl Fn(&T) -> bool + Send + Sync + 'static,
    ) -> Self {
        Self {
            description: description.into(),
            predicate: Arc::new(predicate),
        }
    }

    /// Returns a diagnostic description that never evaluates tmux data.
    #[must_use]
    pub fn description(&self) -> &str {
        &self.description
    }

    /// Evaluates the predicate.
    #[must_use]
    pub fn matches(&self, value: &T) -> bool {
        (self.predicate)(value)
    }

    /// Requires both expressions to match.
    #[must_use]
    pub fn and(self, other: Self) -> Self {
        let description = format!("({}) AND ({})", self.description, other.description);
        Self::new(description, move |value| {
            self.matches(value) && other.matches(value)
        })
    }

    /// Requires either expression to match.
    #[must_use]
    pub fn or(self, other: Self) -> Self {
        let description = format!("({}) OR ({})", self.description, other.description);
        Self::new(description, move |value| {
            self.matches(value) || other.matches(value)
        })
    }

    /// Inverts an expression.
    #[must_use]
    pub fn negate(self) -> Self {
        let description = format!("NOT ({})", self.description);
        Self::new(description, move |value| !self.matches(value))
    }

    /// Compares an extracted value for equality.
    #[must_use]
    pub fn equal<V>(
        field: &'static str,
        accessor: impl Fn(&T) -> V + Send + Sync + 'static,
        expected: V,
    ) -> Self
    where
        V: PartialEq + Send + Sync + 'static,
    {
        Self::new(format!("{field} == <value>"), move |value| {
            accessor(value) == expected
        })
    }

    /// Compares an extracted value for inequality.
    #[must_use]
    pub fn not_equal<V>(
        field: &'static str,
        accessor: impl Fn(&T) -> V + Send + Sync + 'static,
        expected: V,
    ) -> Self
    where
        V: PartialEq + Send + Sync + 'static,
    {
        Self::new(format!("{field} != <value>"), move |value| {
            accessor(value) != expected
        })
    }

    /// Compares an ordered value using one of the four ordering relations.
    #[must_use]
    pub fn compare<V>(
        field: &'static str,
        accessor: impl Fn(&T) -> V + Send + Sync + 'static,
        expected: V,
        comparison: Comparison,
    ) -> Self
    where
        V: PartialOrd + Send + Sync + 'static,
    {
        Self::new(format!("{field} {comparison} <value>"), move |value| {
            accessor(value)
                .partial_cmp(&expected)
                .is_some_and(|ordering| comparison.accepts(ordering))
        })
    }

    /// Requires an extracted value to occur in a supplied collection.
    #[must_use]
    pub fn is_in<V>(
        field: &'static str,
        accessor: impl Fn(&T) -> V + Send + Sync + 'static,
        values: impl IntoIterator<Item = V>,
    ) -> Self
    where
        V: PartialEq + Send + Sync + 'static,
    {
        let values = values.into_iter().collect::<Vec<_>>();
        Self::new(format!("{field} IN <values>"), move |value| {
            values.contains(&accessor(value))
        })
    }

    /// Requires an extracted value not to occur in a supplied collection.
    #[must_use]
    pub fn not_in<V>(
        field: &'static str,
        accessor: impl Fn(&T) -> V + Send + Sync + 'static,
        values: impl IntoIterator<Item = V>,
    ) -> Self
    where
        V: PartialEq + Send + Sync + 'static,
    {
        Self::is_in(field, accessor, values).negate()
    }

    /// Applies a text comparison, optionally with Unicode case folding.
    #[must_use]
    pub fn text(
        field: &'static str,
        accessor: impl Fn(&T) -> String + Send + Sync + 'static,
        expected: impl Into<String>,
        comparison: TextComparison,
        case_insensitive: bool,
    ) -> Self {
        let mut expected = expected.into();
        if case_insensitive {
            expected = expected.to_lowercase();
        }
        Self::new(format!("{field} {comparison} <text>"), move |value| {
            let mut actual = accessor(value);
            if case_insensitive {
                actual = actual.to_lowercase();
            }
            comparison.accepts(&actual, &expected)
        })
    }

    /// Matches an extracted string with a compiled regular expression.
    pub fn regex(
        field: &'static str,
        accessor: impl Fn(&T) -> String + Send + Sync + 'static,
        expression: &str,
        case_insensitive: bool,
    ) -> Result<Self> {
        let regex = RegexBuilder::new(expression)
            .case_insensitive(case_insensitive)
            .build()
            .map_err(|source| Error::InvalidArgument {
                argument: "regular expression",
                message: source.to_string(),
            })?;
        Ok(Self::new(
            format!("{field} ~= /{expression}/"),
            move |value| regex.is_match(&accessor(value)),
        ))
    }
}

impl<T> FilterExpr<T>
where
    T: SnapshotFields + 'static,
{
    /// Compares a catalog-selected raw field without dynamic attribute names.
    #[must_use]
    pub fn format_equal(descriptor: &'static FormatDescriptor, expected: TmuxText) -> Self {
        Self::new(
            format!("{} == <format-value>", descriptor.token()),
            move |snapshot: &T| snapshot.raw_field(descriptor) == Some(&expected),
        )
    }

    /// Applies a text operation to a catalog-selected field.
    #[must_use]
    pub fn format_text(
        descriptor: &'static FormatDescriptor,
        expected: impl Into<String>,
        comparison: TextComparison,
        case_insensitive: bool,
    ) -> Self {
        Self::text(
            descriptor.token(),
            move |snapshot: &T| {
                snapshot
                    .raw_field(descriptor)
                    .map_or_else(String::new, |value| value.to_string_lossy().into_owned())
            },
            expected,
            comparison,
            case_insensitive,
        )
    }
}

impl<T: 'static> std::ops::Not for FilterExpr<T> {
    type Output = Self;

    fn not(self) -> Self::Output {
        self.negate()
    }
}

/// An ordered comparison operation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Comparison {
    /// Less than.
    Less,
    /// Less than or equal.
    LessOrEqual,
    /// Greater than.
    Greater,
    /// Greater than or equal.
    GreaterOrEqual,
}

impl Comparison {
    fn accepts(self, ordering: Ordering) -> bool {
        match self {
            Self::Less => ordering == Ordering::Less,
            Self::LessOrEqual => ordering != Ordering::Greater,
            Self::Greater => ordering == Ordering::Greater,
            Self::GreaterOrEqual => ordering != Ordering::Less,
        }
    }
}

impl fmt::Display for Comparison {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::Less => "<",
            Self::LessOrEqual => "<=",
            Self::Greater => ">",
            Self::GreaterOrEqual => ">=",
        })
    }
}

/// A string comparison operation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum TextComparison {
    /// Exact equality.
    Exact,
    /// Substring containment.
    Contains,
    /// Prefix match.
    StartsWith,
    /// Suffix match.
    EndsWith,
}

impl TextComparison {
    fn accepts(self, actual: &str, expected: &str) -> bool {
        match self {
            Self::Exact => actual == expected,
            Self::Contains => actual.contains(expected),
            Self::StartsWith => actual.starts_with(expected),
            Self::EndsWith => actual.ends_with(expected),
        }
    }
}

impl fmt::Display for TextComparison {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::Exact => "EXACT",
            Self::Contains => "CONTAINS",
            Self::StartsWith => "STARTS WITH",
            Self::EndsWith => "ENDS WITH",
        })
    }
}

/// Iterator returned by [`QueryIteratorExt::filter_expr`].
#[derive(Debug)]
pub struct Filtered<I>
where
    I: Iterator,
{
    iterator: I,
    expression: FilterExpr<I::Item>,
}

impl<I> Iterator for Filtered<I>
where
    I: Iterator,
    I::Item: 'static,
{
    type Item = I::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.iterator.find(|value| self.expression.matches(value))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (_, upper) = self.iterator.size_hint();
        (0, upper)
    }
}

/// Adds typed expression filtering to every iterator.
pub trait QueryIteratorExt: Iterator + Sized
where
    Self::Item: 'static,
{
    /// Filters this iterator using a typed expression.
    fn filter_expr(self, expression: FilterExpr<Self::Item>) -> Filtered<Self> {
        Filtered {
            iterator: self,
            expression,
        }
    }
}

impl<I> QueryIteratorExt for I
where
    I: Iterator,
    I::Item: 'static,
{
}

#[cfg(test)]
mod tests {
    use super::{Comparison, FilterExpr, QueryIteratorExt, TextComparison};

    #[derive(Debug)]
    struct Item {
        name: String,
        count: u32,
    }

    #[test]
    fn expressions_compose_and_filter_iterators() {
        let count = FilterExpr::compare(
            "count",
            |item: &Item| item.count,
            2,
            Comparison::GreaterOrEqual,
        );
        let name = FilterExpr::text(
            "name",
            |item: &Item| item.name.clone(),
            "ALP",
            TextComparison::StartsWith,
            true,
        );
        let values = vec![
            Item {
                name: "alpha".to_owned(),
                count: 1,
            },
            Item {
                name: "Alpine".to_owned(),
                count: 3,
            },
        ];
        let result = values
            .into_iter()
            .filter_expr(count.and(name))
            .collect::<Vec<_>>();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].name, "Alpine");
    }

    #[test]
    fn regex_filters() {
        let Ok(expression) =
            FilterExpr::regex("name", |item: &Item| item.name.clone(), "^a.+a$", true)
        else {
            return;
        };
        assert!(expression.matches(&Item {
            name: "Alpha".to_owned(),
            count: 0
        }));
    }
}