1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.

use crate::client::{Client, HglibError, Runner};
use crate::{runcommand, MkArg};

pub struct Arg<'a> {
    pub rev: &'a str,
    pub all: bool,
}

impl<'a> Default for Arg<'a> {
    fn default() -> Self {
        Self {
            rev: "",
            all: false,
        }
    }
}

impl<'a> Arg<'a> {
    fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
        runcommand!(
            client,
            "manifest",
            &[""],
            "-r",
            self.rev,
            "--all",
            self.all,
            "--debug",
            true
        )
    }
}

#[derive(Debug, PartialEq)]
pub struct File {
    pub node: String,
    pub perm: String,
    pub symlink: bool,
    pub executable: bool,
    pub filename: String,
}

#[derive(Debug, PartialEq)]
pub enum Manifest {
    All(Vec<String>),
    Info(Vec<File>),
}

impl Client {
    pub fn manifest(&mut self, x: Arg) -> Result<Manifest, HglibError> {
        let (data, _) = x.run(self)?;
        if x.all {
            Ok(Manifest::All(
                data.split(|c| *c == b'\n')
                    .filter(|l| !l.is_empty())
                    .map(|s| String::from_utf8(s.to_vec()).unwrap())
                    .collect(),
            ))
        } else {
            let mut res = Vec::new();
            for line in data.split(|c| *c == b'\n').filter(|l| !l.is_empty()) {
                if line.len() >= 48 {
                    res.push(File {
                        node: String::from_utf8(unsafe { line.get_unchecked(..40).to_vec() })?,
                        perm: String::from_utf8(unsafe { line.get_unchecked(41..44).to_vec() })?,
                        symlink: unsafe { *line.get_unchecked(45) == b'@' },
                        executable: unsafe { *line.get_unchecked(45) == b'*' },
                        filename: String::from_utf8(unsafe { line.get_unchecked(47..).to_vec() })?,
                    });
                } else {
                    return Err(HglibError::from(format!(
                        "Hglib error: invalid length for line: {} <= 48",
                        line.len()
                    )));
                }
            }
            Ok(Manifest::Info(res))
        }
    }
}