lang_interpreter/interpreter/
platform.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::collections::HashMap;
use std::ffi::OsString;
use std::fmt::Debug;
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader, Error, Read, Write};
use std::path::{Path, PathBuf};
use crate::interpreter::data::function::native::NativeError;

pub trait PlatformAPI: Debug {
    /**
     * @param langPath Path to the folder
     * @return Return all files inside the folder located at langPath
     */
    fn get_lang_files(&self, lang_path: &Path) -> Result<Vec<PathBuf>, Error>;

    /**
     * @param langFile Path to the file
     * @return Return the canonical path of the file located at langFile
     */
    fn get_lang_path(&self, lang_file: &Path) -> Result<PathBuf, Error>;

    /**
     * @param langFile Path to the file
     * @return Return the file name of the file located at langFile
     */
    fn get_lang_file_name(&self, lang_file: &Path) -> Option<OsString>;

    /**
     * @param langFile Path to the file
     * @return Return a reader for the file
     */
    fn get_lang_buffered_reader(&self, lang_file: &Path) -> Result<Box<dyn BufRead>, Error>;

    /**
     * @param langFile Path to the file
     * @return Return an input stream for the file
     */
    fn get_lang_reader(&self, lang_file: &Path) -> Result<Box<dyn Read>, Error>;

    /**
     * @param langFile Path to the file
     * @param translationMap The Map of all translations
     * @return Return true if successful else false (Return false if not implemented)
     */
    fn write_lang_file(&self, lang_file: &Path, translation_map: HashMap<String, String>) -> Result<(), Error>;

    /**
     * @param text The text prompt to be shown to the user
     * @return Return the value inputed by the user
     * @throws Exception Throw any exception if not implemented or if any other error occurred
     */
    fn show_input_dialog(&self, text: &str) -> Result<String, NativeError>;
}

#[derive(Debug)]
pub struct DefaultPlatformAPI;

impl DefaultPlatformAPI {
    pub fn new() -> Self {
        Self
    }
}

impl Default for DefaultPlatformAPI {
    fn default() -> Self {
        Self::new()
    }
}

impl PlatformAPI for DefaultPlatformAPI {
    fn get_lang_files(&self, lang_path: &Path) -> Result<Vec<PathBuf>, Error> {
        let files = fs::read_dir(lang_path)?;

        let mut lang_files = Vec::new();

        for file in files {
            let file = file?;
            let file_type = file.file_type()?;
            if !file_type.is_dir() {
                let file_name = file.file_name();
                if let Some(file_name) = file_name.to_str() {
                    if file_name.to_ascii_lowercase().ends_with(".lang") {
                        lang_files.push(file.path());
                    }
                }
            }
        }

        Ok(lang_files)
    }

    fn get_lang_path(&self, lang_file: &Path) -> Result<PathBuf, Error> {
        let path = lang_file;
        let mut path = path.parent().unwrap_or(Path::new(""));
        if path == Path::new("") {
            path = Path::new("./");
        }

        let canonical_path = path.canonicalize();
        if let Ok(canonical_path) = canonical_path {
            Ok(canonical_path)
        }else {
            std::path::absolute(path)
        }
    }

    fn get_lang_file_name(&self, lang_file: &Path) -> Option<OsString> {
        let path = lang_file;

        path.file_name().map(|str| str.to_os_string())
    }

    fn get_lang_buffered_reader(&self, lang_file: &Path) -> Result<Box<dyn BufRead>, Error> {
        let path = lang_file;

        let file = File::open(path)?;
        let reader = BufReader::new(file);

        Ok(Box::new(reader))
    }

    fn get_lang_reader(&self, lang_file: &Path) -> Result<Box<dyn Read>, Error> {
        let path = lang_file;

        let file = File::open(path)?;

        Ok(Box::new(file))
    }

    fn write_lang_file(&self, lang_file: &Path, translation_map: HashMap<String, String>) -> Result<(), Error> {
        let path = lang_file;

        let mut file = File::create(path)?;

        for (translation_key, translation_value) in translation_map {
            //For multiline
            let translation_value = translation_value.replace("\n", "\\n");

            writeln!(file, "{translation_key} = {translation_value}")?;
        }

        Ok(())
    }

    /**
     * This method is not implemented
     */
    fn show_input_dialog(&self, _text: &str) -> Result<String, NativeError> {
        Err(NativeError::new(
            "Not Implemented",
            None,
        ))
    }
}