1use bstr::BString;
2
3use crate::{
4 instruction::{Fetch, Push},
5 Instruction, RefSpecRef,
6};
7
8impl RefSpecRef<'_> {
9 pub fn to_bstring(&self) -> BString {
11 let mut buf = Vec::with_capacity(128);
12 self.write_to(&mut buf).expect("no io error");
13 buf.into()
14 }
15
16 pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
18 self.instruction().write_to(out)
19 }
20}
21
22impl Instruction<'_> {
23 pub fn to_bstring(&self) -> BString {
25 let mut buf = Vec::with_capacity(128);
26 self.write_to(&mut buf).expect("no io error");
27 buf.into()
28 }
29
30 pub fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
32 match self {
33 Instruction::Push(Push::Matching {
34 src,
35 dst,
36 allow_non_fast_forward,
37 }) => {
38 if *allow_non_fast_forward {
39 out.write_all(b"+")?;
40 }
41 out.write_all(src)?;
42 out.write_all(b":")?;
43 out.write_all(dst)
44 }
45 Instruction::Push(Push::AllMatchingBranches { allow_non_fast_forward }) => {
46 if *allow_non_fast_forward {
47 out.write_all(b"+")?;
48 }
49 out.write_all(b":")
50 }
51 Instruction::Push(Push::Delete { ref_or_pattern }) => {
52 out.write_all(b":")?;
53 out.write_all(ref_or_pattern)
54 }
55 Instruction::Fetch(Fetch::Only { src }) => out.write_all(src),
56 Instruction::Fetch(Fetch::Exclude { src }) => {
57 out.write_all(b"^")?;
58 out.write_all(src)
59 }
60 Instruction::Fetch(Fetch::AndUpdate {
61 src,
62 dst,
63 allow_non_fast_forward,
64 }) => {
65 if *allow_non_fast_forward {
66 out.write_all(b"+")?;
67 }
68 out.write_all(src)?;
69 out.write_all(b":")?;
70 out.write_all(dst)
71 }
72 }
73 }
74}