New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the rest of the attachment fields to Attachment struct #7
Conversation
/// Optional text that appears above the attachment block | ||
pub author_name: Option<SlackText>, | ||
/// Optional link to the author | ||
pub author_link: Option<SlackText>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be Option<String>
because we don't need to do any escaping in the URL. We should send that as is (e.g. &
shouldn't be changed to &
in a URL).
Thanks @mcasper! One minor change here: you don't need to escape the links sent to slack, only the text. |
Oh good point, thanks @compressed! Do we want those fields to be |
Well I think you mean to be consistent with However, I was also thinking that we should really avoid If you're up for it, I'd like to get to that point. If not, you can just keep it as |
Oh one more thing, please add:
to the end of your commit message. |
I'm absolutely up for it! The current method of building payloads/attachments through template structs is at war with the thought to use an let attachment = Attachment::new() // initializes with all default values
.text("Attachment text")
.color("#6800e8")
// etc
let payload = Payload::new()
.attachments(vec![attachment])
.channel("#channel")
// etc This would allow all of the url builder functions to accept |
Awesome! Yup, I was thinking that the template is not really a good choice anymore and going to builder pattern is more flexible. I'd like to move forward with that approach. However, there is a challenge with using the builder pattern and handling the error ergonomics. Currently, making an attachment (all at once) will return a I think your example would look more like this (using the question mark notation, which should arrive at some point...): let attachment: Result<Attachment, SlackError> = Attachment::default()
.text("Attachment text")?
.color("#6800e8")?
.author_link("http://example.com")?
.author_icon("http://example.com/icon"); which isn't too bad, but with So onto another approach... We can use the builder strategy as mentioned here: colin-kiegel/rust-derive-builder#25 (comment). We'd then have something like this: let attachment: Result<Attachment, SlackError> = AttachmentBuilder::new()
.text("Attachment text")
.color("#6800e8")
.author_link("http://example.com")
.author_icon("http://example.com/icon")
.build(); which seems like it will work and seems better for usability. The underlying implementation would something like this: struct AttachmentBuilder {
inner: Result<Attachment, Error>,
}
impl AttachmentBuilder {
fn new() -> AttachmentBuilder {
AttachmentBuilder { inner: Ok(Default::default()) }
}
fn fallback<S: Into<String>>(mut self, text: S) -> AttachmentBuilder {
match self.inner {
Ok(mut inner) => {
inner.fallback = SlackText::new(text); // I will refactor this to accept `Into<String>`
AttachmentBuilder { inner: Ok(inner) }
}
_ => self,
}
}
....
fn build(self) -> Result<Attachment, Error> {
self.inner
}
} Let me know your thoughts. I could see a macro being useful here to help write the methods, since most of them will look quite similar. Since this will be a breaking change, I'm going to take this opportunity to do some other refactoring of the library, especially cleaning up the |
☔ The latest upstream changes (presumably #8) made this pull request unmergeable. Please resolve the merge conflicts. |
@mcasper I've completed the refactoring I wanted to do. You can check this branch: https://github.com/compressed/rust-slack/tree/builder. The code should be much cleaner now. I've added starting builder functions for Please make your changes on top of my branch and then I can review and pull them in. One other point: I've added the Let me know if you have any questions. Thanks! |
@mcasper I went ahead and added these fields (along with |
Support the rest of the attachment fields detailed here: https://api.slack.com/docs/message-attachments#attachment_structure