I’m struck on Stage #KP1
The tester is failing to test valid output.
I’ve tried matching my output to the codecrafter’s test expected output but we are going in circles.
I’ve tried:
- having a new line after writing a name(expected this to be the right way)
- printing a new line only after writing all names
- printing a new line after writing a name but not after the last name.
Here are my logs:
And here’s a snippet of my code:
fn read_tree(mut object: Object<impl BufRead>, name_only: bool) {
let mut info_buf = Vec::new();
let mut hash_buf = [0; 20];
let mut to_make_codecrafter_happy = Vec::new();
loop {
// Read for each object line individually.
// A line has the following structure
// <mode> <type> <null char> <sha1 hash of length 20>
info_buf.clear();
let read_bytes = object
.buf_reader
.read_until(0, &mut info_buf)
.expect("Failed to read tree's entry");
if read_bytes == 0 {
// End of tree
break;
}
let (mode, name) = std::ffi::CStr::from_bytes_with_nul(&info_buf)
.expect("Failed to parse infobuf for tree's entry")
.to_str()
.expect("Failed to parse valid UTF8 from infobuf")
.split_once(' ')
.expect("Failed to parse valid mode and name");
object
.buf_reader
.read_exact(&mut hash_buf[..])
.expect("Failed to read sha1 hash from tree's entry");
if name_only {
// println!("{name}");
// To make codecrafters happy
to_make_codecrafter_happy.push(name.to_owned());
// stdout.write_all(name.as_bytes()).unwrap();
// writeln!(stdout, "").unwrap();
continue;
}
let hash = hex::encode(&hash_buf);
let object = read_object(hash.clone());
println!("{:0>6} {:?} {} {}", mode, object.object_type, hash, name);
return;
}
print!("{}", to_make_codecrafter_happy.join("\n"));
// for (index, name) in to_make_codecrafter_happy.clone().into_iter().enumerate() {
// ss.push_str(name.as_str());
// if index != to_make_codecrafter_happy.len() - 1 {
// ss.push_str("\n");
// }
// }
}