1use std::env::temp_dir;
2
3use crate::errors::OpenEditorError;
4
5mod editor;
6pub mod editor_call_builder;
7mod editor_kind;
8pub mod errors;
9
10pub use editor_call_builder::EditorCallBuilder;
11
12static ENV_VARS: &[&str] = &["VISUAL", "EDITOR"];
13
14pub fn edit_in_editor(string: &str) -> Result<String, OpenEditorError> {
22 edit_in_editor_with_env_vars(string, ENV_VARS)
23}
24
25pub fn edit_in_editor_with_env_vars(
28 string: &str,
29 env_vars: &[&str],
30) -> Result<String, OpenEditorError> {
31 let mut filename = temp_dir();
32 filename.push(String::from("open_editor_tmp_file"));
33
34 std::fs::write(&filename, string).map_err(OpenEditorError::FileManipulationFail)?;
36
37 EditorCallBuilder::new_with_env_vars(filename.clone(), env_vars)?.call_editor()?;
38 let result =
39 std::fs::read_to_string(&filename).map_err(OpenEditorError::FileManipulationFail)?;
40
41 std::fs::remove_file(&filename).map_err(|_| {
43 OpenEditorError::TempFileCleanupFail(filename.to_string_lossy().into_owned())
44 })?;
45
46 Ok(result)
47}
48
49pub fn edit_mut_in_editor(string: &mut String) -> Result<(), OpenEditorError> {
58 edit_mut_in_editor_with_env_vars(string, ENV_VARS)
59}
60
61pub fn edit_mut_in_editor_with_env_vars(
64 string: &mut String,
65 env_vars: &[&str],
66) -> Result<(), OpenEditorError> {
67 *string = edit_in_editor_with_env_vars(string, env_vars)?;
68 Ok(())
69}
70
71pub fn open_editor() -> Result<String, OpenEditorError> {
79 open_editor_with_env_vars(ENV_VARS)
80}
81
82pub fn open_editor_with_env_vars(env_vars: &[&str]) -> Result<String, OpenEditorError> {
85 edit_in_editor_with_env_vars("", env_vars)
86}