Skip to content

Commit

Permalink
Add ParagraphInspectWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Jan 29, 2024
1 parent a246678 commit 125ea07
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
4 changes: 2 additions & 2 deletions libherokubuildpack/src/buildpack_output/inline_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//!
//! inline_output::step("Clearing the cache")
//! ```
use crate::buildpack_output::{state, BuildpackOutput, Stream};
use crate::buildpack_output::{state, BuildpackOutput, ParagraphInspectWrite, Stream};
use std::io::Stdout;
use std::time::Instant;

Expand Down Expand Up @@ -76,7 +76,7 @@ pub fn important(s: impl AsRef<str>) {

fn build_buildpack_output() -> BuildpackOutput<state::Section, Stdout> {
BuildpackOutput::<state::Section, Stdout> {
io: std::io::stdout(),
io: ParagraphInspectWrite::new(std::io::stdout()),
// Be careful not to do anything that might access this state
// as it's ephemeral data (i.e. not passed in from the start of the build)
started: Some(Instant::now()),
Expand Down
11 changes: 6 additions & 5 deletions libherokubuildpack/src/buildpack_output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! output.finish();
//! ```
//!
use crate::buildpack_output::util::ParagraphInspectWrite;
use std::fmt::Debug;
use std::io::Write;
use std::sync::{Arc, Mutex};
Expand All @@ -28,7 +29,7 @@ mod util;
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct BuildpackOutput<T, W: Debug> {
pub(crate) io: W,
pub(crate) io: ParagraphInspectWrite<W>,
pub(crate) started: Option<Instant>,
pub(crate) state: T,
}
Expand Down Expand Up @@ -138,7 +139,7 @@ where
{
pub fn new(io: W) -> Self {
Self {
io,
io: ParagraphInspectWrite::new(io),
state: state::NotStarted,
started: None,
}
Expand Down Expand Up @@ -186,7 +187,7 @@ where
writeln_now(&mut self.io, style::section("Done"));
}

self.io
self.io.inner
}
}

Expand Down Expand Up @@ -265,9 +266,9 @@ where
/// Mostly used for outputting a running command.
#[derive(Debug)]
#[doc(hidden)]
pub struct Stream<W> {
pub struct Stream<W: Debug> {
buildpack_output_started: Option<Instant>,
arc_io: Arc<Mutex<W>>,
arc_io: Arc<Mutex<ParagraphInspectWrite<W>>>,
started: Instant,
}

Expand Down
58 changes: 58 additions & 0 deletions libherokubuildpack/src/buildpack_output/util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::fmt::Debug;
use std::io::Write;

/// Iterator yielding every line in a string. The line includes newline character(s).
///
/// <https://stackoverflow.com/a/40457615>
Expand Down Expand Up @@ -35,6 +38,41 @@ impl<'a> Iterator for LinesWithEndings<'a> {
}
}

#[derive(Debug)]
pub(crate) struct ParagraphInspectWrite<W: Debug> {
pub(crate) inner: W,
pub(crate) was_paragraph: bool,
}

impl<W> ParagraphInspectWrite<W>
where
W: Debug,
{
pub(crate) fn new(io: W) -> Self {
Self {
inner: io,
was_paragraph: false,
}
}
}

impl<W: Write + Debug> Write for ParagraphInspectWrite<W> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
// Only modify `was_paragraph` if we write anything
if !buf.is_empty() {
// TODO: This will not work with Windows line endings
self.was_paragraph =
buf.len() >= 2 && buf[buf.len() - 2] == b'\n' && buf[buf.len() - 1] == b'\n';
}

self.inner.write(buf)
}

fn flush(&mut self) -> std::io::Result<()> {
self.inner.flush()
}
}

#[cfg(test)]
pub(crate) mod test_helpers {
use super::*;
Expand Down Expand Up @@ -84,4 +122,24 @@ mod test {

assert_eq!("zfoo\nzbar\n", actual);
}

#[test]
#[allow(clippy::write_with_newline)]
fn test_paragraph_inspect_write() {
use std::io::Write;

let buffer: Vec<u8> = vec![];
let mut inspect_write = ParagraphInspectWrite::new(buffer);

assert!(!inspect_write.was_paragraph);

write!(&mut inspect_write, "Hello World!\n").unwrap();
assert!(!inspect_write.was_paragraph);

write!(&mut inspect_write, "Hello World!\n\n").unwrap();
assert!(inspect_write.was_paragraph);

write!(&mut inspect_write, "End.\n").unwrap();
assert!(!inspect_write.was_paragraph);
}
}

0 comments on commit 125ea07

Please sign in to comment.