scrawl/
lib.rs

1//! # Scrawl
2//! A library for opening a file for editing in a text editor and capturing the result as a String
3#![deny(
4    missing_docs,
5    missing_debug_implementations,
6    missing_copy_implementations,
7    trivial_casts,
8    trivial_numeric_casts,
9    unstable_features,
10    unsafe_code,
11    unused_import_braces,
12    unused_qualifications
13)]
14
15/* Standard Library */
16use std::error::Error;
17use std::path::Path;
18
19/* Internal Modules */
20pub mod editor;
21pub use editor::Contents;
22
23/* Convenience functions */
24/// New opens an empty text buffer in an editor and returns a Readable struct on success.
25///
26/// # Example
27/// ```no_run
28/// # use std::error::Error;
29/// # use std::io::Read;
30/// # fn main() -> Result<(), Box<dyn Error>> {
31///     /* Opens the user's editor */
32///     let input = scrawl::new()?;
33///     println!("{}", input.to_string()?);
34/// #   Ok(())
35/// # }
36/// ```
37pub fn new() -> Result<editor::Reader, Box<dyn Error>> {
38    editor::new().open(Contents::Empty)
39}
40
41/// With opens a text buffer with the provided contents in an editor. Returns a Readble struct on success.
42///
43/// # Example
44/// ```no_run
45/// # use std::error::Error;
46/// # use std::io::Read;
47/// # fn main() -> Result<(), Box<dyn Error>> {
48///     /* Opens the user's editor, buffer pre-filled with custom content */
49///     let input = scrawl::with(&"What is your favorite color")?;
50///     println!("{}", input.to_string()?);
51/// #   Ok(())
52/// # }
53/// ```
54pub fn with<U: AsRef<[u8]>>(input: &U) -> Result<editor::Reader, Box<dyn Error>> {
55    editor::new().open(Contents::FromString(input))
56}
57
58/// FromFile opens a text buffer with the content of the provided file in an editor. Returns a Readble struct on success.
59///
60/// # Example
61/// ```no_run
62/// # use std::error::Error;
63/// # use std::io::Read;
64/// # fn main() -> Result<(), Box<dyn Error>> {
65///     /* Opens the user's editor, buffer pre-filled with custom content */
66///     let input = scrawl::from_file(&"foo.txt")?;
67///     println!("{}", input.to_string()?);
68/// #   Ok(())
69/// # }
70/// ```
71pub fn from_file<P: AsRef<Path>>(path: &P) -> Result<editor::Reader, Box<dyn Error>> {
72    editor::new().open(Contents::FromFile(path))
73}
74
75/// EditFile opens a text buffer with the content of the provided file, allowing direct editing in an editor. Returns a Readble struct on success.
76///
77/// # Example
78/// ```no_run
79/// # use std::error::Error;
80/// # use std::io::Read;
81/// # fn main() -> Result<(), Box<dyn Error>> {
82///     /* Opens the user's editor, buffer pre-filled with custom content */
83///     let input = scrawl::edit_file(&"bar.rs")?;
84///     println!("{}", input.to_string()?);
85/// #   Ok(())
86/// # }
87/// ```
88pub fn edit_file<P: AsRef<Path>>(path: &P) -> Result<editor::Reader, Box<dyn Error>> {
89    editor::new().edit(path)
90}