ares/decoders/
interface.rs

1use crate::checkers::CheckerTypes;
2
3use super::crack_results::CrackResult;
4
5/// The Interface defines what the struct for each decoder looks like
6//TODO: rename this file
7pub struct Decoder<Type> {
8    /// The English name of the decoder.
9    pub name: &'static str,
10    /// A description, you can take the first line from Wikipedia
11    /// Sometimes our decoders do not exist on Wikipedia so we write our own.
12    pub description: &'static str,
13    /// Wikipedia Link
14    pub link: &'static str,
15    /// The tags it has. See other decoders. Think of it as a "category"
16    /// This is used to filter decoders.
17    /// For example, if you want to filter decoders that are "base64"
18    /// you would use the tag "base64" or "base".
19    /// You can also add tags like "online" to filter decoders that are online.
20    pub tags: Vec<&'static str>,
21    /// We get popularity by eye-balling it or using the API's data
22    pub popularity: f32,
23    /// we don't use the Type, so we use PhantomData to mark it!
24    pub phantom: std::marker::PhantomData<Type>,
25}
26
27/// The default implementation for a decoder
28pub struct DefaultDecoder;
29impl Default for Decoder<DefaultDecoder> {
30    fn default() -> Decoder<DefaultDecoder> {
31        Decoder {
32            name: "Default decoder",
33            description: "N/A",
34            link: "N/A",
35            tags: vec!["N/A"],
36            popularity: 0.0,
37            phantom: std::marker::PhantomData,
38        }
39    }
40}
41
42/// All decoders will share the same Crack trait
43/// Which let's us put them into a vector and iterate over them,
44/// Running `.crack()` on each of them.
45/// Relevant docs: https://docs.rs/crack/0.3.0/crack/trait.Crack.html
46pub trait Crack {
47    /// This function generates a new crack trait
48    fn new() -> Self
49    where
50        Self: Sized;
51    /// Crack is the function that actually does the decoding
52    fn crack(&self, text: &str, checker: &CheckerTypes) -> CrackResult;
53    /// Get all tags for the current decoder
54    fn get_tags(&self) -> &Vec<&str>;
55    /// Get the nam of the current decoder
56    fn get_name(&self) -> &str;
57}
58
59/// Returns a boolean of True if the string is successfully changed
60/// So empty strings fail, but non-empty strings succeed
61/// and only if the string is different from the original text.
62pub fn check_string_success(decoded_text: &str, original_text: &str) -> bool {
63    if decoded_text.is_empty() {
64        return false;
65    } else if decoded_text != original_text {
66        return true;
67    }
68    false
69}