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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![deny(warnings, clippy::all)]

use std::path::PathBuf;

use crate::{env::Env, error::Error, status::Status};
use env::Filter;
use logix_type::LogixLoader;
use logix_vfs::RelFs;

pub mod config;
pub mod env;
pub mod error;
pub mod status;

pub enum CmpRes {
    UpToDate,
    Missing,
    LocalAdded,
    LogixAdded,
    Modified,
}

impl CmpRes {
    fn diff_files(a: Vec<u8>, b: Vec<u8>) -> CmpRes {
        if a == b {
            Self::UpToDate
        } else {
            Self::Modified
        }
    }
}

#[derive(Debug, PartialEq)]
pub struct LocalFile {
    pub local: PathBuf,
    pub logix: PathBuf,
}

impl LocalFile {
    fn compare_files(&self) -> Result<CmpRes, Error> {
        let Self { local, logix } = self;
        if local.exists() {
            if logix.exists() {
                let a = std::fs::read(local).map_err(Error::ReadForDiff)?;
                let b = std::fs::read(logix).map_err(Error::ReadForDiff)?;
                Ok(CmpRes::diff_files(a, b))
            } else {
                Ok(CmpRes::LocalAdded)
            }
        } else if logix.exists() {
            Ok(CmpRes::LogixAdded)
        } else {
            Ok(CmpRes::Missing)
        }
    }
}

#[derive(Debug, PartialEq)]
pub struct VirtualFile {
    local: PathBuf,
    content: String,
}

impl VirtualFile {
    fn compare_files(&self) -> Result<CmpRes, Error> {
        todo!()
    }
}

#[derive(Debug, PartialEq)]
pub enum ManagedFile {
    Local(LocalFile),
    Virtual(VirtualFile),
    Recommend(LocalFile),
}

impl ManagedFile {
    pub fn compare_files(&self) -> Result<CmpRes, Error> {
        match self {
            Self::Local(file) => file.compare_files(),
            Self::Virtual(file) => file.compare_files(),
            Self::Recommend(file) => file.compare_files(),
        }
    }
}

pub struct Logix {
    env: Env,
    config: config::Logix,
}

impl Logix {
    pub fn load(env: Env) -> Result<Self, Error> {
        let mut loader = LogixLoader::new(RelFs::new(env.logix_config_dir()));
        Ok(Self {
            env,
            config: loader.load_file("root.logix")?,
        })
    }

    pub fn config(&self) -> &config::Logix {
        &self.config
    }

    pub fn calculate_managed_files(&self) -> Result<Vec<ManagedFile>, Error> {
        let config::Logix { home } = &self.config;
        let mut ret = Vec::new();
        {
            let config::UserProfile {
                username: _,
                name: _,
                email: _,
                shell,
                editor,
                ssh,
            } = home;
            match shell {
                Some(config::Shell::Bash) => {
                    ret.extend([self.env.calculate_managed_dotfile(".bashrc")])
                }
                None => {}
            }
            match editor {
                Some(config::Editor::Helix) => {
                    self.env
                        .calculate_managed_config_dir("helix", &mut ret, Filter::HELIX)?
                }
                None => {}
            }
            match ssh {
                Some(config::Ssh::OpenSSH {
                    agent: config::SshAgent::SystemD,
                    keys,
                }) => {
                    ret.push(
                        self.env
                            .calculate_managed_config_file("systemd/user/ssh-agent.service"),
                    );
                    debug_assert!(keys.is_empty(), "TODO: {keys:?}");
                }
                None => {}
            }
        }
        Ok(ret)
    }

    pub fn calculate_status(&self) -> Result<Status, Error> {
        Status::calculate(self)
    }
}