rust/chg/src/clientext.rs
author Georges Racinet <gracinet@anybox.fr>
Fri, 30 Nov 2018 00:46:55 +0100
changeset 40959 d097dd0afc19
parent 39978 045ea159418d
child 43818 ce088b38f92b
permissions -rw-r--r--
rust: translation of missingancestors This is as direct as possible a translation of the ancestor.missingancestors Python class in pure Rust. The goal for this changeset is to make it easy to compare with the Python version. We also add to Python tests the cases that helped us develop and debug this implementation. Some possible optimizations are marked along the way as TODO comments Differential Revision: https://phab.mercurial-scm.org/D5416
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
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     8
use std::ffi::OsStr;
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
     9
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
    10
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
    11
use std::path::Path;
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    12
use tokio_hglib::{Client, Connection};
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    13
use tokio_hglib::protocol::OneShotRequest;
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    14
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    15
use super::attachio::AttachIo;
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    16
use super::message;
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    17
use super::runcommand::ChgRunCommand;
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    18
use super::uihandler::SystemHandler;
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    19
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    20
pub trait ChgClientExt<C>
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    21
    where C: Connection + AsRawFd,
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
    /// 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
    24
    fn attach_io<I, O, E>(self, stdin: I, stdout: O, stderr: E) -> AttachIo<C, I, O, E>
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    25
        where I: AsRawFd,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    26
              O: AsRawFd,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    27
              E: AsRawFd;
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    28
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    29
    /// 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
    30
    fn set_current_dir<P>(self, dir: P) -> OneShotRequest<C>
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    31
        where P: AsRef<Path>;
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    32
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    33
    /// 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
    34
    fn run_command_chg<I, P, H>(self, handler: H, args: I) -> ChgRunCommand<C, H>
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    35
        where I: IntoIterator<Item = P>,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    36
              P: AsRef<OsStr>,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    37
              H: SystemHandler;
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    38
}
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    39
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    40
impl<C> ChgClientExt<C> for Client<C>
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    41
    where C: Connection + AsRawFd,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    42
{
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    43
    fn attach_io<I, O, E>(self, stdin: I, stdout: O, stderr: E) -> AttachIo<C, I, O, E>
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    44
        where I: AsRawFd,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    45
              O: AsRawFd,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    46
              E: AsRawFd,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    47
    {
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    48
        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
    49
    }
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    50
39978
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    51
    fn set_current_dir<P>(self, dir: P) -> OneShotRequest<C>
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    52
        where P: AsRef<Path>,
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    53
    {
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    54
        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
    55
    }
045ea159418d rust-chg: add interface to chdir the server
Yuya Nishihara <yuya@tcha.org>
parents: 39977
diff changeset
    56
39977
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    57
    fn run_command_chg<I, P, H>(self, handler: H, args: I) -> ChgRunCommand<C, H>
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    58
        where I: IntoIterator<Item = P>,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    59
              P: AsRef<OsStr>,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    60
              H: SystemHandler,
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    61
    {
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    62
        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
    63
    }
74da9d999cd7 rust-chg: add Client extensions to run cHg-specific requests
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    64
}