Skip to content

Commit

Permalink
🚧 Get rid of TryRender
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Oct 29, 2024
1 parent dfc9013 commit 0cabc8e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::atomic::{AtomicU64, Ordering};

use bon::Builder;
use maud::Render;

use crate::{
db::{
Expand All @@ -14,7 +15,7 @@ use crate::{
methods::{AllowedUpdate, GetMe, GetUpdates, Method, SendMessage},
notification::SendNotification,
objects::{Chat, ParseMode, ReplyParameters, Update, UpdatePayload},
render::{ListingCaption, TryRender},
render::ListingCaption,
start::{StartCommand, StartPayload},
},
};
Expand Down Expand Up @@ -128,7 +129,7 @@ impl Bot {
.search_query(query)
.commands(&[subscribe_command])
.build()
.try_render()?
.render()
.into_string();
SendNotification::builder()
.chat_id(chat_id.into())
Expand Down
41 changes: 14 additions & 27 deletions src/telegram/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,25 @@ use crate::{
Price,
Seller,
},
prelude::*,
telegram::start::StartCommand,
};

pub trait TryRender {
fn try_render(&self) -> Result<Markup>;
}

impl<R: Render> TryRender for R {
fn try_render(&self) -> Result<Markup> {
Ok(self.render())
}
}

#[derive(Builder)]
pub struct ListingCaption<'a> {
search_query: SearchQuery,
listing: &'a Listing,
commands: &'a [StartCommand<'a>],
}

impl<'a> TryRender for ListingCaption<'a> {
fn try_render(&self) -> Result<Markup> {
Ok(html! {
impl<'a> Render for ListingCaption<'a> {
fn render(&self) -> Markup {
html! {
strong { a href=(self.listing.https_url()) { (self.listing.title) } }
"\n"
em { (self.search_query.text) }
@for command in self.commands {
strong { " • " }
(command.try_render()?)
(command)
}
"\n\n"
(self.listing.price)
Expand All @@ -63,19 +52,18 @@ impl<'a> TryRender for ListingCaption<'a> {
strong { " • " }
(self.listing.location)
}
})
}
}
}

impl<'a> TryRender for StartCommand<'a> {
fn try_render(&self) -> Result<Markup> {
let mut url = Url::parse("https://t.me")?;
url.set_path(self.me);
url.set_query(Some(&format!(
"start={}",
impl<'a> Render for StartCommand<'a> {
fn render(&self) -> Markup {
let url = format!(
"https://t.me/{}?start={}",
self.me,
base64_url::encode(&self.payload.encode_to_vec())
)));
Ok(html! { a href=(url) { (self.text) } })
);
html! { a href=(url) { (self.text) } }
}
}

Expand Down Expand Up @@ -177,16 +165,15 @@ mod tests {
use crate::telegram::start::StartPayload;

#[test]
fn test_render_start_command_ok() -> Result {
fn test_render_start_command_ok() {
let command = StartCommand::builder()
.me("mrktpltsbot")
.payload(StartPayload::subscribe_to(42))
.text("Subscribe")
.build();
assert_eq!(
command.try_render()?.into_string(),
command.render().into_string(),
r#"<a href="https://t.me/mrktpltsbot?start=CgkJKgAAAAAAAAA">Subscribe</a>"#,
);
Ok(())
}
}
2 changes: 2 additions & 0 deletions src/telegram/start.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! `/start` command.

use bon::Builder;
use prost::Message;

Expand Down

0 comments on commit 0cabc8e

Please sign in to comment.