Initial commit
This commit is contained in:
3
src/utils/mod.rs
Normal file
3
src/utils/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod status;
|
||||
pub mod text;
|
||||
pub mod text_to_speech;
|
||||
11
src/utils/status.rs
Normal file
11
src/utils/status.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
pub fn create_error_message_text(text: &str) -> String {
|
||||
format!("⚠️ Error: {}", text)
|
||||
}
|
||||
|
||||
pub fn create_success_message_text(text: &str) -> String {
|
||||
format!("✅ {}", text)
|
||||
}
|
||||
|
||||
pub fn create_tooltip_message_text(text: &str) -> String {
|
||||
format!("💡 {}", text)
|
||||
}
|
||||
19
src/utils/text.rs
Normal file
19
src/utils/text.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
pub fn block_quote(text: &str) -> String {
|
||||
text.lines()
|
||||
.map(|line| format!("> {}", line))
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
pub fn block_unquote(text: &str) -> String {
|
||||
text.lines()
|
||||
.map(|line| {
|
||||
if let Some(stripped) = line.strip_prefix("> ") {
|
||||
stripped.to_string()
|
||||
} else {
|
||||
line.to_string()
|
||||
}
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
}
|
||||
69
src/utils/text_to_speech.rs
Normal file
69
src/utils/text_to_speech.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use crate::agent::AgentPurpose;
|
||||
|
||||
use super::text::{block_quote, block_unquote};
|
||||
|
||||
/// Creates a text message which is based on transcribed audio.
|
||||
/// This text message is prefixed with an emoji and blockquoted, to indicate that it is a transcription.
|
||||
/// To reverse the process, use `parse_transcribed_message_text()`.
|
||||
pub fn create_transcribed_message_text(text: &str) -> String {
|
||||
block_quote(&format!("{} {}", AgentPurpose::SpeechToText.emoji(), text))
|
||||
}
|
||||
|
||||
/// Parses a transcribed message text, reversing the process done by `create_transcribed_message_text()`.
|
||||
/// If the provided text string does not match the expected format, None is returned.
|
||||
pub fn parse_transcribed_message_text(text: &str) -> Option<String> {
|
||||
if !text.starts_with("> ") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let unquoted = block_unquote(text);
|
||||
|
||||
let emoji_prefix = format!("{} ", AgentPurpose::SpeechToText.emoji());
|
||||
|
||||
if let Some(original) = unquoted.strip_prefix(&emoji_prefix) {
|
||||
return Some(original.to_string());
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub mod test {
|
||||
#[test]
|
||||
fn test_transcribed_message_text_creation() {
|
||||
let text = "Hello there!\nHow are you?";
|
||||
let expected = format!(
|
||||
"> {} Hello there!\n> How are you?",
|
||||
crate::agent::AgentPurpose::SpeechToText.emoji()
|
||||
);
|
||||
assert_eq!(expected, super::create_transcribed_message_text(text));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transcribed_message_text_parsing() {
|
||||
// All good
|
||||
let text = format!(
|
||||
"> {} Hello there!\n> How are you?",
|
||||
crate::agent::AgentPurpose::SpeechToText.emoji()
|
||||
);
|
||||
let expected = "Hello there!\nHow are you?";
|
||||
assert_eq!(
|
||||
Some(expected.to_owned()),
|
||||
super::parse_transcribed_message_text(&text)
|
||||
);
|
||||
|
||||
// No blockquote
|
||||
let text = format!(
|
||||
"{} Hello there!\nHow are you?",
|
||||
crate::agent::AgentPurpose::SpeechToText.emoji()
|
||||
);
|
||||
assert_eq!(None, super::parse_transcribed_message_text(&text));
|
||||
|
||||
// No emoji
|
||||
let text = "> Hello there!\n> How are you?";
|
||||
assert_eq!(None, super::parse_transcribed_message_text(text));
|
||||
|
||||
// Different emoji
|
||||
let text = "> 🌸 Hello there!\n> How are you?";
|
||||
assert_eq!(None, super::parse_transcribed_message_text(text));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user