1#![warn(missing_docs)]
7
8pub mod analyzer_data;
10pub mod char_mapping;
12pub mod corpus_cleaner;
14pub mod data;
16pub mod fast_layout;
18pub mod generate;
20pub mod layout;
22pub mod trigram_patterns;
24pub mod utility;
26pub mod weights;
28
29use std::path::{Path, PathBuf};
30
31pub use rayon;
32pub use serde;
33
34use thiserror::Error;
35
36pub const REPLACEMENT_CHAR: char = char::REPLACEMENT_CHARACTER;
38pub const SPACE_CHAR: char = '␣';
40pub const SHIFT_CHAR: char = '⇑';
42pub const REPEAT_KEY: char = '↻';
44
45#[derive(Debug, Error)]
47pub enum OxeylyzerError {
48 #[error("Bigrams should contain 2 characters, bigram with length {0} encountered.")]
50 InvalidBigramLength(usize),
51 #[error("Trigrams should contain 3 characters, trigram with length {0} encountered.")]
53 InvalidTrigramLength(usize),
54 #[error("Failed to create a file chunker")]
56 ChunkerInitError,
57 #[error("Failed to create appropriate chunks")]
59 ChunkerChunkError,
60 #[error("Path must be either a directory or a file, '{}' is neither", .0.display())]
62 NotAFile(PathBuf),
63 #[error("Cannot load data for '{}' as it does not exist.", .0.display())]
65 PathDoesNotExist(PathBuf),
66 #[error("Specifying a name for the corpus is required")]
68 MissingDataName,
69 #[error("Failed to serialize data for language '{0}'")]
71 CouldNotSerializeData(String),
72 #[error("Corpus path '{}' is invalid as it does not end in a (.json) file.", .0.display())]
74 InvalidCorpusPath(PathBuf),
75
76 #[error("{0:#}")]
78 AnyhowError(#[from] anyhow::Error),
79
80 #[cfg(target_arch = "wasm32")]
82 #[error("{0}")]
83 GlooError(#[from] gloo_net::Error),
84}
85
86pub type Result<T> = std::result::Result<T, OxeylyzerError>;
88
89pub trait OxeylyzerResultExt<T> {
91 fn path_context<P: AsRef<Path>>(self, path: P) -> Result<T>;
93
94 fn str_context<S: ToString>(self, s: S) -> Result<T>;
96}
97
98impl<T, E> OxeylyzerResultExt<T> for std::result::Result<T, E>
99where
100 E: std::error::Error + Send + Sync + 'static,
101{
102 fn path_context<P: AsRef<Path>>(self, path: P) -> Result<T> {
103 use anyhow::Context;
104
105 self.context(path.as_ref().display().to_string())
106 .map_err(OxeylyzerError::AnyhowError)
107 }
108
109 fn str_context<S: ToString>(self, s: S) -> Result<T> {
110 use anyhow::Context;
111
112 self.context(s.to_string())
113 .map_err(OxeylyzerError::AnyhowError)
114 }
115}