/src/gitoxide/gix-packetline/src/blocking_io/write.rs
Line | Count | Source |
1 | | use std::io; |
2 | | |
3 | | use crate::{MAX_DATA_LEN, U16_HEX_BYTES, blocking_io::encode}; |
4 | | |
5 | | /// An implementor of [`Write`][io::Write] which passes all input to an inner `Write` in packet line data encoding, |
6 | | /// one line per `write(…)` call or as many lines as it takes if the data doesn't fit into the maximum allowed line length. |
7 | | pub struct Writer<T> { |
8 | | /// the `Write` implementation to which to propagate packet lines |
9 | | inner: T, |
10 | | binary: bool, |
11 | | } |
12 | | |
13 | | impl<T: io::Write> Writer<T> { |
14 | | /// Create a new instance from the given `write` |
15 | 737 | pub fn new(write: T) -> Self { |
16 | 737 | Writer { |
17 | 737 | inner: write, |
18 | 737 | binary: true, |
19 | 737 | } |
20 | 737 | } <gix_packetline::blocking_io::write::Writer<alloc::vec::Vec<u8>>>::new Line | Count | Source | 15 | 737 | pub fn new(write: T) -> Self { | 16 | 737 | Writer { | 17 | 737 | inner: write, | 18 | 737 | binary: true, | 19 | 737 | } | 20 | 737 | } |
Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::new Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<alloc::boxed::Box<dyn core::io::write::Write>>>::new Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<&mut std::process::ChildStdin>>::new Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::new Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<std::process::ChildStdin>>::new Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<std::io::stdio::StdoutLock>>::new Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::new |
21 | | } |
22 | | |
23 | | /// Non-IO methods |
24 | | impl<T> Writer<T> { |
25 | | /// If called, each call to [`write()`][io::Write::write()] will write bytes as is. |
26 | 491 | pub fn enable_binary_mode(&mut self) { |
27 | 491 | self.binary = true; |
28 | 491 | } <gix_packetline::blocking_io::write::Writer<alloc::vec::Vec<u8>>>::enable_binary_mode Line | Count | Source | 26 | 491 | pub fn enable_binary_mode(&mut self) { | 27 | 491 | self.binary = true; | 28 | 491 | } |
Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::enable_binary_mode Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<alloc::boxed::Box<dyn core::io::write::Write>>>::enable_binary_mode Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<&mut std::process::ChildStdin>>::enable_binary_mode Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::enable_binary_mode Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::enable_binary_mode |
29 | | /// If called, each call to [`write()`][io::Write::write()] will write the input as text, appending a trailing newline |
30 | | /// if needed before writing. |
31 | 246 | pub fn enable_text_mode(&mut self) { |
32 | 246 | self.binary = false; |
33 | 246 | } <gix_packetline::blocking_io::write::Writer<alloc::vec::Vec<u8>>>::enable_text_mode Line | Count | Source | 31 | 246 | pub fn enable_text_mode(&mut self) { | 32 | 246 | self.binary = false; | 33 | 246 | } |
Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::enable_text_mode Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<alloc::boxed::Box<dyn core::io::write::Write>>>::enable_text_mode Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::enable_text_mode Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::enable_text_mode |
34 | | /// Return the inner writer, consuming self. |
35 | 737 | pub fn into_inner(self) -> T { |
36 | 737 | self.inner |
37 | 737 | } <gix_packetline::blocking_io::write::Writer<alloc::vec::Vec<u8>>>::into_inner Line | Count | Source | 35 | 737 | pub fn into_inner(self) -> T { | 36 | 737 | self.inner | 37 | 737 | } |
Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::into_inner Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::into_inner Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::into_inner |
38 | | /// Return a mutable reference to the inner writer, useful if packet lines should be serialized directly. |
39 | 0 | pub fn inner_mut(&mut self) -> &mut T { |
40 | 0 | &mut self.inner |
41 | 0 | } Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::inner_mut Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<alloc::boxed::Box<dyn core::io::write::Write>>>::inner_mut Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::inner_mut Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<std::process::ChildStdin>>::inner_mut Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<std::io::stdio::StdoutLock>>::inner_mut Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_>>::inner_mut |
42 | | } |
43 | | |
44 | | impl<T: io::Write> io::Write for Writer<T> { |
45 | 737 | fn write(&mut self, mut buf: &[u8]) -> io::Result<usize> { |
46 | 737 | if buf.is_empty() { |
47 | 0 | return Err(io::Error::other( |
48 | 0 | "empty packet lines are not permitted as '0004' is invalid", |
49 | 0 | )); |
50 | 737 | } |
51 | | |
52 | 737 | let mut written = 0; |
53 | 1.47k | while !buf.is_empty() { |
54 | 737 | let (data, rest) = buf.split_at(buf.len().min(MAX_DATA_LEN)); |
55 | 737 | written += if self.binary { |
56 | 491 | encode::data_to_write(data, &mut self.inner) |
57 | | } else { |
58 | 246 | encode::text_to_write(data, &mut self.inner) |
59 | 0 | }?; |
60 | | // subtract header (and trailing NL) because write-all can't handle writing more than it passes in |
61 | 737 | written -= U16_HEX_BYTES + usize::from(!self.binary); |
62 | 737 | buf = rest; |
63 | | } |
64 | 737 | Ok(written) |
65 | 737 | } <gix_packetline::blocking_io::write::Writer<alloc::vec::Vec<u8>> as core::io::write::Write>::write Line | Count | Source | 45 | 737 | fn write(&mut self, mut buf: &[u8]) -> io::Result<usize> { | 46 | 737 | if buf.is_empty() { | 47 | 0 | return Err(io::Error::other( | 48 | 0 | "empty packet lines are not permitted as '0004' is invalid", | 49 | 0 | )); | 50 | 737 | } | 51 | | | 52 | 737 | let mut written = 0; | 53 | 1.47k | while !buf.is_empty() { | 54 | 737 | let (data, rest) = buf.split_at(buf.len().min(MAX_DATA_LEN)); | 55 | 737 | written += if self.binary { | 56 | 491 | encode::data_to_write(data, &mut self.inner) | 57 | | } else { | 58 | 246 | encode::text_to_write(data, &mut self.inner) | 59 | 0 | }?; | 60 | | // subtract header (and trailing NL) because write-all can't handle writing more than it passes in | 61 | 737 | written -= U16_HEX_BYTES + usize::from(!self.binary); | 62 | 737 | buf = rest; | 63 | | } | 64 | 737 | Ok(written) | 65 | 737 | } |
Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_> as core::io::write::Write>::write Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<alloc::boxed::Box<dyn core::io::write::Write>> as core::io::write::Write>::write Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<&mut std::process::ChildStdin> as core::io::write::Write>::write Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_> as core::io::write::Write>::write Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<std::process::ChildStdin> as core::io::write::Write>::write Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<std::io::stdio::StdoutLock> as core::io::write::Write>::write Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_> as core::io::write::Write>::write |
66 | | |
67 | 0 | fn flush(&mut self) -> io::Result<()> { |
68 | 0 | self.inner.flush() |
69 | 0 | } Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_> as core::io::write::Write>::flush Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<alloc::boxed::Box<dyn core::io::write::Write>> as core::io::write::Write>::flush Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<&mut std::process::ChildStdin> as core::io::write::Write>::flush Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_> as core::io::write::Write>::flush Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<std::process::ChildStdin> as core::io::write::Write>::flush Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<std::io::stdio::StdoutLock> as core::io::write::Write>::flush Unexecuted instantiation: <gix_packetline::blocking_io::write::Writer<_> as core::io::write::Write>::flush |
70 | | } |