Skip to main content

libkit/
kitin.rs

1use serde::{Deserialize, Serialize};
2use std::{
3    io::{Read, Write},
4    path::Path,
5};
6
7const KITIN_PROJECT_FILE: &str = "kitin.yaml";
8
9#[derive(Serialize, Deserialize, Debug)]
10struct KitinModule {
11    source: Option<String>,
12}
13
14#[derive(Serialize, Deserialize, Debug)]
15pub struct KitinProject {
16    name: Option<String>,
17    modules: Option<std::collections::HashMap<String, KitinModule>>,
18}
19
20impl KitinProject {
21    pub fn new() -> KitinProject {
22        KitinProject {
23            name: None,
24            modules: None,
25        }
26    }
27
28    // loads a .kit file from the current working directory
29    pub fn load_from_file(&mut self) {
30        let path = std::path::Path::new(KITIN_PROJECT_FILE);
31        if path.exists() {
32            let mut file = std::fs::File::open(path).unwrap();
33            let mut contents = String::new();
34            file.read_to_string(&mut contents).unwrap();
35            let data: KitinProject = serde_yaml::from_str(&contents).unwrap();
36            self.name = data.name;
37            self.modules = data.modules;
38        }
39    }
40
41    // checks if a directory has a .kit file, and subsequently a kit project.
42    pub fn directory_has_project(directory: &std::path::Path) -> bool {
43        return directory
44            .join(std::path::Path::new(KITIN_PROJECT_FILE))
45            .exists();
46    }
47
48    pub fn save_to_file(&self, directory: &std::path::Path) {
49        let mut file = std::fs::File::create(directory.join(KITIN_PROJECT_FILE)).unwrap();
50        let data = serde_yaml::to_string(&self).unwrap();
51        file.write_all(data.as_bytes()).unwrap();
52    }
53
54    pub fn set_name(&mut self, name: &str) {
55        self.name = Some(name.to_string());
56    }
57}