Gemention/src/models/mentions.rs

57 lines
1.6 KiB
Rust
Raw Normal View History

use async_trait::async_trait;
use fluffer::GemBytes;
use std::fmt::Display;
use crate::error::GementionError;
pub(crate) enum MentionType {
Reply,
Like,
}
pub(crate) struct Mention {
mention_type: MentionType,
user: String,
content: Option<String>,
}
// This is for converting an incoming Titan request into a mention. It
// is very flexible: If the incoming value matches a specific format,
// it'll accurately convert the stringi nto a proper mention.
// Otherwise, it just assumes comment. An error is returned only if
// input is not utf8.
impl TryFrom<Vec<u8>> for Mention {
type Error = GementionError;
fn try_from(content: Vec<u8>) -> Result<Self, Self::Error> {
let content = std::str::from_utf8(&content)?;
Ok(Mention {
mention_type: MentionType::Reply,
user: "".to_string(),
content: None,
})
}
}
impl Display for MentionType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Reply => write!(f, "Reply"),
Self::Like => write!(f, "Like"),
}
}
}
#[async_trait]
impl GemBytes for Mention {
async fn gem_bytes(self) -> Vec<u8> {
// TODO change output based on mention type:
// Reply/comment = current format.
// Like = "<user> liked this"
let headline = format!("## {} from {}", self.mention_type, self.user);
let content = self.content.unwrap_or(String::from("[no content]"));
format!("20 text/gemini\r\n{}\n\n{}", headline, content).into_bytes()
}
}