summaryrefslogtreecommitdiff
path: root/src/cmd/cmds/status.rs
blob: d73a28abeb96f01473567d0b556c4cc2f0a5d174 (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
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
use std::{
    collections::{HashMap, HashSet},
    path::PathBuf,
    time::SystemTime,
};

use clap::Parser;
use just_enough_vcs::vcs::data::{
    local::workspace_analyzer::{
        CreatedRelativePathBuf, FromRelativePathBuf, LostRelativePathBuf, ModifiedRelativePathBuf,
        ToRelativePathBuf,
    },
    member::MemberId,
    sheet::SheetName,
    vault::virtual_file::VirtualFileId,
};
use serde::Serialize;

use crate::{
    cmd::{
        cmd_system::{JVCommand, JVCommandContext},
        errors::{CmdExecuteError, CmdPrepareError, CmdRenderError},
        renderer::{JVRenderResult, JVResultRenderer},
    },
    utils::workspace_reader::LocalWorkspaceReader,
};

pub struct JVStatusCommand;

#[derive(Parser, Debug)]
pub struct JVStatusArgument;

#[derive(Serialize)]
pub struct JVStatusResult {
    pub current_account: MemberId,
    pub current_sheet: SheetName,
    pub moved: HashMap<VirtualFileId, (FromRelativePathBuf, ToRelativePathBuf)>,
    pub created: HashSet<CreatedRelativePathBuf>,
    pub lost: HashSet<LostRelativePathBuf>,
    pub erased: HashSet<PathBuf>,
    pub modified: HashSet<ModifiedRelativePathBuf>,
    pub update_time: SystemTime,
    pub now_time: SystemTime,
}

impl Default for JVStatusResult {
    fn default() -> Self {
        Self {
            current_account: MemberId::default(),
            current_sheet: SheetName::default(),
            moved: HashMap::default(),
            created: HashSet::default(),
            lost: HashSet::default(),
            erased: HashSet::default(),
            modified: HashSet::default(),
            update_time: SystemTime::now(),
            now_time: SystemTime::now(),
        }
    }
}

impl JVCommand<JVStatusArgument, JVStatusResult, JVStatusResult, JVStatusRenderer>
    for JVStatusCommand
{
    async fn prepare(
        _args: JVStatusArgument,
        _ctx: JVCommandContext,
    ) -> Result<JVStatusResult, CmdPrepareError> {
        // Initialize a reader for the local workspace and a default result structure
        let mut reader = LocalWorkspaceReader::default();
        let mut input = JVStatusResult::default();

        // Analyze the current status of the local workspace
        // (detects changes like created, modified, moved, etc.)
        let analyzed = reader.analyze_local_status().await?;

        // Retrieve the current account (member) ID
        let account = reader.current_account().await?;

        // Retrieve the name of the current sheet
        let sheet_name = reader.sheet_name().await?;

        // Get the timestamp of the last update, defaulting to the current time if not available
        let update_time = reader
            .latest_info()
            .await?
            .update_instant
            .unwrap_or(SystemTime::now());

        // Record the current system time
        let now_time = SystemTime::now();

        // Populate the result structure with the gathered data
        input.current_account = account;
        input.current_sheet = sheet_name;
        input.moved = analyzed.moved;
        input.created = analyzed.created;
        input.lost = analyzed.lost;
        input.erased = analyzed.erased;
        input.modified = analyzed.modified;
        input.update_time = update_time;
        input.now_time = now_time;

        Ok(input)
    }

    async fn exec(input: JVStatusResult) -> Result<JVStatusResult, CmdExecuteError> {
        Ok(input) // Analyze command, no needs execute
    }

    fn get_help_str() -> String {
        "".to_string()
    }
}

pub struct JVStatusRenderer;

impl JVResultRenderer<JVStatusResult> for JVStatusRenderer {
    async fn render(data: &JVStatusResult) -> Result<JVRenderResult, CmdRenderError> {
        todo!()
    }
}