rust/chg/src/clientext.rs
author Yuya Nishihara <yuya@tcha.org>
Sun, 07 Oct 2018 15:36:34 +0900
changeset 44680 43513444bb88
parent 44675 97e6d435ff7e
child 44683 065048e66f32
permissions -rw-r--r--
rust-chg: add interface to run "validate" request Differential Revision: https://phab.mercurial-scm.org/D8379
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     1
// Copyright 2018 Yuya Nishihara <yuya@tcha.org>
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     2
//
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     3
// This software may be used and distributed according to the terms of the
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     4
// GNU General Public License version 2 or any later version.
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     5
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     6
//! cHg extensions to command server client.
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     7
44680
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
     8
use bytes::Bytes;
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     9
use std::ffi::OsStr;
44680
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    10
use std::io;
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    11
use std::os::unix::ffi::OsStrExt;
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    12
use std::os::unix::io::AsRawFd;
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    13
use std::path::Path;
44680
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    14
use tokio_hglib::protocol::{OneShotQuery, OneShotRequest};
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    15
use tokio_hglib::{Client, Connection};
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    16
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    17
use super::attachio::AttachIo;
44680
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    18
use super::message::{self, Instruction};
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    19
use super::runcommand::ChgRunCommand;
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    20
use super::uihandler::SystemHandler;
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    21
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    22
pub trait ChgClientExt<C>
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    23
where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    24
    C: Connection + AsRawFd,
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    25
{
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    26
    /// Attaches the client file descriptors to the server.
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    27
    fn attach_io<I, O, E>(self, stdin: I, stdout: O, stderr: E) -> AttachIo<C, I, O, E>
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    28
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    29
        I: AsRawFd,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    30
        O: AsRawFd,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    31
        E: AsRawFd;
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    32
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    33
    /// Changes the working directory of the server.
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    34
    fn set_current_dir<P>(self, dir: P) -> OneShotRequest<C>
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    35
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    36
        P: AsRef<Path>;
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    37
44675
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    38
    /// Updates the environment variables of the server.
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    39
    fn set_env_vars_os<I, P>(self, vars: I) -> OneShotRequest<C>
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    40
    where
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    41
        I: IntoIterator<Item = (P, P)>,
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    42
        P: AsRef<OsStr>;
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    43
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    44
    /// Runs the specified Mercurial command with cHg extension.
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    45
    fn run_command_chg<I, P, H>(self, handler: H, args: I) -> ChgRunCommand<C, H>
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    46
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    47
        I: IntoIterator<Item = P>,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    48
        P: AsRef<OsStr>,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    49
        H: SystemHandler;
44680
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    50
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    51
    /// Validates if the server can run Mercurial commands with the expected
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    52
    /// configuration.
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    53
    ///
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    54
    /// The `args` should contain early command arguments such as `--config`
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    55
    /// and `-R`.
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    56
    ///
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    57
    /// Client-side environment must be sent prior to this request, by
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    58
    /// `set_current_dir()` and `set_env_vars_os()`.
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    59
    fn validate<I, P>(self, args: I) -> OneShotQuery<C, fn(Bytes) -> io::Result<Vec<Instruction>>>
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    60
    where
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    61
        I: IntoIterator<Item = P>,
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
    62
        P: AsRef<OsStr>;
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    63
}
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    64
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    65
impl<C> ChgClientExt<C> for Client<C>
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    66
where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    67
    C: Connection + AsRawFd,
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    68
{
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    69
    fn attach_io<I, O, E>(self, stdin: I, stdout: O, stderr: E) -> AttachIo<C, I, O, E>
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    70
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    71
        I: AsRawFd,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    72
        O: AsRawFd,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    73
        E: AsRawFd,
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    74
    {
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    75
        AttachIo::with_client(self, stdin, stdout, Some(stderr))
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    76
    }
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    77
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    78
    fn set_current_dir<P>(self, dir: P) -> OneShotRequest<C>
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    79
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    80
        P: AsRef<Path>,
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    81
    {
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    82
        OneShotRequest::start_with_args(self, b"chdir", dir.as_ref().as_os_str().as_bytes())
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    83
    }
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    84
44675
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    85
    fn set_env_vars_os<I, P>(self, vars: I) -> OneShotRequest<C>
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    86
    where
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    87
        I: IntoIterator<Item = (P, P)>,
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    88
        P: AsRef<OsStr>,
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    89
    {
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    90
        OneShotRequest::start_with_args(self, b"setenv", message::pack_env_vars_os(vars))
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    91
    }
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43818
diff changeset
    92
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    93
    fn run_command_chg<I, P, H>(self, handler: H, args: I) -> ChgRunCommand<C, H>
43818
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    94
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    95
        I: IntoIterator<Item = P>,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    96
        P: AsRef<OsStr>,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39978
diff changeset
    97
        H: SystemHandler,
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    98
    {
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    99
        ChgRunCommand::with_client(self, handler, message::pack_args_os(args))
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   100
    }
44680
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   101
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   102
    fn validate<I, P>(self, args: I) -> OneShotQuery<C, fn(Bytes) -> io::Result<Vec<Instruction>>>
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   103
    where
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   104
        I: IntoIterator<Item = P>,
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   105
        P: AsRef<OsStr>,
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   106
    {
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   107
        OneShotQuery::start_with_args(
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   108
            self,
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   109
            b"validate",
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   110
            message::pack_args_os(args),
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   111
            message::parse_instructions,
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   112
        )
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44675
diff changeset
   113
    }
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   114
}