comparison rust/rhg/src/commands/files.rs @ 46136:dca9cb99971c

rust: replace most "operation" structs with functions The hg-core crate has a partially-formed concept of "operation", represented as structs with constructors and a `run` method. Each struct?s contructor takes different parameters, and each `run` has a different return type. Constructors typically don?t do much more than store parameters for `run` to access them. There was a comment about adding an `Operation` trait when the language supports expressing something so general, but it?s hard to imagine how operations with such different APIs could be used in a generic context. This commit starts removing the concept of "operation", since those are pretty much just functions. Differential Revision: https://phab.mercurial-scm.org/D9595
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 14 Dec 2020 14:59:23 +0100
parents cc6faec62cb7
children 8a4914397d02
comparison
equal deleted inserted replaced
46135:cc6faec62cb7 46136:dca9cb99971c
1 use crate::commands::Command; 1 use crate::commands::Command;
2 use crate::error::{CommandError, CommandErrorKind}; 2 use crate::error::{CommandError, CommandErrorKind};
3 use crate::ui::utf8_to_local; 3 use crate::ui::utf8_to_local;
4 use crate::ui::Ui; 4 use crate::ui::Ui;
5 use hg::operations::FindRoot; 5 use hg::operations::find_root;
6 use hg::operations::{ 6 use hg::operations::{
7 ListDirstateTrackedFiles, ListDirstateTrackedFilesError, 7 list_rev_tracked_files, ListRevTrackedFilesError,
8 ListDirstateTrackedFilesErrorKind, 8 ListRevTrackedFilesErrorKind,
9 }; 9 };
10 use hg::operations::{ 10 use hg::operations::{
11 ListRevTrackedFiles, ListRevTrackedFilesError, 11 Dirstate, ListDirstateTrackedFilesError, ListDirstateTrackedFilesErrorKind,
12 ListRevTrackedFilesErrorKind,
13 }; 12 };
14 use hg::requirements; 13 use hg::requirements;
15 use hg::utils::files::{get_bytes_from_path, relativize_path}; 14 use hg::utils::files::{get_bytes_from_path, relativize_path};
16 use hg::utils::hg_path::{HgPath, HgPathBuf}; 15 use hg::utils::hg_path::{HgPath, HgPathBuf};
17 use std::path::Path; 16 use std::path::Path;
55 } 54 }
56 } 55 }
57 56
58 impl<'a> Command for FilesCommand<'a> { 57 impl<'a> Command for FilesCommand<'a> {
59 fn run(&self, ui: &Ui) -> Result<(), CommandError> { 58 fn run(&self, ui: &Ui) -> Result<(), CommandError> {
60 let root = FindRoot::new().run()?; 59 let root = find_root()?;
61 requirements::check(&root)?; 60 requirements::check(&root)?;
62 if let Some(rev) = self.rev { 61 if let Some(rev) = self.rev {
63 let mut operation = ListRevTrackedFiles::new(&root, rev) 62 let files = list_rev_tracked_files(&root, rev)
64 .map_err(|e| map_rev_error(rev, e))?; 63 .map_err(|e| map_rev_error(rev, e))?;
65 let files = operation.run().map_err(|e| map_rev_error(rev, e))?; 64 self.display_files(ui, &root, files.iter())
66 self.display_files(ui, &root, files)
67 } else { 65 } else {
68 let mut operation = ListDirstateTrackedFiles::new(&root) 66 let distate = Dirstate::new(&root).map_err(map_dirstate_error)?;
69 .map_err(map_dirstate_error)?; 67 let files = distate.tracked_files().map_err(map_dirstate_error)?;
70 let files = operation.run().map_err(map_dirstate_error)?;
71 self.display_files(ui, &root, files) 68 self.display_files(ui, &root, files)
72 } 69 }
73 } 70 }
74 } 71 }
75 72