artichoke_repl_history/
lib.rs

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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#![warn(clippy::all)]
#![warn(clippy::pedantic)]
#![warn(clippy::cargo)]
#![allow(clippy::manual_let_else)]
#![allow(clippy::question_mark)] // https://github.com/rust-lang/rust-clippy/issues/8281
#![allow(unknown_lints)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![warn(missing_copy_implementations)]
#![warn(rust_2018_idioms)]
#![warn(rust_2021_compatibility)]
#![warn(trivial_casts, trivial_numeric_casts)]
#![warn(unused_qualifications)]
#![warn(variant_size_differences)]
// Enable feature callouts in generated documentation:
// https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html
//
// This approach is borrowed from tokio.
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_alias))]

//! Helpers for persisting Artichoke `airb` REPL history to disk.
//!
//! This crate provides platform support for resolving the Artichoke Ruby `airb`
//! REPL's application data folder and path to a history file within it.
//!
//! # Platform Support
//!
//! On macOS, the history file is located in the current user's Application
//! Support directory.
//!
//! On Windows, the history file is located in the current user's `LocalAppData`
//! known folder.
//!
//! On Linux and other non-macOS Unix targets, the history file is located in
//! the `XDG_STATE_HOME` according to the [XDG Base Directory Specification],
//! with the specified fallback if the environment variable is not set.
//!
//! # Examples
//!
//! ```
//! use artichoke_repl_history::repl_history_file;
//!
//! if let Some(hist_file) = repl_history_file() {
//!     // load history ...
//! }
//! ```
//!
//! [XDG Base Directory Specification]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

// Ensure code blocks in `README.md` compile
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
mod readme {}

use std::path::PathBuf;

/// Retrieve the path to the REPL history file.
///
/// This function will attempt to create all parent directories of the returned
/// path. If creating parent directories fails, the error is ignored.
///
/// Callers should call this function once at startup and retain the returned
/// value for later use. Some platforms depend on ambient global state in the
/// environment, so subsequent calls may return different results.
///
/// # Platform Notes
///
/// The file is stored in the application data directory for the host operating
/// system.
///
/// On macOS, the history file is located at a path like:
///
/// ```text
/// /Users/username/Library/Application Support/org.artichokeruby.airb/history
/// ```
///
/// On Windows, the history file is located at a path like:
///
/// ```text
/// C:\Users\username\AppData\Local\Artichoke Ruby\airb\data\history.txt
/// ```
///
/// On Linux and other Unix platforms excluding macOS, the history file is
/// located in the XDG state home following the [XDG Base Directory
/// Specification]. By default, the history file is located at:
///
/// ```txt
/// $HOME/.local/state/artichokeruby/airb_history
/// ```
///
/// # Examples
///
/// ```
/// use artichoke_repl_history::repl_history_file;
///
/// if let Some(hist_file) = repl_history_file() {
///     // load history ...
/// }
/// ```
///
/// [XDG Base Directory Specification]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
#[must_use]
pub fn repl_history_file() -> Option<PathBuf> {
    let data_dir = repl_history_dir()?;

    // Ensure the data directory exists but ignore failures (e.g. the dir
    // already exists) because all operations on the history file are best
    // effort and non-blocking.
    //
    // On Windows, the data dir is a path like:
    //
    // ```
    // C:\Users\username\AppData\Local\Artichoke Ruby\airb\data
    // ```
    //
    // When this path doesn't exist, it contains several directories that
    // must be created, so we must use `fs::create_dir_all`.
    #[cfg(not(any(test, doctest, miri)))] // don't create side effects in tests
    let _ignored = std::fs::create_dir_all(&data_dir);

    Some(data_dir.join(history_file_basename()))
}

#[must_use]
#[cfg(target_os = "macos")]
fn repl_history_dir() -> Option<PathBuf> {
    use std::env;
    use std::ffi::{c_char, CStr, OsString};
    use std::os::unix::ffi::OsStringExt;

    use sysdir::{
        sysdir_get_next_search_path_enumeration, sysdir_search_path_directory_t, sysdir_start_search_path_enumeration,
        PATH_MAX, SYSDIR_DOMAIN_MASK_USER,
    };

    // Use macOS Standard Directories as retrieved by `sysdir(3)` APIs:
    //
    // https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW6
    //
    // Per Apple:
    //
    // > The Library Directory Stores App-Specific Files.
    // >
    // > Application Support: Use this directory to store all app data files
    // > except those associated with the user's documents. For example, you
    // > might use this directory to store app-created data files, configuration
    // > files, templates, or other fixed or modifiable resources that are
    // > managed by the app.

    let mut path = [0; PATH_MAX as usize];

    let dir = sysdir_search_path_directory_t::SYSDIR_DIRECTORY_APPLICATION_SUPPORT;
    let domain_mask = SYSDIR_DOMAIN_MASK_USER;
    let application_support_bytes = unsafe {
        // We don't need to loop here, just take the first result.
        let mut state = sysdir_start_search_path_enumeration(dir, domain_mask);
        let path = path.as_mut_ptr().cast::<c_char>();
        state = sysdir_get_next_search_path_enumeration(state, path);
        if state.is_finished() {
            return None;
        }
        let path = CStr::from_ptr(path);
        path.to_bytes()
    };

    // `std::env::home_dir` does not have problematic behavior on `unix`
    // targets, which includes all apple target OSes and Redox. Per the docs:
    //
    // > Deprecated since 1.29.0: This function's behavior may be unexpected on
    // > Windows. Consider using a crate from crates.io instead.
    // >
    // > -- https://doc.rust-lang.org/1.69.0/std/env/fn.home_dir.html
    //
    // Additionally, the `home` crate on crates.io, which is owned by the
    // @rust-lang organization and used in Rustup and Cargo, uses `std::env::home_dir`
    // to implement `home::home_dir` on `unix` and `target_os = "redox"` targets:
    //
    // https://docs.rs/home/0.5.5/src/home/lib.rs.html#71-75
    #[allow(deprecated)]
    let application_support = match application_support_bytes {
        [] => return None,
        [b'~'] => env::home_dir()?,
        // Per the `sysdir` man page:
        //
        // > Directory paths returned in the user domain will contain "~" to
        // > refer to the user's directory.
        //
        // Below we expand `~/` to `$HOME/` using APIs from `std`.
        [b'~', b'/', tail @ ..] => {
            let home = env::home_dir()?;
            let mut home = home.into_os_string().into_vec();

            home.try_reserve_exact(1 + tail.len()).ok()?;
            home.push(b'/');
            home.extend_from_slice(tail);

            OsString::from_vec(home).into()
        }
        path => {
            let mut buf = vec![];
            buf.try_reserve_exact(path.len()).ok()?;
            buf.extend_from_slice(path);
            OsString::from_vec(buf).into()
        }
    };
    // Per Apple docs: All content in this directory should be placed in a
    // custom subdirectory whose name is that of your app’s bundle identifier
    // or your company.
    Some(application_support.join("org.artichokeruby.airb"))
}

#[must_use]
#[cfg(all(unix, not(target_os = "macos")))]
fn repl_history_dir() -> Option<PathBuf> {
    use std::env;

    // Use state dir from XDG Base Directory Specification/
    //
    // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
    //
    // `$XDG_STATE_HOME` defines the base directory relative to which
    // user-specific state files should be stored. If `$XDG_STATE_HOME` is
    // either not set or empty, a default equal to `$HOME/.local/state` should
    // be used.

    let state_dir = match env::var_os("XDG_STATE_HOME") {
        // if `XDG_STATE_HOME` is empty, ignore it and use the default.
        Some(path) if path.is_empty() => None,
        Some(path) => Some(path),
        // if `XDG_STATE_HOME` is not set, use the default.
        None => None,
    };

    let state_dir = if let Some(state_dir) = state_dir {
        PathBuf::from(state_dir)
    } else {
        // `std::env::home_dir` does not have problematic behavior on `unix`
        // targets, which includes all apple target OSes and Redox. Per the docs:
        //
        // > Deprecated since 1.29.0: This function's behavior may be unexpected on
        // > Windows. Consider using a crate from crates.io instead.
        // >
        // > -- https://doc.rust-lang.org/1.69.0/std/env/fn.home_dir.html
        //
        // Additionally, the `home` crate on crates.io, which is owned by the
        // @rust-lang organization and used in Rustup and Cargo, uses `std::env::home_dir`
        // to implement `home::home_dir` on `unix` and `target_os = "redox"` targets:
        //
        // https://docs.rs/home/0.5.5/src/home/lib.rs.html#71-75
        #[allow(deprecated)]
        let mut state_dir = env::home_dir()?;
        state_dir.extend([".local", "state"]);
        state_dir
    };

    Some(state_dir.join("artichokeruby"))
}

#[must_use]
#[cfg(windows)]
fn repl_history_dir() -> Option<PathBuf> {
    use known_folders::{get_known_folder_path, KnownFolder};

    let local_app_data = get_known_folder_path(KnownFolder::LocalAppData)?;
    Some(local_app_data.join("Artichoke Ruby").join("airb").join("data"))
}

/// Basename for history file.
///
/// # Platform Notes
///
/// - On Windows, this function returns `history.txt`.
/// - On macOS, this function returns `history`.
/// - On non-macOS Unix targets, this function returns `airb_history`.
/// - On all other platforms, this function returns `history`.
#[must_use]
fn history_file_basename() -> &'static str {
    if cfg!(windows) {
        return "history.txt";
    }
    if cfg!(target_os = "macos") {
        return "history";
    }
    if cfg!(unix) {
        return "airb_history";
    }
    "history"
}

#[cfg(test)]
mod tests {
    use std::ffi::OsStr;
    use std::path;

    use super::*;

    // Lock for coordinating access to system env for Unix target tests.
    #[cfg(all(unix, not(target_os = "macos")))]
    static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    #[test]
    fn history_file_basename_is_non_empty() {
        assert!(!history_file_basename().is_empty());
    }

    #[test]
    fn history_file_basename_does_not_contain_path_separators() {
        let filename = history_file_basename();
        for c in filename.chars() {
            assert!(!path::is_separator(c));
        }
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn history_dir_on_macos() {
        let dir = repl_history_dir().unwrap();
        let mut components = dir.components();

        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("/"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Users"));
        let _skip_user_dir = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Library"));
        assert_eq!(
            components.next().unwrap().as_os_str(),
            OsStr::new("Application Support")
        );
        assert_eq!(
            components.next().unwrap().as_os_str(),
            OsStr::new("org.artichokeruby.airb")
        );
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(target_os = "macos")]
    fn history_file_on_macos() {
        let file = repl_history_file().unwrap();
        let mut components = file.components();

        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("/"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Users"));
        let _skip_user_dir = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Library"));
        assert_eq!(
            components.next().unwrap().as_os_str(),
            OsStr::new("Application Support")
        );
        assert_eq!(
            components.next().unwrap().as_os_str(),
            OsStr::new("org.artichokeruby.airb")
        );
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("history"));
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(all(unix, not(target_os = "macos")))]
    fn history_dir_on_unix_xdg_unset() {
        use std::env;

        let _guard = ENV_LOCK.lock();

        env::remove_var("XDG_STATE_HOME");

        let dir = repl_history_dir().unwrap();
        let mut components = dir.components();

        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("/"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("home"));
        let _skip_user_dir = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new(".local"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("state"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("artichokeruby"));
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(all(unix, not(target_os = "macos")))]
    fn history_file_on_unix_xdg_unset() {
        use std::env;

        let _guard = ENV_LOCK.lock();

        env::remove_var("XDG_STATE_HOME");

        let file = repl_history_file().unwrap();
        let mut components = file.components();

        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("/"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("home"));
        let _skip_user_dir = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new(".local"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("state"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("artichokeruby"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("airb_history"));
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(all(unix, not(target_os = "macos")))]
    fn history_dir_on_unix_empty_xdg_state_dir() {
        use std::env;

        let _guard = ENV_LOCK.lock();

        env::remove_var("XDG_STATE_HOME");
        env::set_var("XDG_STATE_HOME", "");

        let dir = repl_history_dir().unwrap();
        let mut components = dir.components();

        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("/"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("home"));
        let _skip_user_dir = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new(".local"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("state"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("artichokeruby"));
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(all(unix, not(target_os = "macos")))]
    fn history_file_on_unix_empty_xdg_state_dir() {
        use std::env;

        let _guard = ENV_LOCK.lock();

        env::remove_var("XDG_STATE_HOME");
        env::set_var("XDG_STATE_HOME", "");

        let file = repl_history_file().unwrap();
        let mut components = file.components();

        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("/"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("home"));
        let _skip_user_dir = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new(".local"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("state"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("artichokeruby"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("airb_history"));
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(all(unix, not(target_os = "macos")))]
    fn history_dir_on_unix_set_xdg_state_dir() {
        use std::env;

        let _guard = ENV_LOCK.lock();

        env::remove_var("XDG_STATE_HOME");
        env::set_var("XDG_STATE_HOME", "/opt/artichoke/state");

        let dir = repl_history_dir().unwrap();
        let mut components = dir.components();

        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("/"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("opt"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("artichoke"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("state"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("artichokeruby"));
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(all(unix, not(target_os = "macos")))]
    fn history_file_on_unix_set_xdg_state_dir() {
        use std::env;

        let _guard = ENV_LOCK.lock();

        env::remove_var("XDG_STATE_HOME");
        env::set_var("XDG_STATE_HOME", "/opt/artichoke/state");

        let file = repl_history_file().unwrap();
        let mut components = file.components();

        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("/"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("opt"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("artichoke"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("state"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("artichokeruby"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("airb_history"));
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(windows)]
    fn history_dir_on_windows() {
        let dir = repl_history_dir().unwrap();
        let mut components = dir.components();

        let _skip_prefix = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new(r"\"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Users"));
        let _skip_user_dir = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("AppData"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Local"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Artichoke Ruby"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("airb"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("data"));
        assert!(components.next().is_none());
    }

    #[test]
    #[cfg(windows)]
    fn history_file_on_windows() {
        let file = repl_history_file().unwrap();
        let mut components = file.components();

        let _skip_prefix = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new(r"\"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Users"));
        let _skip_user_dir = components.next().unwrap();
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("AppData"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Local"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("Artichoke Ruby"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("airb"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("data"));
        assert_eq!(components.next().unwrap().as_os_str(), OsStr::new("history.txt"));
        assert!(components.next().is_none());
    }
}