hglib/commands/
branches.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 {
9    pub active: bool,
10    pub closed: bool,
11}
12
13impl Default for Arg {
14    fn default() -> Self {
15        Self {
16            active: false,
17            closed: false,
18        }
19    }
20}
21
22impl Arg {
23    fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
24        runcommand!(
25            client,
26            "branches",
27            &[""],
28            "-a",
29            self.active,
30            "-c",
31            self.closed
32        )
33    }
34}
35
36#[derive(Debug, PartialEq)]
37pub struct Branch {
38    pub name: String,
39    pub rev: u64,
40    pub node: String,
41}
42
43impl Client {
44    pub fn branches(&mut self, x: Arg) -> Result<Vec<Branch>, HglibError> {
45        let (data, _) = x.run(self)?;
46        let mut branches = Vec::new();
47        for line in data.split(|x| *x == b'\n').filter(|x| !x.is_empty()) {
48            let mut iter = line.split(|x| *x == b' ').filter(|x| !x.is_empty());
49            let name = iter.next().unwrap();
50            let name = String::from_utf8(name.to_vec())?;
51            let rev_node = iter.next().unwrap();
52            let iter = &mut rev_node.iter();
53            let rev = iter
54                .take_while(|x| **x != b':')
55                .fold(0, |r, x| r * 10 + u64::from(*x - b'0'));
56            let node = iter.as_slice();
57            let node = String::from_utf8(node.to_vec())?;
58
59            branches.push(Branch { name, rev, node });
60        }
61        Ok(branches)
62    }
63}