Plan C - Getting the Correct Image From the Specified Kind

Return to Plan C

Plan C - Getting the Correct Image From the Specified Kind

In the "parse_opts()" function, we are using a "match" statement to determine which image, if any, was specified on the command-line, and that information is put into the "kind" variable. We could create a new function that takes that "kind" variable and selects the appropriate image constant to manipulate. The problem with that is that now we'd have two match statements, and if a future ASCII-artist wanted to add a new image, say, of a bicyclist, he would have to make appropriate modifications to two match statements instead of just one. That both increases the possibility of human error, and makes debugging somewhat more difficult, not to mention that it's more work for the ASCII-artist.

So it'd probably be better to use the already-existing match statement. Let's go that route. Rather than having the match return just the kind, we'll have it return the kind and the image string itself, combining them as a tuple (remember, a tuple has nothing to do with "two"; the fact that there are two items in this tuple is just a coincidence; also, remember, a tuple is simply a variable holding a collection of variables; a grocery sack containing grocery sacks, so to speak). And then the overall "parse_opts()" function will add this new variable "image' from the match statement's returned tuple to its own tuple which it returns. Since we don't yet have the other images prepared in their own constants, but each arm of the match statement needs to return the same sort[s] of variable[s] as what the other arms return, we'll just temporarily plug in the "D51" constant as the second tuple item on all the match arms, just while we're developing things. Later we'll use a "C51" constant for the C51 image, and a "JACK" constant for the Jumping Jack image, and etc.

Since we'll be referencing the D51 constant, which is in the "d51.rs" file, we'll need to specify the path to that constant when we make that reference in "parse_opts()". We can do that in one of two ways: when we make the reference, we can specify the path at that point, as in "D" | "D51" => (String::from("D51"), crate::d51::D51.to_string()), or we could put a "use" statement at the top of the "parsing.rs" file. Let's go that latter route:

parsing.rs
/* parsing.rs */

use crate::d51::D51;
  
// Use c[ommand] l[ine] a[rgument] p[arser] to get command-line arguments.
use clap::Parser;
...

And now we can make the necessary changes to the "parse_opts()" function. Notice that since we haven't yet prepared the other images, but we need the output of the match arms to be consistent, we're temporarily using "D51" for all the matches. We'll fix that as we go.

parsing.rs
......
pub fn parse_opts() -> (u64, bool, String, bool, String) {
...
    // Set "kind".
    let (kind: String, image_string) = match (&switches.kind.to_uppercase()).as_str() {
        "D" | "D51" => (String::from("D51"), D51.to_string()),
        "C" | "C51" => (String::from("C51"), D51.to_string()),
        "L" | "LITTLE" => (String::from("LITTLE"), D51.to_string()),
        "J" | "JACK" => (String::from("JACK"), D51.to_string()),
        "B" | "BOAT" => (String::from("BOAT"), D51.to_string()),
        "P" | "PLANE" => (String::from("PLANE"), D51.to_string()),
        _ => {
            // If none of them match, then ...
            println!("Invalid input; run program with '--help' option for more information. You entered '{}', so the default of 'd51' was used.", switches.kind);
            (String::from("D51"), D51.to_string())
        }
    };

    return (speed, switches.fly, kind, switches.oops, image_string);
} // end of parse_opts()

Now in our "main()" function, instead of displaying the "D51" constant, we can display the "image_string" variable, which has been set to the "D51" constant by the match statement in "parse_opts()", according to what was entered on the command-line.

main.rs
/* main.rs */

mod d51;
// mod parsing;

use d51::*;
// use parsing::*;

fn main() {
    // let (speed, fly, kind, oops, image_string) = parse_opts(); // Get the program options from the command-line, if any.
    // draw_d51(speed, fly, oops);
    if kind == "D51" {
        println!("{}", D51);
    } else {
        println!("You entered {}.", kind);
    }
    println!("{}", image_string);
} // end of main()

Verify that this code works, and then let's Add The C51, Little, and Jack Images.