hglib/commands/
resolve.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this file,
3// You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use crate::client::{Client, HglibError, Runner};
6use crate::{runcommand, MkArg};
7
8pub struct Arg<'a> {
9    pub file: &'a [&'a str],
10    pub all: bool,
11    pub listfiles: bool,
12    pub mark: bool,
13    pub unmark: bool,
14    pub tool: &'a str,
15    pub include: &'a [&'a str],
16    pub exclude: &'a [&'a str],
17}
18
19impl<'a> Default for Arg<'a> {
20    fn default() -> Self {
21        Self {
22            file: &[],
23            all: false,
24            listfiles: false,
25            mark: false,
26            unmark: false,
27            tool: "",
28            include: &[],
29            exclude: &[],
30        }
31    }
32}
33
34impl<'a> Arg<'a> {
35    fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
36        runcommand!(
37            client,
38            "resolve",
39            self.file,
40            "-a",
41            self.all,
42            "-l",
43            self.listfiles,
44            "-m",
45            self.mark,
46            "-u",
47            self.unmark,
48            "-t",
49            self.tool,
50            "-I",
51            self.include,
52            "-X",
53            self.exclude
54        )
55    }
56}
57
58#[derive(Debug, PartialEq)]
59pub enum Kind {
60    Resolved,
61    Unresolved,
62}
63
64#[derive(Debug, PartialEq)]
65pub struct Resolve {
66    pub kind: Kind,
67    pub filename: String,
68}
69
70impl Client {
71    pub fn resolve(&mut self, x: Arg) -> Result<Option<Vec<Resolve>>, HglibError> {
72        let (data, _) = x.run(self)?;
73        if x.listfiles {
74            let mut res = Vec::new();
75            for line in data.split(|c| *c == b'\n') {
76                if line.len() >= 3 {
77                    let filename = unsafe { line.get_unchecked(2..) };
78                    let filename = String::from_utf8(filename.to_vec())?;
79                    let c = unsafe { line.get_unchecked(0) };
80                    let kind = match c {
81                        b'R' => Kind::Resolved,
82                        b'U' => Kind::Unresolved,
83                        _ => {
84                            return Err(HglibError::from("Invalid value"));
85                        }
86                    };
87                    res.push(Resolve { kind, filename });
88                }
89            }
90            Ok(Some(res))
91        } else {
92            Ok(None)
93        }
94    }
95}