comparison rust/hg-core/examples/nodemap/main.rs @ 49755:14bfd22a57a9

hg-core: upgrade `clap` dependency Upgrading is a finally possible now that we're supporting a more recent version of Rust. This required changes in the `nodemap` example.
author Rapha?l Gom?s <rgomes@octobus.net>
date Mon, 14 Nov 2022 17:14:20 +0100
parents e834b79def74
children 58074252db3c
comparison
equal deleted inserted replaced
49754:a5447a4a8c5d 49755:14bfd22a57a9
1 // Copyright 2019-2020 Georges Racinet <georges.racinet@octobus.net> 1 // Copyright 2019-2020 Georges Racinet <georges.racinet@octobus.net>
2 // 2 //
3 // This software may be used and distributed according to the terms of the 3 // This software may be used and distributed according to the terms of the
4 // GNU General Public License version 2 or any later version. 4 // GNU General Public License version 2 or any later version.
5 5
6 use clap::*;
7 use hg::revlog::node::*; 6 use hg::revlog::node::*;
8 use hg::revlog::nodemap::*; 7 use hg::revlog::nodemap::*;
9 use hg::revlog::*; 8 use hg::revlog::*;
10 use memmap2::MmapOptions; 9 use memmap2::MmapOptions;
11 use rand::Rng; 10 use rand::Rng;
12 use std::fs::File; 11 use std::fs::File;
13 use std::io; 12 use std::io;
14 use std::io::Write; 13 use std::io::Write;
15 use std::path::{Path, PathBuf}; 14 use std::path::{Path, PathBuf};
16 use std::str::FromStr;
17 use std::time::Instant; 15 use std::time::Instant;
18 16
19 mod index; 17 mod index;
20 use index::Index; 18 use index::Index;
21 19
84 last 82 last
85 ); 83 );
86 } 84 }
87 85
88 fn main() { 86 fn main() {
89 let matches = App::new("Nodemap pure Rust example") 87 use clap::{Parser, Subcommand};
90 .arg(
91 Arg::with_name("REPOSITORY")
92 .help("Path to the repository, always necessary for its index")
93 .required(true),
94 )
95 .arg(
96 Arg::with_name("NODEMAP_FILE")
97 .help("Path to the nodemap file, independent of REPOSITORY")
98 .required(true),
99 )
100 .subcommand(
101 SubCommand::with_name("create")
102 .about("Create NODEMAP_FILE by scanning repository index"),
103 )
104 .subcommand(
105 SubCommand::with_name("query")
106 .about("Query NODEMAP_FILE for PREFIX")
107 .arg(Arg::with_name("PREFIX").required(true)),
108 )
109 .subcommand(
110 SubCommand::with_name("bench")
111 .about(
112 "Perform #QUERIES random successful queries on NODEMAP_FILE")
113 .arg(Arg::with_name("QUERIES").required(true)),
114 )
115 .get_matches();
116 88
117 let repo = matches.value_of("REPOSITORY").unwrap(); 89 #[derive(Parser)]
118 let nm_path = matches.value_of("NODEMAP_FILE").unwrap(); 90 #[command()]
119 91 /// Nodemap pure Rust example
120 let index = mmap_index(&Path::new(repo)); 92 struct App {
121 93 // Path to the repository, always necessary for its index
122 if let Some(_) = matches.subcommand_matches("create") { 94 #[arg(short, long)]
123 println!("Creating nodemap file {} for repository {}", nm_path, repo); 95 repository: PathBuf,
124 create(&index, &Path::new(nm_path)).unwrap(); 96 // Path to the nodemap file, independent of REPOSITORY
125 return; 97 #[arg(short, long)]
98 nodemap_file: PathBuf,
99 #[command(subcommand)]
100 command: Command,
126 } 101 }
127 102
128 let nm = mmap_nodemap(&Path::new(nm_path)); 103 #[derive(Subcommand)]
129 if let Some(matches) = matches.subcommand_matches("query") { 104 enum Command {
130 let prefix = matches.value_of("PREFIX").unwrap(); 105 /// Create `NODEMAP_FILE` by scanning repository index
131 println!( 106 Create,
132 "Querying {} in nodemap file {} of repository {}", 107 /// Query `NODEMAP_FILE` for `prefix`
133 prefix, nm_path, repo 108 Query { prefix: String },
134 ); 109 /// Perform #`QUERIES` random successful queries on `NODEMAP_FILE`
135 query(&index, &nm, prefix); 110 Bench { queries: usize },
136 } 111 }
137 if let Some(matches) = matches.subcommand_matches("bench") { 112
138 let queries = 113 let app = App::parse();
139 usize::from_str(matches.value_of("QUERIES").unwrap()).unwrap(); 114
140 println!( 115 let repo = &app.repository;
141 "Doing {} random queries in nodemap file {} of repository {}", 116 let nm_path = &app.nodemap_file;
142 queries, nm_path, repo 117
143 ); 118 let index = mmap_index(repo);
144 bench(&index, &nm, queries); 119 let nm = mmap_nodemap(nm_path);
120
121 match &app.command {
122 Command::Create => {
123 println!(
124 "Creating nodemap file {} for repository {}",
125 nm_path.display(),
126 repo.display()
127 );
128 create(&index, &Path::new(nm_path)).unwrap();
129 }
130 Command::Bench { queries } => {
131 println!(
132 "Doing {} random queries in nodemap file {} of repository {}",
133 queries,
134 nm_path.display(),
135 repo.display()
136 );
137 bench(&index, &nm, *queries);
138 }
139 Command::Query { prefix } => {
140 println!(
141 "Querying {} in nodemap file {} of repository {}",
142 prefix,
143 nm_path.display(),
144 repo.display()
145 );
146 query(&index, &nm, prefix);
147 }
145 } 148 }
146 } 149 }