summaryrefslogtreecommitdiff
path: root/src/cmds/cmd/storage_build.rs
blob: 8e4d39c2ea97f09f810b26bbd223e88cf1cf86af (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
use crate::{
    cmd_output,
    cmds::{
        arg::storage_build::JVStorageBuildArgument, collect::empty::JVEmptyCollect,
        r#in::storage_rw::JVStorageRWInput, out::none::JVNoneOutput,
    },
    systems::cmd::{
        cmd_system::JVCommandContext,
        errors::{CmdExecuteError, CmdPrepareError},
    },
};
use cli_utils::display::md;
use cmd_system_macros::exec;
use just_enough_vcs::system::storage_system::{error::StorageIOError, store::build_file};
use rust_i18n::t;
use std::any::TypeId;

pub struct JVStorageBuildCommand;
type Cmd = JVStorageBuildCommand;
type Arg = JVStorageBuildArgument;
type In = JVStorageRWInput;
type Collect = JVEmptyCollect;

fn help_str() -> String {
    todo!()
}

async fn prepare(args: &Arg, _ctx: &JVCommandContext) -> Result<In, CmdPrepareError> {
    let output_file = match &args.output_file {
        Some(v) => v.clone(),
        None => args.index_file.clone().with_extension("unknown"),
    };

    let (input, storage, output) = just_enough_vcs::system::storage_system::store::precheck(
        args.index_file.clone(),
        args.storage.clone(),
        output_file,
    )
    .await?;

    Ok(JVStorageRWInput {
        input,
        storage,
        output,
        chunking_policy: None,
    })
}

async fn collect(_args: &Arg, _ctx: &JVCommandContext) -> Result<Collect, CmdPrepareError> {
    Ok(Collect {})
}

#[exec]
async fn exec(
    input: In,
    _collect: Collect,
) -> Result<(Box<dyn std::any::Any + Send + 'static>, TypeId), CmdExecuteError> {
    build_file(input.input, input.storage, input.output)
        .await
        .map_err(|e| match e {
            StorageIOError::IOErr(error) => CmdExecuteError::Io(error),
            StorageIOError::HashTooShort => {
                CmdExecuteError::Error(md(t!("storage_write.hash_too_short")).to_string())
            }
        })?;

    cmd_output!(JVNoneOutput => JVNoneOutput {})
}

crate::command_template!();