open_editor/
lib.rs

1pub mod editor;
2pub mod editor_call_builder;
3pub mod editor_kind;
4pub mod errors;
5
6use std::path::Path;
7
8pub use editor::Editor;
9pub use editor_call_builder::EditorCallBuilder;
10pub use editor_kind::EditorKind;
11
12use crate::errors::OpenEditorError;
13
14static ENV_VARS: &[&str] = &["VISUAL", "EDITOR"];
15/// Macro to implement static methods for `EditorCallBuilder`.
16macro_rules! impl_static_editor_methods {
17    (
18        $(
19            $(#[$doc:meta])*
20            $static_name:ident($($param:ident: $param_type:ty),*) -> $return_type:ty => $instance_method:ident
21        ),* $(,)?
22    ) => {
23            $(
24                $(#[$doc])*
25                pub fn $static_name($($param: $param_type),*) -> $return_type {
26                    EditorCallBuilder::new().$instance_method($($param),*)
27                }
28            )*
29
30    };
31}
32
33impl_static_editor_methods! {
34    /// Open the default editor and return what was written in it.
35    ///
36    /// This is a static convenience method equivalent to `EditorCallBuilder::new().open_editor()`.
37    ///
38    /// # Errors
39    /// Returns an error if the editor call fails, or if the temporary file cannot be read or cleaned up.
40    open_editor() -> Result<String, OpenEditorError> => open_editor,
41
42    /// Edit a string in the default editor and return the result.
43    ///
44    /// This is a static convenience method equivalent to `EditorCallBuilder::new().edit_string(string)`.
45    ///
46    /// # Errors
47    /// Returns an error if the editor call fails, or if the temporary file cannot be read or cleaned up.
48    edit_string(string: &str) -> Result<String, OpenEditorError> => edit_string,
49
50    /// Edit a mutable string in place using the default editor.
51    ///
52    /// This is a static convenience method equivalent to `EditorCallBuilder::new().edit_string_mut(string)`.
53    ///
54    /// # Errors
55    /// Returns an error if the editor call fails, or if the temporary file cannot be read or cleaned up.
56    edit_string_mut(string: &mut String) -> Result<(), OpenEditorError> => edit_string_mut,
57
58    /// Open a file in the default editor.
59    ///
60    /// This is a static convenience method equivalent to `EditorCallBuilder::new().open_file(file_path)`.
61    ///
62    /// # Errors
63    /// Returns an error if the editor call fails or if the file cannot be read.
64    open_file(file_path: &Path) -> Result<(), OpenEditorError> => open_file,
65}