Crate reword [] [src]

A fast and safe translation generator.

The reword! macro generates types that allows for fast lookup of &'static str based on the current selected language. As seen in the example below, the enum generated has a reword method that translates the given message based on the current selected language.

Examples

#[macro_use]
extern crate reword;

reword! {
    enum Lang {
        Hi {
            EN_UK | EN_US = "Hi";
            NO = "Hei";
        }
        HowAreYou {
            EN_UK = "How are you?";
            EN_US = "How you doing?";
            NO = "Hvordan går det?";
        }
    }
}

fn main() {
    let mut lang = Lang::NO;
    assert_eq!(lang.reword::<Hi>(), "Hei");

    lang = Lang::EN_UK;
    assert_eq!(lang.reword::<HowAreYou>(), "How are you?");

    lang = Lang::EN_US;
    assert_eq!(lang.reword::<HowAreYou>(), "How you doing?");
}

Visibility

The generated types are not exported out of its module by default. Use pub before the enum to export it.

#[macro_use]
extern crate reword;

mod example {
    reword! {
        pub enum Lang {
            Hi {
                EN_UK | EN_US = "Hi";
                NO = "Hei";
            }
            HowAreYou {
                EN_UK = "How are you?";
                EN_US = "How you doing?";
                NO = "Hvordan går det?";
            }
        }
    }
}

fn main() {
    let mut lang = example::Lang::NO;
    assert_eq!(lang.reword::<example::Hi>(), "Hei");

    lang = example::Lang::EN_UK;
    assert_eq!(lang.reword::<example::HowAreYou>(), "How are you?");

    lang = example::Lang::EN_US;
    assert_eq!(lang.reword::<example::HowAreYou>(), "How you doing?");
}

Attributes

Attributes can be attached to both the enum and the structs generated.

The Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, and Hash traits are automatically derived for the types using the derive attribute.

#[macro_use]
extern crate reword;

reword! {
    #[repr(C)]
    enum Lang {
        #[derive(Default)]
        Hi {
            EN_UK | EN_US = "Hi";
            NO = "Hei";
        }
        HowAreYou {
            EN_UK = "How are you?";
            EN_US = "How you doing?";
            NO = "Hvordan går det?";
        }
    }
}

fn main() {
    let mut lang = Lang::NO;
    assert_eq!(lang.reword::<Hi>(), "Hei");

    lang = Lang::EN_UK;
    assert_eq!(lang.reword::<HowAreYou>(), "How are you?");

    lang = Lang::EN_US;
    assert_eq!(lang.reword::<HowAreYou>(), "How you doing?");
}

Generated Code

Usage:

reword! {
    enum Lang {
        Hi {
            EN_UK | EN_US = "Hi";
            NO = "Hei";
        }
        HowAreYou {
            EN_UK = "How are you?";
            EN_US = "How you doing?";
            NO = "Hvordan går det?";
        }
    }
}

Generated:

#[allow(bad_style)]
trait Word {
    const EN_UK: &'static str;
    const EN_US: &'static str;
    const NO: &'static str;
}

#[allow(bad_style)]
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
enum Lang {
    EN_UK,
    EN_US,
    NO,
}

impl Lang {
    #[inline]
    fn reword<W: Word>(self) -> &'static str {
        match self {
            Lang::EN_UK => W::EN_UK,
            Lang::EN_US => W::EN_US,
            Lang::NO => W::NO,
        }
    }
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
struct Hi;

impl Word for Hi {
    const EN_UK: &'static str = "Hi";
    const EN_US: &'static str = "Hi";
    const NO: &'static str = "Hei";
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)]
struct HowAreYou;

impl Word for HowAreYou {
    const EN_UK: &'static str = "How are you?";
    const EN_US: &'static str = "How you doing?";
    const NO: &'static str = "Hvordan går det?";
}

Macros

reword

The macro used to generate the language structures.