sm-chat
A lightweight and efficient Rust crate for building simple chat applications and managing chat interactions. Provides essential functionalities for handling messages, users, and basic chatroom operations.
Installation
To use sm-chat in your Rust project, add the following to your Cargo.toml file under the dependencies section:
toml
sm-chat = "0.1.0" # Replace with the actual version
Usage Examples
Here are a few examples demonstrating how to use the sm-chat crate:
1. Creating a New Chatroom and Adding Users: rust use sm_chat::{Chatroom, User};
fn main() { let mut chatroom = Chatroom::new("General Chat");
let user1 = User::new("Alice".to_string());
let user2 = User::new("Bob".to_string());
chatroom.add_user(user1);
chatroom.add_user(user2);
println!("Chatroom name: {}", chatroom.name());
println!("Number of users: {}", chatroom.users().len());
}
2. Sending and Receiving Messages: rust use sm_chat::{Chatroom, User, Message};
fn main() { let mut chatroom = Chatroom::new("Project Updates"); let alice = User::new("Alice".to_string()); let bob = User::new("Bob".to_string());
chatroom.add_user(alice.clone());
chatroom.add_user(bob.clone());
let message1 = Message::new(alice.id(), "Hello Bob!".to_string());
chatroom.send_message(message1.clone());
let message2 = Message::new(bob.id(), "Hi Alice!".to_string());
chatroom.send_message(message2.clone());
let messages = chatroom.messages();
println!("Number of messages: {}", messages.len());
for message in messages {
println!("Message from user {}: {}", message.sender_id(), message.content());
}
}
3. Retrieving User Information: rust use sm_chat::{Chatroom, User};
fn main() { let mut chatroom = Chatroom::new("Support Channel"); let user1 = User::new("Charlie".to_string());
chatroom.add_user(user1.clone());
if let Some(user) = chatroom.get_user(user1.id()) {
println!("User ID: {}", user.id());
println!("User Name: {}", user.name());
} else {
println!("User not found.");
}
}
4. Removing a User from a Chatroom: rust use sm_chat::{Chatroom, User};
fn main() { let mut chatroom = Chatroom::new("Moderation Room"); let user1 = User::new("David".to_string());
chatroom.add_user(user1.clone());
println!("Number of users before removal: {}", chatroom.users().len());
chatroom.remove_user(user1.id());
println!("Number of users after removal: {}", chatroom.users().len());
}
5. Broadcasting a System Message: rust use sm_chat::{Chatroom, Message};
fn main() { let mut chatroom = Chatroom::new("Announcements");
let system_message = Message::new("SYSTEM".to_string(), "Server is going down for maintenance in 1 hour.".to_string());
chatroom.send_message(system_message.clone());
let messages = chatroom.messages();
println!("Number of messages: {}", messages.len());
for message in messages {
println!("Message from {}: {}", message.sender_id(), message.content());
}
}
Feature Summary
- Chatroom Management: Create, name, and manage chatrooms.
- User Management: Add and remove users from chatrooms. Retrieve user information.
- Message Handling: Send and receive messages within chatrooms.
- Basic Message Storage: Stores messages within the chatroom for retrieval.
- User Identification: Uses unique IDs to identify users.
License
MIT
This crate is part of the sm-chat ecosystem. For advanced features and enterprise-grade tools, visit: https://supermaker.ai/chat/