Skip to content

Commit

Permalink
feat(translator): Improve auto-suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonbrad committed Oct 24, 2023
1 parent 0a1b0d7 commit b279d0e
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 68 deletions.
20 changes: 12 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ rhai = ["dep:rhai"]

[dependencies]
rhai = { version = "1.16.2", optional = true }
indexmap = { version = "2.0.2", features = ["serde"] }
serde = { version = "1.0.188", features = ["derive"] }
toml = "0.8.2"
27 changes: 14 additions & 13 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
#![deny(missing_docs)]

use indexmap::IndexMap;
#[cfg(feature = "rhai")]
use rhai::{Engine, AST};
use serde::Deserialize;
use std::result::Result;
use std::{collections::HashMap, error, fs, path::Path};
use std::{error, fs, path::Path};
use toml::{self};

/// Hold information about a configuration.
#[derive(Deserialize, Debug, Clone)]
pub struct Config {
/// The core config.
pub core: Option<CoreConfig>,
data: Option<HashMap<String, Data>>,
data: Option<IndexMap<String, Data>>,
#[cfg(feature = "rhai")]
translators: Option<HashMap<String, Data>>,
translation: Option<HashMap<String, Data>>,
translators: Option<IndexMap<String, Data>>,
translation: Option<IndexMap<String, Data>>,
}

/// Core information about a configuration.
Expand Down Expand Up @@ -89,7 +90,7 @@ impl Config {
.unwrap_or(true);

// Data
let mut data = HashMap::new();
let mut data = IndexMap::new();

config.data.unwrap_or_default().iter().try_for_each(
|(key, value)| -> Result<(), Box<dyn error::Error>> {
Expand Down Expand Up @@ -117,7 +118,7 @@ impl Config {
// Translators
#[cfg(feature = "rhai")]
{
let mut translators = HashMap::new();
let mut translators = IndexMap::new();

config.translators.unwrap_or_default().iter().try_for_each(
|(key, value)| -> Result<(), Box<dyn error::Error>> {
Expand All @@ -140,7 +141,7 @@ impl Config {
}

// Translation
let mut translation = HashMap::new();
let mut translation = IndexMap::new();

config.translation.unwrap_or_default().iter().try_for_each(
|(key, value)| -> Result<(), Box<dyn error::Error>> {
Expand Down Expand Up @@ -174,8 +175,8 @@ impl Config {
}

/// Extract the data from the configuration.
pub fn extract_data(&self) -> HashMap<String, String> {
let empty = HashMap::default();
pub fn extract_data(&self) -> IndexMap<String, String> {
let empty = IndexMap::default();

self.data
.as_ref()
Expand All @@ -193,8 +194,8 @@ impl Config {

/// Extract the translators from the configuration.
#[cfg(feature = "rhai")]
pub fn extract_translators(&self) -> Result<HashMap<String, AST>, Box<dyn error::Error>> {
let empty = HashMap::default();
pub fn extract_translators(&self) -> Result<IndexMap<String, AST>, Box<dyn error::Error>> {
let empty = IndexMap::default();
let mut engine = Engine::new();

// allow nesting up to 50 layers of expressions/statements
Expand Down Expand Up @@ -226,8 +227,8 @@ impl Config {
}

/// Extract the translation from the configuration.
pub fn extract_translation(&self) -> HashMap<String, Vec<String>> {
let empty = HashMap::new();
pub fn extract_translation(&self) -> IndexMap<String, Vec<String>> {
let empty = IndexMap::new();

self.translation
.as_ref()
Expand Down
5 changes: 4 additions & 1 deletion engine/translator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ authors = ["Brady Fomegne <[email protected]>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["rhai"]
default = ["rhai", "strsim"]
rhai = ["dep:rhai"]
strsim = ["dep:strsim"]

[dependencies]
rhai = { version = "1.16.2", optional = true }
indexmap = { version = "2.0.2", features = ["serde"] }
strsim = { version = "0.10.0", optional = true }
Loading

0 comments on commit b279d0e

Please sign in to comment.