Skip to main content

jules_core/builder/
message.rs

1use crate::message::{Message, Role};
2
3/// A builder for creating a `Message`.
4#[derive(Debug, Clone)]
5pub struct MessageBuilder {
6    role: Role,
7    content: String,
8}
9
10impl Default for MessageBuilder {
11    fn default() -> Self {
12        Self {
13            role: Role::User,
14            content: String::new(),
15        }
16    }
17}
18
19impl MessageBuilder {
20    /// Creates a new `MessageBuilder`.
21    #[must_use]
22    pub fn new() -> Self {
23        Self::default()
24    }
25
26    /// Sets the role of the message.
27    #[must_use]
28    pub fn role(mut self, role: Role) -> Self {
29        self.role = role;
30        self
31    }
32
33    /// Sets the content of the message.
34    #[must_use]
35    pub fn content(mut self, content: impl Into<String>) -> Self {
36        self.content = content.into();
37        self
38    }
39
40    /// Builds the `Message`.
41    #[must_use]
42    pub fn build(self) -> Message {
43        Message::new(self.role, self.content)
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_message_builder_default() {
53        let builder = MessageBuilder::new();
54        let msg = builder.build();
55        assert_eq!(*msg.role(), Role::User);
56        assert!(msg.content().is_empty());
57    }
58
59    #[test]
60    fn test_message_builder_custom() {
61        let builder = MessageBuilder::new()
62            .role(Role::Assistant)
63            .content("Hello from assistant");
64        let msg = builder.build();
65
66        assert_eq!(*msg.role(), Role::Assistant);
67        assert_eq!(msg.content(), "Hello from assistant");
68    }
69}