Chapter 5 Hints

Working with files

There are some nice functions in the standard library for reading and writing files. std::fs::read and std::fs::write in particular.

If you're writing a function that reads from a file, there's a nice way to accept the file's path as a parameter using the AsRef trait. Your function signature will look something like fn from_file<P: AsRef<Path>>(path: P).

Working with strings and bytes

See the Chapter 2 Hints

Stubs


#![allow(unused_variables)]
fn main() {
use std::convert::TryFrom;
use std::fs;
use std::str::FromStr;

use crate::{Error, Result};
use crate::args::{DecodeArgs, EncodeArgs, PrintArgs, RemoveArgs};
use crate::png::{Chunk, ChunkType, Png};

/// Encodes a message into a PNG file and saves the result
pub fn encode(args: EncodeArgs) -> Result<()> {
    todo!()
}

/// Searches for a message hidden in a PNG file and prints the message if one is found
pub fn decode(args: DecodeArgs) -> Result<()> {
    todo!()
}

/// Removes a chunk from a PNG file and saves the result
pub fn remove(args: RemoveArgs) -> Result<()> {
    todo!()
}

/// Prints all of the chunks in a PNG file
pub fn print_chunks(args: PrintArgs) -> Result<()> {
    todo!()
}
}