rust/chg/src/clientext.rs
author Yuya Nishihara <yuya@tcha.org>
Sat, 11 Apr 2020 00:21:37 +0900
changeset 44693 61fda2dbc522
parent 44690 6bef9d43cc55
child 44847 e9e44e61042b
permissions -rw-r--r--
rust-chg: leverage impl trait at argument position Differential Revision: https://phab.mercurial-scm.org/D8401
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
39978
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
44684
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
     8
use bytes::{BufMut, Bytes, BytesMut};
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     9
use std::ffi::OsStr;
44681
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
    10
use std::io;
44684
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
    11
use std::mem;
39979
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39978
diff changeset
    12
use std::os::unix::ffi::OsStrExt;
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    13
use std::os::unix::io::AsRawFd;
39979
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39978
diff changeset
    14
use std::path::Path;
44681
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
    15
use tokio_hglib::protocol::{OneShotQuery, OneShotRequest};
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    16
use tokio_hglib::{Client, Connection};
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    17
44690
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
    18
use crate::attachio::AttachIo;
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
    19
use crate::message::{self, Instruction};
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
    20
use crate::runcommand::ChgRunCommand;
6bef9d43cc55 rust-chg: use "crate::" to import local modules
Yuya Nishihara <yuya@tcha.org>
parents: 44685
diff changeset
    21
use crate::uihandler::SystemHandler;
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    22
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    23
pub trait ChgClientExt<C>
43836
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    24
where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    25
    C: Connection + AsRawFd,
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    26
{
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    27
    /// 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
    28
    fn attach_io<I, O, E>(self, stdin: I, stdout: O, stderr: E) -> AttachIo<C, I, O, E>
43836
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    29
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    30
        I: AsRawFd,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    31
        O: AsRawFd,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    32
        E: AsRawFd;
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    33
39979
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39978
diff changeset
    34
    /// Changes the working directory of the server.
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    35
    fn set_current_dir(self, dir: impl AsRef<Path>) -> OneShotRequest<C>;
39979
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39978
diff changeset
    36
44676
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43836
diff changeset
    37
    /// Updates the environment variables of the server.
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    38
    fn set_env_vars_os(
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    39
        self,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    40
        vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    41
    ) -> OneShotRequest<C>;
44676
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43836
diff changeset
    42
44685
80d6e3415636 rust-chg: update name of the server process
Yuya Nishihara <yuya@tcha.org>
parents: 44684
diff changeset
    43
    /// Changes the process title of the server.
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    44
    fn set_process_name(self, name: impl AsRef<OsStr>) -> OneShotRequest<C>;
44685
80d6e3415636 rust-chg: update name of the server process
Yuya Nishihara <yuya@tcha.org>
parents: 44684
diff changeset
    45
44684
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
    46
    /// Changes the umask of the server process.
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
    47
    fn set_umask(self, mask: u32) -> OneShotRequest<C>;
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
    48
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    49
    /// Runs the specified Mercurial command with cHg extension.
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    50
    fn run_command_chg<H>(
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    51
        self,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    52
        handler: H,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    53
        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    54
    ) -> ChgRunCommand<C, H>
43836
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    55
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    56
        H: SystemHandler;
44681
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
    57
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
    58
    /// 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: 44676
diff changeset
    59
    /// configuration.
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
    60
    ///
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
    61
    /// 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: 44676
diff changeset
    62
    /// and `-R`.
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
    63
    ///
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
    64
    /// 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: 44676
diff changeset
    65
    /// `set_current_dir()` and `set_env_vars_os()`.
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    66
    fn validate(
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    67
        self,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    68
        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    69
    ) -> OneShotQuery<C, fn(Bytes) -> io::Result<Vec<Instruction>>>;
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    70
}
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    71
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    72
impl<C> ChgClientExt<C> for Client<C>
43836
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    73
where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    74
    C: Connection + AsRawFd,
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    75
{
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    76
    fn attach_io<I, O, E>(self, stdin: I, stdout: O, stderr: E) -> AttachIo<C, I, O, E>
43836
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    77
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    78
        I: AsRawFd,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    79
        O: AsRawFd,
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
    80
        E: AsRawFd,
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    81
    {
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    82
        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
    83
    }
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    84
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    85
    fn set_current_dir(self, dir: impl AsRef<Path>) -> OneShotRequest<C> {
39979
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39978
diff changeset
    86
        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: 39978
diff changeset
    87
    }
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39978
diff changeset
    88
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    89
    fn set_env_vars_os(
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    90
        self,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    91
        vars: impl IntoIterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    92
    ) -> OneShotRequest<C> {
44676
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43836
diff changeset
    93
        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: 43836
diff changeset
    94
    }
97e6d435ff7e rust-chg: send client-side environment variables to server
Yuya Nishihara <yuya@tcha.org>
parents: 43836
diff changeset
    95
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
    96
    fn set_process_name(self, name: impl AsRef<OsStr>) -> OneShotRequest<C> {
44685
80d6e3415636 rust-chg: update name of the server process
Yuya Nishihara <yuya@tcha.org>
parents: 44684
diff changeset
    97
        OneShotRequest::start_with_args(self, b"setprocname", name.as_ref().as_bytes())
80d6e3415636 rust-chg: update name of the server process
Yuya Nishihara <yuya@tcha.org>
parents: 44684
diff changeset
    98
    }
80d6e3415636 rust-chg: update name of the server process
Yuya Nishihara <yuya@tcha.org>
parents: 44684
diff changeset
    99
44684
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
   100
    fn set_umask(self, mask: u32) -> OneShotRequest<C> {
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
   101
        let mut args = BytesMut::with_capacity(mem::size_of_val(&mask));
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
   102
        args.put_u32_be(mask);
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
   103
        OneShotRequest::start_with_args(self, b"setumask2", args)
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
   104
    }
065048e66f32 rust-chg: send client side umask to server
Yuya Nishihara <yuya@tcha.org>
parents: 44681
diff changeset
   105
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   106
    fn run_command_chg<H>(
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   107
        self,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   108
        handler: H,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   109
        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   110
    ) -> ChgRunCommand<C, H>
43836
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
   111
    where
ce088b38f92b rust: run rustfmt
Gregory Szorc <gregory.szorc@gmail.com>
parents: 39979
diff changeset
   112
        H: SystemHandler,
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   113
    {
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   114
        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
   115
    }
44681
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
   116
44693
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   117
    fn validate(
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   118
        self,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   119
        args: impl IntoIterator<Item = impl AsRef<OsStr>>,
61fda2dbc522 rust-chg: leverage impl trait at argument position
Yuya Nishihara <yuya@tcha.org>
parents: 44690
diff changeset
   120
    ) -> OneShotQuery<C, fn(Bytes) -> io::Result<Vec<Instruction>>> {
44681
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
   121
        OneShotQuery::start_with_args(
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
   122
            self,
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
   123
            b"validate",
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
   124
            message::pack_args_os(args),
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
   125
            message::parse_instructions,
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
   126
        )
43513444bb88 rust-chg: add interface to run "validate" request
Yuya Nishihara <yuya@tcha.org>
parents: 44676
diff changeset
   127
    }
39978
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   128
}