Skip to content

Commit

Permalink
move pretty print from Display to Debug
Browse files Browse the repository at this point in the history
  • Loading branch information
yuandrew committed Aug 27, 2024
1 parent 9a8ef41 commit 8ea9fb1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
60 changes: 59 additions & 1 deletion sdk-core-protos/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{env, path::PathBuf};
use std::{
env,
fs::File,
io::{BufRead, BufReader},
path::PathBuf,
};

static ALWAYS_SERDE: &str = "#[cfg_attr(not(feature = \"serde_serialize\"), \
derive(::serde::Serialize, ::serde::Deserialize))]";
Expand Down Expand Up @@ -131,5 +136,58 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
],
)?;

let file_path = out.join("temporal.api.common.v1.rs");
post_process_payload_file(file_path)?;

Ok(())
}

fn post_process_payload_file(file_path: PathBuf) -> Result<(), Box<dyn std::error::Error>> {
let file_contents: BufReader<File> = BufReader::new(File::open(file_path.clone())?);
let mut new_file_contents = String::new();

let mut found_payload = false;
let mut written = false;
for line in file_contents.lines() {
let line = line?;
if line.contains("pub struct Payload {") {
new_file_contents.push_str("#[prost(skip_debug)]\n");
found_payload = true;
}

// Write the current line to the new content
new_file_contents.push_str(&line);
new_file_contents.push('\n');

if found_payload && line == "}" && !written {
let debug_impl = r#"
impl std::fmt::Debug for Payload {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.data.len() > 64 {
let mut windows = self.data.as_slice().windows(32);
write!(
f,
"[{}..{}]",
BASE64_STANDARD.encode(windows.next().unwrap_or_default()),
BASE64_STANDARD.encode(windows.next_back().unwrap_or_default())
)
} else {
write!(f, "[{}]", BASE64_STANDARD.encode(&self.data))
}
}
}
"#;
new_file_contents.push_str(debug_impl);
new_file_contents.push('\n');

written = true;
}
}

assert!(found_payload);

// Replace the original file with the modified content
std::fs::write(&file_path, new_file_contents)?;

Ok(())
}
12 changes: 1 addition & 11 deletions sdk-core-protos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,17 +1750,7 @@ pub mod temporal {

impl Display for Payload {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.data.len() > 64 {
let mut windows = self.data.as_slice().windows(32);
write!(
f,
"[{}..{}]",
BASE64_STANDARD.encode(windows.next().unwrap_or_default()),
BASE64_STANDARD.encode(windows.next_back().unwrap_or_default())
)
} else {
write!(f, "[{}]", BASE64_STANDARD.encode(&self.data))
}
write!(f, "{:?}", self)
}
}

Expand Down

0 comments on commit 8ea9fb1

Please sign in to comment.