pub fn load_with_config<P: AsRef<Path>>(
        &self,
        config: P,
        allow_zero: bool,
    ) -> io::Result<Option<Value>> {
        let cmd = self.cmd;
        let scope = self.scope.as_str();
        let var = self.name.as_ref();

        let full_cmd = {
            if config.as_ref().as_os_str().is_empty() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "path to vimrc is required for neovim/vim",
                ));
            }

            // NOTE: We have a lot of settings being applied, so documenting
            //       them here
            //
            //       1. -Es is our silent, batch, ex mode
            //       2. -i NONE removes shada/viminfo file reading and writing
            //       3. -u "{}" loads our vimrc, which is required as -Es does
            //          not load vim scripts by default
            //       4. +set nonumber is used to turn off line numbers, which
            //          are getting picked up by neovim/vim in vimrc configs
            //          and showing up in output
            //       5. redir writes to a register our message (json) and then
            //          places it in our buffer
            //       6. prints out the content in our buffer (current line)
            format!(
                r#"{} -Es -i NONE -u "{}" '+set nonumber' '+redir => m | echon json_encode(get({}, "{}")) | redir END | put=m' '+%p' '+qa!'"#,
                cmd,
                config.as_ref().to_string_lossy(),
                scope,
                var,
            )
        };

        // TODO: Support windows here (won't have sh)
        let output = Command::new("sh").arg("-c").arg(full_cmd).output()?;

        // If our program failed, we want to report the failure
        //
        // NOTE: neovim/vim seems to return exit code 1; so, for now we'll
        //       ignore that specific exit code for now
        if !output.status.success() && (output.status.code() != Some(1)) {
            let code = output
                .status
                .code()
                .as_ref()
                .map(ToString::to_string)
                .unwrap_or_else(|| String::from("--"));
            return Err(io::Error::new(
                io::ErrorKind::Other,
                format!(
                    "[Exit code {}]: {}",
                    code,
                    String::from_utf8_lossy(&output.stderr).trim()
                )
                .as_str(),
            ));
        }

        let output_string = String::from_utf8_lossy(&output.stdout);

        // Report a better error than the serde one if the output was empty
        if output_string.trim().is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Result from {} was empty", self.cmd),
            ));
        }

        let value: Value = serde_json::from_str(output_string.trim()).map_err(|_| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Failed to parse as JSON: \"{}\"", output_string.trim()),
            )
        })?;

        if !allow_zero && value == serde_json::json!(0) {
            Ok(None)
        } else {
            Ok(Some(value))
        }
    }
/// Loads the variable's value using neovim's headless mode or vim's ex
/// mode
///
/// ### Notes
///
/// * Spawns a vim process whose goal is to print out the contents of a
///   variable as a JSON string
/// * Leverages batch & ex modes with redir to execute and capture output
/// * Relies on the variable being available upon loading vim configs
/// * If `allow_zero` is true, then a value of 0 is considered the value of
///   the variable rather than vim's default of not being found
Loads the variable's value using neovim's headless mode or vim's ex
mode
Loads the variable ' s value using neovim ' s headless mode or vim ' s ex mode
pub fn load_with_config<P: AsRef<Path>>(
        &self,
        config: P,
        allow_zero: bool,
    ) -> io::Result<Option<Value>> {
        let cmd = self.cmd;
        let scope = self.scope.as_str();
        let var = self.name.as_ref();
        let full_cmd = {
            if config.as_ref().as_os_str().is_empty() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "path to vimrc is required for neovim/vim",
                ));
            }
            // NOTE: We have a lot of settings being applied, so documenting
            //       them here
            //
            //       1. -Es is our silent, batch, ex mode
            //       2. -i NONE removes shada/viminfo file reading and writing
            //       3. -u "{}" loads our vimrc, which is required as -Es does
            //          not load vim scripts by default
            //       4. +set nonumber is used to turn off line numbers, which
            //          are getting picked up by neovim/vim in vimrc configs
            //          and showing up in output
            //       5. redir writes to a register our message (json) and then
            //          places it in our buffer
            //       6. prints out the content in our buffer (current line)
            format!(
                r#"{} -Es -i NONE -u "{}" '+set nonumber' '+redir => m | echon json_encode(get({}, "{}")) | redir END | put=m' '+%p' '+qa!'"#,
                cmd,
                config.as_ref().to_string_lossy(),
                scope,
                var,
            )
        };
        // TODO: Support windows here (won't have sh)
        let output = Command::new("sh").arg("-c").arg(full_cmd).output()?;
        // If our program failed, we want to report the failure
        //
        // NOTE: neovim/vim seems to return exit code 1; so, for now we'll
        //       ignore that specific exit code for now
        if !output.status.success() && (output.status.code() != Some(1)) {
            let code = output
                .status
                .code()
                .as_ref()
                .map(ToString::to_string)
                .unwrap_or_else(|| String::from("--"));
            return Err(io::Error::new(
                io::ErrorKind::Other,
                format!(
                    "[Exit code {}]: {}",
                    code,
                    String::from_utf8_lossy(&output.stderr).trim()
                )
                .as_str(),
            ));
        }
        let output_string = String::from_utf8_lossy(&output.stdout);
        // Report a better error than the serde one if the output was empty
        if output_string.trim().is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Result from {} was empty", self.cmd),
            ));
        }
        let value: Value = serde_json::from_str(output_string.trim()).map_err(|_| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Failed to parse as JSON: \"{}\"", output_string.trim()),
            )
        })?;
        if !allow_zero && value == serde_json::json!(0) {
            Ok(None)
        } else {
            Ok(Some(value))
        }
    }
pub fn load_with_config < P : AsRef < Path > > ( & self , config : P , allow_zero : bool , ) -> io :: Result < Option < Value > > { let cmd = self . cmd ; let scope = self . scope . as_str ( ) ; let var = self . name . as_ref ( ) ; let full_cmd = { if config . as_ref ( ) . as_os_str ( ) . is_empty ( ) { return Err ( io :: Error :: new ( io :: ErrorKind :: InvalidInput , " " , ) ) ; } // NOTE: We have a lot of settings being applied, so documenting //       them here // //       1. -Es is our silent, batch, ex mode //       2. -i NONE removes shada/viminfo file reading and writing //       3. -u "{}" loads our vimrc, which is required as -Es does //          not load vim scripts by default //       4. +set nonumber is used to turn off line numbers, which //          are getting picked up by neovim/vim in vimrc configs //          and showing up in output //       5. redir writes to a register our message (json) and then //          places it in our buffer //       6. prints out the content in our buffer (current line) format ! ( r#"{} -Es -i NONE -u "{}" '+set nonumber' '+redir => m | echon json_encode(get({}, "{}")) | redir END | put=m' '+%p' '+qa!'"# cmd config as_ref ( ) to_string_lossy ( ) scope var ) } ; // TODO: Support windows here (won't have sh) let output = Command :: new ( " " ) . arg ( " " ) . arg ( full_cmd ) . output ( ) ? ; // If our program failed, we want to report the failure // // NOTE: neovim/vim seems to return exit code 1; so, for now we'll //       ignore that specific exit code for now if ! output . status . success ( ) && ( output . status . code ( ) != Some ( 1 ) ) { let code = output . status . code ( ) . as_ref ( ) . map ( ToString :: to_string ) . unwrap_or_else ( | | String :: from ( " " ) ) ; return Err ( io :: Error :: new ( io :: ErrorKind :: Other , format ! ( " " code String from_utf8_lossy ( output stderr ) trim ( ) ) . as_str ( ) , ) ) ; } let output_string = String :: from_utf8_lossy ( & output . stdout ) ; // Report a better error than the serde one if the output was empty if output_string . trim ( ) . is_empty ( ) { return Err ( io :: Error :: new ( io :: ErrorKind :: InvalidData , format ! ( " " self cmd ) , ) ) ; } let value : Value = serde_json :: from_str ( output_string . trim ( ) ) . map_err ( | _ | { io :: Error :: new ( io :: ErrorKind :: InvalidData , format ! ( " \" \" " output_string trim ( ) ) , ) } ) ? ; if ! allow_zero && value == serde_json :: json ! ( 0 ) { Ok ( None ) } else { Ok ( Some ( value ) ) } }
Loads the variable's value using neovim's headless mode or vim's ex
mode
Loads the variable ' s value using neovim ' s headless mode or vim ' s ex mode
&self
config
P
allow_zero
bool
notes
Spawns a vim process whose goal is to print out the contents of a
variable as a JSON string
Leverages batch & ex modes with redir to execute and capture output
Relies on the variable being available upon loading vim configs
If `allow_zero` is true, then a value of 0 is considered the value of
the variable rather than vim's default of not being found
Spawns a vim process whose goal is to print out the contents of a variable as a JSON string Leverages batch & ex modes with redir to execute and capture output Relies on the variable being available upon loading vim configs If ` allow_zero ` is true then a value of 0 is considered the value of the variable rather than vim ' s default of not being found