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
//! # Scrawl
//! A library for opening a file for editing in a text editor and capturing the result as a String
#![deny(
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unstable_features,
    unsafe_code,
    unused_import_braces,
    unused_qualifications
)]

/* Standard Library */
use std::error::Error;
use std::path::Path;

/* Internal Modules */
pub mod editor;
pub use editor::Contents;

/* Convenience functions */
/// New opens an empty text buffer in an editor and returns a Readable struct on success.
///
/// # Example
/// ```no_run
/// # use std::error::Error;
/// # use std::io::Read;
/// # fn main() -> Result<(), Box<dyn Error>> {
///     /* Opens the user's editor */
///     let input = scrawl::new()?;
///     println!("{}", input.to_string()?);
/// #   Ok(())
/// # }
/// ```
pub fn new() -> Result<editor::Reader, Box<dyn Error>> {
    editor::new().open(Contents::Empty)
}

/// With opens a text buffer with the provided contents in an editor. Returns a Readble struct on success.
///
/// # Example
/// ```no_run
/// # use std::error::Error;
/// # use std::io::Read;
/// # fn main() -> Result<(), Box<dyn Error>> {
///     /* Opens the user's editor, buffer pre-filled with custom content */
///     let input = scrawl::with(&"What is your favorite color")?;
///     println!("{}", input.to_string()?);
/// #   Ok(())
/// # }
/// ```
pub fn with<U: AsRef<[u8]>>(input: &U) -> Result<editor::Reader, Box<dyn Error>> {
    editor::new().open(Contents::FromString(input))
}

/// FromFile opens a text buffer with the content of the provided file in an editor. Returns a Readble struct on success.
///
/// # Example
/// ```no_run
/// # use std::error::Error;
/// # use std::io::Read;
/// # fn main() -> Result<(), Box<dyn Error>> {
///     /* Opens the user's editor, buffer pre-filled with custom content */
///     let input = scrawl::from_file(&"foo.txt")?;
///     println!("{}", input.to_string()?);
/// #   Ok(())
/// # }
/// ```
pub fn from_file<P: AsRef<Path>>(path: &P) -> Result<editor::Reader, Box<dyn Error>> {
    editor::new().open(Contents::FromFile(path))
}

/// EditFile opens a text buffer with the content of the provided file, allowing direct editing in an editor. Returns a Readble struct on success.
///
/// # Example
/// ```no_run
/// # use std::error::Error;
/// # use std::io::Read;
/// # fn main() -> Result<(), Box<dyn Error>> {
///     /* Opens the user's editor, buffer pre-filled with custom content */
///     let input = scrawl::edit_file(&"bar.rs")?;
///     println!("{}", input.to_string()?);
/// #   Ok(())
/// # }
/// ```
pub fn edit_file<P: AsRef<Path>>(path: &P) -> Result<editor::Reader, Box<dyn Error>> {
    editor::new().edit(path)
}