DEV Community

Discussion on: Implementing Base64 from scratch in Rust

Collapse
 
minigamedev profile image
Mossa

You're missing encode and decode. You have a description of them, maybe it is enough to write it myself.

Collapse
 
minigamedev profile image
Mossa

Oh, I'm a bit wrong here: The included encode and decode calls themselves, which is not entirely correct. The version that works is here:

fn encode(input: &String) -> String {
    encoder::encode_using_alphabet(&Classic, input.as_bytes())
}

fn decode(input: &String) -> Result<String, CLIError> {
    let decoded =
        decoder::decode_using_alphabet(Classic, input).map_err(|_| CLIError::DecodingError)?;

    let decoded_as_string = std::str::from_utf8(&decoded).map_err(|_| CLIError::DecodingError)?;

    Ok(decoded_as_string.to_owned())
}
Enter fullscreen mode Exit fullscreen mode

I love how this example is self-contained enough that I can actually guess the intent.

Collapse
 
tiemen profile image
Tiemen • Edited

The implementation of these functions was described in the chapter "Piecing it together" (third code block). It's pretty much what you wrote here, instead I used the convenience function that assumes you want to encode and decode against the classic alphabet :)

But maybe I'm misinterpreting your comment. In any case, thank you for feedback so far!

Thread Thread
 
minigamedev profile image
Mossa

Thanks for the reply. I went through it the first time, and could interpret it as a first time reader. This is no longer the case, so I don't know why I wrote that. Sorry.

But I think it is okay as is now.

Do you want me to delete these corrections now that they have been remedied?

Thread Thread
 
tiemen profile image
Tiemen

No worries! I appreciate the feedback regardless ☺️ Don't worry about the comments, I think that's part of the post and perhaps it encourages folks to point out mistakes I might have made. Thanks👌