Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyYe committed Feb 29, 2024
1 parent 4ce5135 commit 5f8a5f6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# will have compiled files and executables
debug/
target/

.idea/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
Expand Down
20 changes: 0 additions & 20 deletions config.yml

This file was deleted.

12 changes: 6 additions & 6 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Rule {
name: String,
sequence: Vec<i32>,
timeout: i32,
command: String,
pub name: String,
pub sequence: Vec<i32>,
pub timeout: i32,
pub command: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
interface: String,
rules: Vec<Rule>,
pub interface: String,
pub rules: Vec<Rule>,
}
27 changes: 27 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
mod config;

use config::Config;
use std::fs::File;
use std::io::Read;

fn load_config() -> Result<Config, Box<dyn std::error::Error>> {
let mut file = File::open("config.yaml")?;
let mut content = String::new();

file.read_to_string(&mut content)?;
let config: Config = serde_yaml::from_str(&content)?;

Ok(config)
}

// test case for load_config
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_load_config() {
let config = load_config().unwrap();
assert_eq!(config.interface, "enp3s0");
assert_eq!(config.rules.len(), 2);
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod server;
mod config;

use server::Server;

fn main() {
Expand Down

0 comments on commit 5f8a5f6

Please sign in to comment.