waitforit 0.1.0

A library to aid in synchronously waiting for some condition to be met.
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use std::{
    cell::Cell,
    path::{Path, PathBuf},
    time::{Duration, Instant, SystemTime},
};

#[cfg(feature = "http")]
use url::Url;

/// Waits for some condition to be met.
#[derive(Clone, Debug)]
pub enum Wait {
    /// Waits until `end_instant`. This can be negated, in which case it will
    /// only trigger until the specified instant.
    Elapsed { end_instant: Instant, not: bool },

    /// Waits until `path` exists (or with `not`, until it no longer exists)
    Exists { not: bool, path: PathBuf },

    /// Waits until a file is updated (or with `not`, until it stops being updated)
    Update {
        not: bool,
        path: PathBuf,
        last_update: Cell<Option<SystemTime>>,
    },

    /// Waits until a file hasn't been updated in some specified [Duration] (or
    /// with `not`, until it hasn't been updated in at least some Duration).
    UpdateSince {
        not: bool,
        path: PathBuf,
        trigger_duration: Duration,
    },

    /// Waits until a connection can be made to `host` (or with `not`, until a
    /// connection can no longer be made).
    TcpHost { not: bool, host: String },

    /// Waits until an HTTP GET to `url` returns `status` (or with `not`, until
    /// it no longer returns that code)
    #[cfg(feature = "http")]
    HttpGet { not: bool, url: String, status: u16 },

    /// Waits until a file's size has been changed (or with `not`, until it
    /// stops changing). Nothing is implied about the direction of change.
    FileSize {
        not: bool,
        path: PathBuf,
        size_bytes: Cell<Option<u64>>,
    },

    /// Waits until the specified `fn` (not `Fn`) returns true.
    Custom { f: fn() -> bool, not: bool },
    // Pid { pid: u64, },
    // FileOpen(??), // Check if a handle is open on a particular file (ie, when a file is done being modified)
}

impl Wait {
    /// Creates a new `Wait` that will complete at `end_instant`.
    ///
    /// Negating this type does nothing.
    pub fn new_elapsed(end_instant: Instant) -> Self {
        Self::Elapsed {
            end_instant,
            not: false,
        }
    }

    /// Creates a new `Wait` that will complete after `duration` has passed,
    /// starting immediately.
    pub fn new_elapsed_from_duration(duration: Duration) -> Self {
        Self::Elapsed {
            end_instant: std::time::Instant::now() + duration,
            not: false,
        }
    }

    /// Creates a new `Wait` that completes when an HTTP GET to `url` returns
    /// the specified `status` code.
    ///
    /// When negated, this completes when an HTTP GET to `url` returns any
    /// other status value.
    #[cfg(feature = "http")]
    pub fn new_http_get<T>(url: T, status: u16) -> Self
    where
        T: Into<String>,
    {
        Self::HttpGet {
            not: false,
            url: url.into(),
            status,
        }
    }

    /// Creates a new `Wait` that completes when a TCP connection can be
    /// established to `host`.
    ///
    /// When `not` is specified, this completes when a TCP connection can no
    /// longer be established.
    pub fn new_tcp_connect<T>(host: T) -> Self
    where
        T: Into<String>,
    {
        Self::TcpHost {
            not: false,
            host: host.into(),
        }
    }

    /// Creates a new `Wait` that completes when the specified file exists.
    ///
    /// When negated, this completes when the file doesn't exist.
    pub fn new_file_exists<T>(path: T) -> Self
    where
        T: Into<PathBuf>,
    {
        Self::Exists {
            not: false,
            path: path.into(),
        }
    }

    /// Creates a new `Wait` that completes when the specified file is updated
    /// (according to its [metadata](std::fs::Metadata)'s modified time). In
    /// other words: as soon as the file is updated, this completes.
    ///
    /// When negated, this completes when the metadata has not been updated in
    /// two consecutive cycles.
    ///
    /// Contrast this with [Self::new_file_update_since], which completes when a
    /// specified duration has passed after the file was last updated.
    pub fn new_file_update<T>(path: T) -> Self
    where
        T: Into<PathBuf>,
    {
        Self::Update {
            not: false,
            path: path.into(),
            last_update: Cell::new(None),
        }
    }

    /// Creates a new `Wait` that completes when the specified file is updated
    /// (according to its [metadata](std::fs::Metadata)'s modified time).
    ///
    /// When negated, this completes when the metadata has not been updated in
    /// two consecutive cycles.
    pub fn new_file_update_since<T>(path: T, trigger_duration: Duration) -> Self
    where
        T: Into<PathBuf>,
    {
        Self::UpdateSince {
            not: false,
            path: path.into(),
            trigger_duration,
        }
    }

    /// Creates a new `Wait` that completes when the specified file's size is
    /// updated (according to its [metadata](std::fs::Metadata)'s length).
    ///
    /// When negated, this completes when the file's length has not been
    /// updated in two consecutive cycles.
    ///
    /// If metadata can't be retrieved for this file, this
    pub fn new_file_size<T>(path: T) -> Self
    where
        T: Into<PathBuf>,
    {
        Self::FileSize {
            not: false,
            path: path.into(),
            size_bytes: Cell::new(None),
        }
    }

    pub fn new_custom(f: fn() -> bool) -> Self {
        Self::Custom { f, not: false }
    }

    //

    /// Checks whether this condition is met.
    ///
    /// This is non-blocking, but depending on the variant may have some associated
    /// delay (eg, an HTTP GET incurs TCP and possibly TLS handshake latency).
    pub fn condition_met(&self) -> bool {
        match self {
            Wait::Elapsed { end_instant, not } => {
                if *not {
                    *end_instant >= Instant::now()
                } else {
                    *end_instant < Instant::now()
                }
            }
            Wait::Exists { not: true, path } => !Path::new(path).exists(),
            Wait::Exists { not: false, path } => Path::new(path).exists(),
            #[cfg(feature = "http")]
            Wait::HttpGet { not, url, status } => {
                let result = ureq::get(url).call();
                if *not {
                    *status != result.status()
                } else {
                    *status == result.status()
                }
            }
            Wait::TcpHost { not: false, host } => std::net::TcpStream::connect(host).is_ok(),
            Wait::TcpHost { not: true, host } => std::net::TcpStream::connect(host).is_err(),
            Wait::Update {
                not,
                path,
                last_update,
            } => {
                // "Update" checks that the file has (not) been updated in the last 'trigger_duration'
                let current_modified = match get_modified_time(path) {
                    Some(systime) => systime,
                    None => return true, // Can't get the modified time, so we'll assume the condition is met.
                };

                match last_update.get() {
                    Some(last_updated) => {
                        let is_updated = last_updated != current_modified;

                        if *not {
                            // We want to trigger when the file *isn't* updating.
                            if is_updated {
                                // Shouldn't trigger yet, but we should update the last known modified date
                                last_update.set(Some(current_modified));
                                false
                            } else {
                                // File hasn't updated, so we should trigger
                                true
                            }
                        } else {
                            // Since not==false: iff the file is updated, we trigger.
                            // Triggering on an updated mod time doesn't ever need to update the value
                            is_updated
                        }
                    }
                    None => {
                        // Haven't tracked the time yet. We'll hang onto it now for the next iteration
                        last_update.set(Some(current_modified));
                        false
                    }
                }
            }
            Wait::UpdateSince {
                not,
                path,
                trigger_duration,
            } => {
                // "Update" checks that the file has (not) been updated in the last 'trigger_duration'
                let last_updated = match get_modified_time(path) {
                    Some(up) => up,
                    None => return true, // Can't get the modified time, so we'll assume the condition is met.
                };

                let since_last_update = match SystemTime::now().duration_since(last_updated) {
                    Ok(d) => d,
                    Err(_) => return true, // can't calculate the duration, so we'll assume the condition is met
                };

                let is_recently_updated = since_last_update < *trigger_duration;

                // The condition is met if:
                // 1. we're looking for a recent update and it was recently updated, or
                // 2. it's not recently updated and we're not looking for it, which means
                // condition_met := is_recently_updated XOR not_negating
                is_recently_updated ^ not
            }

            Wait::FileSize {
                not,
                path,
                size_bytes: bytes,
            } => {
                match (bytes.get(), get_file_size(path)) {
                    // Can't get the file size. This is probably due to file non-existence,
                    // so we'll assume the condition is met
                    (_, None) => true,
                    // Sizes are different when not negating -- condition is met
                    (Some(prev), Some(curr)) if !*not && prev != curr => true,
                    // Size hasn't changed when negating -- condition is met
                    (Some(prev), Some(curr)) if *not && prev == curr => true,
                    // First time or subsequent with changing values - save the (new) size and try again
                    (_, curr) => {
                        bytes.set(curr);
                        false
                    }
                }
            }

            Wait::Custom { f, not } => {
                if *not {
                    !(f)()
                } else {
                    (f)()
                }
            } //Wait::Pid { pid: _ } => todo!(),
        }
    }

    /// Wait for the completion of this condition. This will block the thread.
    pub fn wait(&self, interval: Duration) {
        loop {
            let start = Instant::now();
            if self.condition_met() {
                return;
            }

            let loop_time = start.elapsed();
            if interval > loop_time {
                std::thread::sleep(interval - loop_time);
            }
        }
    }
}

impl std::ops::Not for Wait {
    type Output = Self;

    fn not(mut self) -> Self::Output {
        let not = match &mut self {
            Wait::Elapsed { not, .. } => not,
            Wait::Exists { not, .. } => not,
            Wait::HttpGet { not, .. } => not,
            Wait::TcpHost { not, .. } => not,
            Wait::Update { not, .. } => not,
            Wait::UpdateSince { not, .. } => not,
            Wait::FileSize { not, .. } => not,
            Wait::Custom { not, .. } => not,
        };

        *not = !*not;

        self

        /*
        match self {
            Wait::Elapsed { end_instant, not } => Wait::Elapsed {
                end_instant,
                not: !not,
            },
            Wait::Exists { not, path } => Wait::Exists { not: !not, path },
            Wait::HttpGet { not, url, status } => Wait::HttpGet {
                not: !not,
                url,
                status,
            },
            Wait::TcpHost { not, host } => Wait::TcpHost { not: !not, host },
            Wait::Update {
                not,
                path,
                last_update,
            } => Wait::Update {
                not: !not,
                path,
                last_update,
            },
            Wait::UpdateSince {
                not,
                path,
                trigger_duration,
            } => Wait::UpdateSince {
                not: !not,
                path,
                trigger_duration,
            },
            Wait::FileSize {
                not,
                path,
                size_bytes,
            } => Wait::FileSize {
                not: !not,
                path,
                size_bytes,
            },
            Wait::Custom { f, not } => Wait::Custom { f, not: !not },
        }
        */
    }
}

fn get_modified_time(path: &Path) -> Option<SystemTime> {
    let meta = path.metadata().ok()?;
    meta.modified().ok()
}

fn get_file_size(path: &Path) -> Option<u64> {
    let meta = path.metadata().ok()?;
    Some(meta.len())
}

/// Parses a simple human-readable duration, returning a `Duration`
///
/// "3h10m" -> 11400 seconds
pub fn parse_duration(duration: &str) -> Option<Duration> {
    let mut total_delay = 0;

    let mut acc = 0;
    for c in duration.chars() {
        match c {
            '0'..='9' => {
                acc *= 10;
                acc += c.to_digit(10).unwrap();
            }
            'd' => {
                // days
                total_delay += acc * 86400;
                acc = 0;
            }
            'h' => {
                // hours
                total_delay += acc * 3600;
                acc = 0;
            }
            'm' => {
                // minutes
                total_delay += acc * 60;
                acc = 0;
            }
            's' => {
                // seconds
                total_delay += acc;
                acc = 0;
            }
            _ => return None,
        }
    }

    total_delay += acc;

    let d = Duration::from_secs(total_delay as u64);

    Some(d)
}

/// Parses an input argument for an HTTP GET into the expected status code and URL to hit.
///
/// The URL is validated with the `url` crate, if possible, cleaning potential errors.
/// If that fails, the URL is used as-is.
#[cfg(feature = "http")]
pub fn parse_http_get(urlarg: &str) -> (u16, String) {
    let urlbytes = urlarg.chars().collect::<Vec<_>>();

    let (status_code, urlarg) = if urlarg.len() > 4
        && urlbytes[0..3].iter().all(|c| c.is_numeric())
        && urlbytes[3] == ','
    {
        let code = 100 * (urlbytes[0] as u16 - '0' as u16)
            + 10 * (urlbytes[1] as u16 - '0' as u16)
            + (urlbytes[2] as u16 - '0' as u16);

        (code, &urlarg[4..])
    } else {
        (200, urlarg)
    };

    if let Some(url) = parse_url(urlarg) {
        (status_code, url.to_string())
    } else {
        (status_code, urlarg.to_string())
    }
}

/// Tries to parse a URL using the `url` crate.
#[cfg(feature = "http")]
fn parse_url(urlarg: &str) -> Option<Url> {
    let violations = std::cell::RefCell::new(Vec::new());
    let url = Url::options()
        .syntax_violation_callback(Some(&|v| {
            violations.borrow_mut().push(v);
        }))
        .parse(urlarg)
        .ok()?;

    Some(url)
}

/// Checks that the input appears to be a valid `hostname:port` input, where `port`
/// is a u16.
pub fn validate_tcp(hostarg: &str) -> bool {
    // Assume that the last location of ':' is the delimiter for the port
    let last_colon = hostarg.char_indices().filter(|(_i, c)| c == &':').last();
    if let Some((i, _c)) = last_colon {
        // Everything after the colon should be a u16 port number
        let port = &hostarg[i + 1..];
        port.parse::<u16>().is_ok()
    } else {
        // There's no ':' in the input, so assume this isn't a host to which we can connect
        false
    }
}

mod tests {
    #[test]
    fn valid_tcp() {
        assert!(super::validate_tcp("localhost:80"));
        assert!(!super::validate_tcp("localhost"));

        assert!(super::validate_tcp("127.0.0.1:80"));
        assert!(!super::validate_tcp("127.0.0.1"));

        assert!(super::validate_tcp("127.0.0.1:8000"));
        assert!(super::validate_tcp("127.0.0.1:65534"));
        assert!(super::validate_tcp("127.0.0.1:65535"));
        assert!(!super::validate_tcp("127.0.0.1:65536"));
        assert!(!super::validate_tcp("127.0.0.1:-1"));
    }
}