gpui-component/crates/story/examples/fixtures/test.rs
Jason Lee 2b796c94e9
highlighter: Improve performance for large file. (#911)
Still need to improve.

This version can work smooth when the file lines are less than 5000
lines.

> cargo run --release on MacBook Pro, Apple M3 CPU

- Editing at 110 FPS+
- Display at 120 FPS.

<img width="977" alt="image"
src="https://github.com/user-attachments/assets/37958bcb-20d8-486b-8c50-2728d16f971b"
/>
2025-06-03 23:20:33 +08:00

1001 lines
26 KiB
Rust

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use tokio::time;
// Version number of the HelloWorld struct
const VERSION: &str = "1.0.0";
/// HelloWorld struct provides greeting functionality with configuration options
///
/// # Features
/// - Async greetings with customizable names
/// - Configuration management via HashMap
/// - Report generation
/// - Error handling with custom error types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HelloWorld {
name: String,
#[serde(skip)]
options: HashMap<String, serde_json::Value>,
created_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, thiserror::Error)]
pub enum HelloError {
#[error("Invalid name: {0}")]
InvalidName(String),
#[error("Operation timeout")]
Timeout,
}
type Result<T> = std::result::Result<T, HelloError>;
impl HelloWorld {
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
options: HashMap::new(),
created_at: chrono::Utc::now(),
}
}
// Greets multiple people asynchronously with configurable delay
pub async fn greet<T: AsRef<str>>(&self, names: &[T]) -> Result<()> {
for name in names {
time::sleep(Duration::from_millis(100)).await;
println!("Hello, {}!", name.as_ref());
}
Ok(())
}
fn generate_report(&self) -> String {
format!(
"HelloWorld Report\n================\nName: {}\nCreated: {}\nOptions: {:?}",
self.name, self.created_at, self.options
)
}
}
trait Configurable {
fn configure(&mut self, options: HashMap<String, serde_json::Value>);
fn is_configured(&self) -> bool;
}
impl Configurable for HelloWorld {
fn configure(&mut self, options: HashMap<String, serde_json::Value>) {
self.options.extend(options);
}
fn is_configured(&self) -> bool {
!self.options.is_empty()
}
}
#[tokio::main]
async fn main() -> Result<()> {
let mut greeter = HelloWorld::new("Rust");
let mut config = HashMap::new();
config.insert("timeout".to_string(), serde_json::json!(5000));
config.insert("retries".to_string(), serde_json::json!(3));
greeter.configure(config);
match greeter.greet(&["Alice", "Bob"]).await {
Ok(_) => println!("Greetings sent successfully"),
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}