EditorBuilder

Struct EditorBuilder 

Source
pub struct EditorBuilder<'a> { /* private fields */ }
Expand description

A builder for customizing an Editor. In simple cases you can just use Editor::new and don’t have to interact with this struct. See crate-level documentation for more details and examples.

§Example

The builder works by calling one or more “source” methods. Each source may (or may not) provide an editor command. The first source that provides a command will be used, and subsequent sources will be ignored. For example, here’s a builder that uses 3 sources:

  • User’s configured editor
  • Environment variables
  • Static fallback
use editor_command::EditorBuilder;
use std::process::Command;

std::env::set_var("VISUAL", "vim"); // This gets overridden
let editor = EditorBuilder::new()
    .string(configured_editor())
    .environment()
    // If both VISUAL and EDITOR are undefined, we'll fall back to this
    .string(Some("vi"))
    .build()
    .unwrap();
let command = editor.open("file.txt");

assert_eq!(command.get_program(), "code");
assert_eq!(command.get_args().collect::<Vec<_>>(), &["--wait", "file.txt"]);

fn configured_editor() -> Option<String> {
    // In reality this would load from a config file or similar
    Some("code --wait".into())
}

§Lifetimes

EditorBuilder accepts a lifetime parameter, which is bound to the string data it contains (both command strings and paths). This is to prevent unnecessary cloning when building editors from &strs. If you need the instance of EditorBuilder to be 'static, e.g. so it can be returned from a function, you can simply use EditorBuilder<'static>. Internally, all strings are stored as Cows, so clones will be made as necessary. Once the builder is converted into an Editor, all strings will be cloned.

use editor_command::EditorBuilder;

/// This is a contrived example of returning a command with owned data
fn get_editor_builder<'a>(command: &'a str) -> EditorBuilder<'static> {
    // The lifetime bounds enforce the .to_owned() call
    EditorBuilder::new().string(Some(command.to_owned()))
}

let editor = get_editor_builder("vim").build().unwrap();
assert_eq!(editor.open("file").get_program(), "vim");

Implementations§

Source§

impl<'a> EditorBuilder<'a>

Source

pub fn new() -> Self

Create a new editor command with no sources. You probably want to call environment on the returned value.

Source

pub fn string(self, source: Option<impl Into<Cow<'a, str>>>) -> Self

Load the editor command from a string. This is useful for static defaults or external sources such as a configuration file. This accepts an Option so you can easily build a chain of sources that may or may not be defined.

Source

pub fn environment(self) -> Self

Load the editor command from the VISUAL and EDITOR environment variables, in that order. The variables will be evaluated immediately, not during build.

Source

pub fn build(self) -> Result<Editor, EditorBuilderError>

Search all configured sources (in their order of definition), and parse the first one that’s populated as a shell command. Then use that to build an executable Command.

Trait Implementations§

Source§

impl<'a> Clone for EditorBuilder<'a>

Source§

fn clone(&self) -> EditorBuilder<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for EditorBuilder<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for EditorBuilder<'a>

Source§

fn default() -> EditorBuilder<'a>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for EditorBuilder<'a>

§

impl<'a> RefUnwindSafe for EditorBuilder<'a>

§

impl<'a> Send for EditorBuilder<'a>

§

impl<'a> Sync for EditorBuilder<'a>

§

impl<'a> Unpin for EditorBuilder<'a>

§

impl<'a> UnwindSafe for EditorBuilder<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.