1007 lines
27 KiB
Rust
1007 lines
27 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,
|
|
}
|
|
|
|
// Document colors: #FF0033, #00AA33, #0033FF, #FFAA33
|
|
|
|
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
|
|
//
|
|
// Use `Command-click` on `Duration` will jump to its definition.
|
|
// Use `Control-click` on `String`, `HashMap` or `Result` will open its documentation page.
|
|
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(())
|
|
}
|