From 88b14fe15216b2125ebc6f5f2498684d26e76024 Mon Sep 17 00:00:00 2001 From: paulobressan Date: Tue, 6 Aug 2024 15:03:53 -0300 Subject: [PATCH] chore: updated to load words static --- data/adjectives.json | 52 ------------------------------------- data/nouns.json | 52 ------------------------------------- docker/dockerfile.daemon | 1 + docker/dockerfile.rpc | 3 --- src/domain/utils.rs | 45 -------------------------------- src/domain/utils/adjectives | 50 +++++++++++++++++++++++++++++++++++ src/domain/utils/mod.rs | 25 ++++++++++++++++++ src/domain/utils/nouns | 50 +++++++++++++++++++++++++++++++++++ 8 files changed, 126 insertions(+), 152 deletions(-) delete mode 100644 data/adjectives.json delete mode 100644 data/nouns.json delete mode 100644 src/domain/utils.rs create mode 100644 src/domain/utils/adjectives create mode 100644 src/domain/utils/mod.rs create mode 100644 src/domain/utils/nouns diff --git a/data/adjectives.json b/data/adjectives.json deleted file mode 100644 index d6712c0..0000000 --- a/data/adjectives.json +++ /dev/null @@ -1,52 +0,0 @@ -[ - "tasteful", - "existing", - "charming", - "ethereal", - "faithful", - "imported", - "parallel", - "fearless", - "accurate", - "material", - "juvenile", - "nebulous", - "powerful", - "adorable", - "truthful", - "freezing", - "peaceful", - "precious", - "absorbed", - "aromatic", - "detailed", - "assorted", - "exciting", - "separate", - "actually", - "skillful", - "hallowed", - "cultured", - "succinct", - "possible", - "medieval", - "romantic", - "magnetic", - "fabulous", - "metallic", - "singular", - "harmonic", - "bohemian", - "synaptic", - "greenish", - "vitreous", - "wearable", - "cetacean", - "oracular", - "viridian", - "aquarian", - "sidereal", - "diatonic", - "granular", - "platonic" -] diff --git a/data/nouns.json b/data/nouns.json deleted file mode 100644 index 099350a..0000000 --- a/data/nouns.json +++ /dev/null @@ -1,52 +0,0 @@ -[ - "sympathy", - "election", - "politics", - "patience", - "analysis", - "teaching", - "contract", - "guidance", - "internet", - "homework", - "audience", - "birthday", - "quantity", - "customer", - "employee", - "relation", - "magazine", - "emphasis", - "elevator", - "ambition", - "argument", - "presence", - "priority", - "reaction", - "baseball", - "language", - "midnight", - "delivery", - "attitude", - "stranger", - "decision", - "republic", - "property", - "category", - "strategy", - "director", - "addition", - "kindness", - "calmness", - "gladness", - "laughter", - "appetite", - "elegance", - "aircraft", - "infusion", - "aerobics", - "dinosaur", - "catapult", - "zeppelin", - "broccoli" -] diff --git a/docker/dockerfile.daemon b/docker/dockerfile.daemon index 5efdcdd..3f8bd87 100644 --- a/docker/dockerfile.daemon +++ b/docker/dockerfile.daemon @@ -16,5 +16,6 @@ RUN apt update RUN apt install -y libssl-dev libsasl2-dev COPY --from=build /app/target/release/ ./ + CMD ["./daemon"] LABEL service=daemon diff --git a/docker/dockerfile.rpc b/docker/dockerfile.rpc index 719a032..10eaf0f 100644 --- a/docker/dockerfile.rpc +++ b/docker/dockerfile.rpc @@ -12,13 +12,10 @@ RUN cargo build --release --bin=rpc FROM rust:1.79-slim-buster -WORKDIR /fabric - RUN apt update RUN apt install -y libssl-dev libsasl2-dev COPY --from=build /app/target/release/rpc . -COPY ./data ./data CMD ["./rpc"] LABEL service=rpc diff --git a/src/domain/utils.rs b/src/domain/utils.rs deleted file mode 100644 index 46bb70b..0000000 --- a/src/domain/utils.rs +++ /dev/null @@ -1,45 +0,0 @@ -use lazy_static::lazy_static; -use rand::distributions::Alphanumeric; -use rand::Rng; - -use std::fs::File; -use std::io::BufReader; -use std::path::Path; - -lazy_static! { - static ref ADJECTIVES: Vec = load_adjectives(); - static ref NOUNS: Vec = load_nouns(); -} - -fn load_adjectives() -> Vec { - let path = Path::new("data/adjectives.json"); - let file = File::open(path).expect("invalid adjectives file path"); - let reader = BufReader::new(file); - serde_json::from_reader(reader).expect("adjectives file must be a json array of string") -} - -fn load_nouns() -> Vec { - let path = Path::new("data/nouns.json"); - let file = File::open(path).expect("invalid nouns file path"); - let reader = BufReader::new(file); - serde_json::from_reader(reader).expect("nouns file must be a json array of string") -} - -fn get_random_word(list: &[String]) -> String { - let mut rng = rand::thread_rng(); - let idx = rng.gen_range(0..list.len()); - list[idx].clone() -} - -pub fn get_random_name() -> String { - let salt: String = rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(6) - .map(char::from) - .collect(); - - let adjective = get_random_word(&ADJECTIVES); - let noun = get_random_word(&NOUNS); - - format!("{adjective}-{noun}-{}", salt.to_lowercase()) -} diff --git a/src/domain/utils/adjectives b/src/domain/utils/adjectives new file mode 100644 index 0000000..d09074f --- /dev/null +++ b/src/domain/utils/adjectives @@ -0,0 +1,50 @@ +tasteful +existing +charming +ethereal +faithful +imported +parallel +fearless +accurate +material +juvenile +nebulous +powerful +adorable +truthful +freezing +peaceful +precious +absorbed +aromatic +detailed +assorted +exciting +separate +actually +skillful +hallowed +cultured +succinct +possible +medieval +romantic +magnetic +fabulous +metallic +singular +harmonic +bohemian +synaptic +greenish +vitreous +wearable +cetacean +oracular +viridian +aquarian +sidereal +diatonic +granular +platonic diff --git a/src/domain/utils/mod.rs b/src/domain/utils/mod.rs new file mode 100644 index 0000000..bafdbad --- /dev/null +++ b/src/domain/utils/mod.rs @@ -0,0 +1,25 @@ +use rand::distributions::Alphanumeric; +use rand::Rng; + +const ADJECTIVES: &str = include_str!("adjectives"); +const NOUNS: &str = include_str!("nouns"); + +fn get_random_word(list: Vec<&str>) -> String { + let mut rng = rand::thread_rng(); + let idx = rng.gen_range(0..list.len()); + list[idx].to_string() +} + +pub fn get_random_name() -> String { + let salt: String = rand::thread_rng() + .sample_iter(&Alphanumeric) + .filter(|c| c.is_ascii_lowercase() || c.is_ascii_digit()) + .take(6) + .map(char::from) + .collect(); + + let adjective = get_random_word(ADJECTIVES.lines().collect()); + let noun = get_random_word(NOUNS.lines().collect()); + + format!("{adjective}-{noun}-{salt}") +} diff --git a/src/domain/utils/nouns b/src/domain/utils/nouns new file mode 100644 index 0000000..09c7beb --- /dev/null +++ b/src/domain/utils/nouns @@ -0,0 +1,50 @@ +sympathy +election +politics +patience +analysis +teaching +contract +guidance +internet +homework +audience +birthday +quantity +customer +employee +relation +magazine +emphasis +elevator +ambition +argument +presence +priority +reaction +baseball +language +midnight +delivery +attitude +stranger +decision +republic +property +category +strategy +director +addition +kindness +calmness +gladness +laughter +appetite +elegance +aircraft +infusion +aerobics +dinosaur +catapult +zeppelin +broccoli