base64 encode/decode functions added for sake of usage simplicity

URL_SAFE encoding is used
Both functions seem to pass the tests as well
This commit is contained in:
shockrah 2020-05-10 13:02:59 -07:00
parent 11669840b2
commit bd45508584

30
server/src/utils.rs Normal file
View File

@ -0,0 +1,30 @@
// Generic functions used by pretty much everyone
use base64::{encode_config, decode_config, URL_SAFE};
use std::str;
pub fn encode_param(raw: &str) -> String {
encode_config(raw, URL_SAFE)
}
pub fn decode_param(enc: &str) -> Result<String, i32> {
let bytes = decode_config(enc, URL_SAFE).unwrap();
let res = str::from_utf8(&bytes);
match res {
Ok(data) => Ok(data.into()),
Err(_e) => Err(1)
}
}
#[cfg(test)]
mod encoder_decoder_tests {
use super::*;
#[test]
fn encode_decode() {
let raw = "some random bs";
let enc = encode_param(raw);
let dec = decode_param(&enc);
assert_eq!(raw, dec.unwrap());
}
}