use std::collections::HashMap;
use std::io::{self, Read};
pub struct Config {
pub name: String,
pub values: HashMap<String, String>,
}
struct InternalState {
count: u32,
}
pub trait Processor {
fn process(&self, input: &str) -> Result<String, Box<dyn std::error::Error>>;
fn is_ready(&self) -> bool;
}
pub enum Status {
Active,
Inactive,
Error(String),
}
impl Config {
pub fn new(name: String) -> Self {
Self {
name,
values: HashMap::new(),
}
}
pub fn get(&self, key: &str) -> Option<&String> {
self.values.get(key)
}
fn internal_method(&self) -> bool {
true
}
}
impl Processor for Config {
fn process(&self, input: &str) -> Result<String, Box<dyn std::error::Error>> {
Ok(format!("{}: {}", self.name, input))
}
fn is_ready(&self) -> bool {
!self.name.is_empty()
}
}
pub(crate) fn crate_visible_func() -> u32 {
42
}
pub(super) fn super_visible_func() -> u32 {
43
}
pub async fn fetch_remote(url: &str) -> io::Result<String> {
Ok(format!("fetched: {}", url))
}
pub fn calculate(x: i32, y: i32) -> i32 {
if x > 0 {
if y > 0 {
x + y
} else {
x - y
}
} else {
match y {
0 => 0,
1..=10 => y * 2,
_ => y,
}
}
}
mod inner {
pub fn inner_func() -> &'static str {
"inner"
}
}
macro_rules! my_macro {
($x:expr) => {
$x + 1
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_new() {
let config = Config::new("test".into());
assert_eq!(config.name, "test");
}
#[test]
fn test_calculate() {
assert_eq!(calculate(1, 2), 3);
assert_eq!(calculate(-1, 5), 10);
}
}