hglib/commands/
phase.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 revs: &'a [&'a str],
10    pub secret: bool,
11    pub draft: bool,
12    pub public: bool,
13    pub force: bool,
14}
15
16impl<'a> Default for Arg<'a> {
17    fn default() -> Self {
18        Self {
19            revs: &[],
20            secret: false,
21            draft: false,
22            public: false,
23            force: false,
24        }
25    }
26}
27
28impl<'a> Arg<'a> {
29    fn run(&self, client: &mut Client) -> Result<(Vec<u8>, i32), HglibError> {
30        runcommand!(
31            client,
32            "phase",
33            self.revs,
34            "--secret",
35            self.secret,
36            "--draft",
37            self.draft,
38            "--public",
39            self.public,
40            "--force",
41            self.force
42        )
43    }
44}
45
46#[derive(Debug, PartialEq)]
47pub struct Phase {
48    pub num: u64,
49    pub phase: String,
50}
51
52impl Client {
53    pub fn phase(&mut self, x: Arg) -> Result<Option<Vec<Phase>>, HglibError> {
54        let (data, _) = x.run(self)?;
55        if x.draft || x.public || x.secret {
56            Ok(None)
57        } else {
58            let mut phases = Vec::new();
59            let mut num: Option<u64> = None;
60            for elem in data
61                .split(|x| *x == b'\n' || *x == b' ' || *x == b':')
62                .filter(|x| !x.is_empty())
63            {
64                num = if let Some(num) = num {
65                    let phase = String::from_utf8(elem.to_vec())?;
66                    phases.push(Phase { num, phase });
67                    None
68                } else {
69                    let num = elem.iter().fold(0, |r, x| r * 10 + u64::from(*x - b'0'));
70                    Some(num)
71                }
72            }
73            Ok(Some(phases))
74        }
75    }
76}