aboutsummaryrefslogtreecommitdiff
path: root/planetwars-cli/src/workspace/mod.rs
blob: 64777c21a9e47d61169e803b59e37e396f0e99dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

use self::bot::WorkspaceBot;

const WORKSPACE_CONFIG_FILENAME: &str = "pw_workspace.toml";

pub mod bot;

pub struct Workspace {
    root_path: PathBuf,
    config: WorkspaceConfig,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WorkspaceConfig {
    paths: WorkspacePaths,
    bots: HashMap<String, BotEntry>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct WorkspacePaths {
    maps_dir: PathBuf,
    matches_dir: PathBuf,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct BotEntry {
    path: PathBuf,
}

impl Workspace {
    pub fn open(root_path: &Path) -> io::Result<Workspace> {
        let config_path = root_path.join(WORKSPACE_CONFIG_FILENAME);
        let config_str = fs::read_to_string(config_path)?;
        let workspace_config: WorkspaceConfig = toml::from_str(&config_str)?;

        Ok(Workspace {
            root_path: root_path.to_path_buf(),
            config: workspace_config,
        })
    }

    pub fn open_current_dir() -> io::Result<Workspace> {
        Workspace::open(&env::current_dir()?)
    }

    pub fn maps_dir(&self) -> PathBuf {
        self.root_path.join(&self.config.paths.maps_dir)
    }

    pub fn map_path(&self, map_name: &str) -> PathBuf {
        self.maps_dir().join(format!("{}.json", map_name))
    }

    pub fn matches_dir(&self) -> PathBuf {
        self.root_path.join(&self.config.paths.matches_dir)
    }

    pub fn match_path(&self, match_name: &str) -> PathBuf {
        self.matches_dir().join(format!("{}.log", match_name))
    }

    pub fn get_bot(&self, bot_name: &str) -> io::Result<WorkspaceBot> {
        let bot_entry = self.config.bots.get(bot_name).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::NotFound,
                format!("no such bot: {}", bot_name),
            )
        })?;
        WorkspaceBot::open(&self.root_path.join(&bot_entry.path))
    }
}