-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
100 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,45 @@ | ||
use crate::error::{Error, Result}; | ||
use crate::{ | ||
config::{Config, DEFAULT_CONFIG}, | ||
error::{Error, Result}, | ||
}; | ||
use rust_embed::RustEmbed; | ||
use std::{fmt, io::Cursor}; | ||
|
||
/// File extension of the audio files. | ||
const FILE_EXTENSION: &str = "mp3"; | ||
|
||
/// A typewriter sound. | ||
#[derive(Debug)] | ||
#[allow(dead_code)] | ||
pub enum Sound { | ||
Ding, | ||
Keydown, | ||
Keystroke, | ||
Keyup, | ||
Newline, | ||
} | ||
|
||
impl fmt::Display for Sound { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", format!("{:?}", self).to_lowercase()) | ||
} | ||
} | ||
|
||
impl Sound { | ||
/// Returns the file name of the sound. | ||
fn as_file_name(&self) -> String { | ||
format!("{self}.{FILE_EXTENSION}") | ||
} | ||
} | ||
use std::io::Cursor; | ||
use std::str; | ||
|
||
/// Embedded sound assets. | ||
#[derive(RustEmbed)] | ||
#[folder = "sounds"] | ||
pub struct Sounds; | ||
pub struct EmbeddedSound; | ||
|
||
impl Sounds { | ||
impl EmbeddedSound { | ||
/// Returns the bytes of the sound. | ||
pub fn get_sound(sound: Sound) -> Result<Cursor<Vec<u8>>> { | ||
Self::get(&sound.as_file_name()) | ||
pub fn get_sound(name: &str) -> Result<Cursor<Vec<u8>>> { | ||
Self::get(name) | ||
.map(|v| Cursor::new(v.data.to_vec())) | ||
.ok_or_else(|| Error::AssetNotFound(sound.to_string())) | ||
.ok_or_else(|| Error::AssetNotFound(name.to_string())) | ||
} | ||
} | ||
|
||
/// Configuration file embedder/extractor. | ||
/// | ||
/// Embeds `config/`[`DEFAULT_CONFIG`] into the binary. | ||
#[derive(Debug, RustEmbed)] | ||
#[folder = "config/"] | ||
pub struct EmbeddedConfig; | ||
|
||
impl EmbeddedConfig { | ||
/// Extracts the embedded content. | ||
pub fn get_config() -> Result<String> { | ||
match Self::get(DEFAULT_CONFIG) { | ||
Some(v) => Ok(str::from_utf8(&v.data)?.to_string()), | ||
None => Err(Error::Embedded(String::from("embedded config not found"))), | ||
} | ||
} | ||
|
||
/// Parses the extracted content into [`Config`]. | ||
/// | ||
/// [`Config`]: Config | ||
pub fn parse() -> Result<Config> { | ||
Ok(toml::from_str(&Self::get_config()?)?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters