version_manager/cli/
files.rs1use crate::{
2 VersionError,
3 files::TrackedFiles,
4 version::{Operator, SetTypes},
5};
6use clap::{Args, Parser, Subcommand, value_parser};
7use clio::ClioPath;
8use regex::Regex;
9
10#[derive(Parser, Debug, Clone, PartialEq)]
11#[command(arg_required_else_help(true))]
12pub struct FilesCommand {
13 #[clap(subcommand)]
14 pub command: Files,
15}
16
17impl TryFrom<FilesCommand> for Operator {
18 type Error = VersionError;
19
20 fn try_from(cmd: FilesCommand) -> Result<Self, Self::Error> {
21 cmd.command.try_into()
22 }
23}
24
25impl TryFrom<&FilesCommand> for Operator {
26 type Error = VersionError;
27
28 fn try_from(cmd: &FilesCommand) -> Result<Self, Self::Error> {
29 (&cmd.command).try_into()
30 }
31}
32
33#[derive(Subcommand, Debug, Clone, PartialEq)]
34#[command(arg_required_else_help(true))]
35pub enum Files {
36 Track(TrackFile),
38 Rm(File),
40 Update(File),
42 UpdateAll,
44 List,
46}
47
48impl TryFrom<Files> for Operator {
49 type Error = VersionError;
50
51 fn try_from(cmd: Files) -> Result<Self, Self::Error> {
52 let op = match cmd {
53 Files::Track(track) => Operator::AddFile(track.try_into()?),
54 Files::Rm(file) => Operator::RmFile(file.to_string()),
55 Files::Update(file) => Operator::Update(file.to_string()),
56 Files::UpdateAll => Operator::UpdateAll,
57 Files::List => Operator::ListFiles,
58 };
59 Ok(op)
60 }
61}
62
63impl TryFrom<&Files> for Operator {
64 type Error = VersionError;
65
66 fn try_from(cmd: &Files) -> Result<Self, Self::Error> {
67 let op = match cmd {
68 Files::Track(track) => Operator::AddFile(track.try_into()?),
69 Files::Rm(file) => Operator::RmFile(file.to_string()),
70 Files::Update(file) => Operator::Update(file.to_string()),
71 Files::UpdateAll => Operator::UpdateAll,
72 Files::List => Operator::ListFiles,
73 };
74 Ok(op)
75 }
76}
77
78#[derive(Args, Debug, Clone, PartialEq)]
79#[command(arg_required_else_help(true))]
80pub struct TrackFile {
81 #[arg(value_parser = value_parser!(ClioPath).exists().is_file())]
83 pub path: ClioPath,
84 pub expr: String,
88}
89
90impl TryFrom<&TrackFile> for SetTypes {
91 type Error = VersionError;
92
93 fn try_from(track_file: &TrackFile) -> Result<Self, Self::Error> {
94 let track_file = TrackedFiles::new_from_path_and_regex(
95 track_file.path.to_path_buf(),
96 track_file.expr.clone().parse::<Regex>()?,
97 );
98 Ok(SetTypes::NewFile(track_file))
99 }
100}
101
102impl TryFrom<TrackFile> for SetTypes {
103 type Error = VersionError;
104
105 fn try_from(track_file: TrackFile) -> Result<Self, Self::Error> {
106 let track_file = TrackedFiles::new_from_path_and_regex(
107 track_file.path.to_path_buf(),
108 track_file.expr.clone().parse::<Regex>()?,
109 );
110 Ok(SetTypes::NewFile(track_file))
111 }
112}
113
114#[derive(Args, Debug, Clone, PartialEq)]
115#[command(arg_required_else_help(true))]
116pub struct File {
117 #[arg(value_parser = clap::value_parser!(ClioPath).exists().is_file())]
119 pub path: ClioPath,
120}
121
122impl ToString for File {
123 fn to_string(&self) -> String {
124 self.path.to_string()
125 }
126}
127
128impl TryFrom<&File> for SetTypes {
129 type Error = VersionError;
130
131 fn try_from(file: &File) -> Result<Self, Self::Error> {
132 Ok(SetTypes::String(file.path.to_string()))
133 }
134}
135
136impl TryFrom<File> for SetTypes {
137 type Error = VersionError;
138
139 fn try_from(file: File) -> Result<Self, Self::Error> {
140 Ok(SetTypes::String(file.path.to_string()))
141 }
142}