gpui-component/crates/story/examples/fixtures/test.rs
Jason Lee 3fa996c331
input: Fix line column by use chars offset. (#1239)
- Add autocorrect to markdown example for test `markers`.

<img width="1104" height="590" alt="image"
src="https://github.com/user-attachments/assets/03103088-e828-4039-82b0-c816d5f36141"
/>
2025-09-10 20:56:15 +08:00

1002 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
/// This is CJK 中文🎊 for test line, column.
///
/// # 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(())
}