You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.2KB

  1. use ansi_term::Colour::{Blue, Green, Red};
  2. use ansi_term::Style;
  3. use clap::ArgMatches;
  4. use serde_json;
  5. use std::convert::From;
  6. use std::io;
  7. use std::io::Write;
  8. use std::path::PathBuf;
  9. use tantivy;
  10. use tantivy::schema::Cardinality;
  11. use tantivy::schema::*;
  12. use tantivy::Index;
  13. pub fn run_new_cli(matches: &ArgMatches) -> Result<(), String> {
  14. let index_directory = PathBuf::from(matches.value_of("index").unwrap());
  15. run_new(index_directory).map_err(|e| format!("{:?}", e))
  16. }
  17. fn prompt_input<P: Fn(&str) -> Result<(), String>>(prompt_text: &str, predicate: P) -> String {
  18. loop {
  19. print!(
  20. "{prompt_text:<width$} ? ",
  21. prompt_text = Style::new().bold().fg(Blue).paint(prompt_text),
  22. width = 40
  23. );
  24. io::stdout().flush().unwrap();
  25. let mut buffer = String::new();
  26. io::stdin()
  27. .read_line(&mut buffer)
  28. .ok()
  29. .expect("Failed to read line");
  30. let answer = buffer.trim_end().to_string();
  31. match predicate(&answer) {
  32. Ok(()) => {
  33. return answer;
  34. }
  35. Err(msg) => {
  36. println!("Error: {}", Style::new().bold().fg(Red).paint(msg));
  37. }
  38. }
  39. }
  40. }
  41. fn field_name_validate(field_name: &str) -> Result<(), String> {
  42. if is_valid_field_name(field_name) {
  43. Ok(())
  44. } else {
  45. Err(String::from(
  46. "Field name must match the pattern [_a-zA-Z0-9]+",
  47. ))
  48. }
  49. }
  50. fn prompt_options(msg: &str, codes: Vec<char>) -> char {
  51. let options_string: Vec<String> = codes.iter().map(|c| format!("{}", c)).collect();
  52. let options = options_string.join("/");
  53. let predicate = |entry: &str| {
  54. if entry.len() != 1 {
  55. return Err(format!("Invalid input. Options are ({})", options));
  56. }
  57. let c = entry.chars().next().unwrap().to_ascii_uppercase();
  58. if codes.contains(&c) {
  59. return Ok(());
  60. } else {
  61. return Err(format!("Invalid input. Options are ({})", options));
  62. }
  63. };
  64. let message = format!("{} ({})", msg, options);
  65. let entry = prompt_input(&message, predicate);
  66. entry.chars().next().unwrap().to_ascii_uppercase()
  67. }
  68. fn prompt_yn(msg: &str) -> bool {
  69. prompt_options(msg, vec!['Y', 'N']) == 'Y'
  70. }
  71. fn ask_add_field_text(field_name: &str, schema_builder: &mut SchemaBuilder) {
  72. let mut text_options = TextOptions::default();
  73. if prompt_yn("Should the field be stored") {
  74. text_options = text_options.set_stored();
  75. }
  76. if prompt_yn("Should the field be indexed") {
  77. let mut text_indexing_options = TextFieldIndexing::default()
  78. .set_index_option(IndexRecordOption::Basic)
  79. .set_tokenizer("en_stem");
  80. if prompt_yn("Should the term be tokenized?") {
  81. if prompt_yn("Should the term frequencies (per doc) be in the index") {
  82. if prompt_yn("Should the term positions (per doc) be in the index") {
  83. text_indexing_options = text_indexing_options
  84. .set_index_option(IndexRecordOption::WithFreqsAndPositions);
  85. } else {
  86. text_indexing_options =
  87. text_indexing_options.set_index_option(IndexRecordOption::WithFreqs);
  88. }
  89. }
  90. } else {
  91. text_indexing_options = text_indexing_options.set_tokenizer("raw");
  92. }
  93. text_options = text_options.set_indexing_options(text_indexing_options);
  94. }
  95. schema_builder.add_text_field(field_name, text_options);
  96. }
  97. fn ask_add_field_u64(field_name: &str, schema_builder: &mut SchemaBuilder) {
  98. let mut u64_options = IntOptions::default();
  99. if prompt_yn("Should the field be stored") {
  100. u64_options = u64_options.set_stored();
  101. }
  102. if prompt_yn("Should the field be fast") {
  103. u64_options = u64_options.set_fast(Cardinality::SingleValue);
  104. }
  105. if prompt_yn("Should the field be indexed") {
  106. u64_options = u64_options.set_indexed();
  107. }
  108. schema_builder.add_u64_field(field_name, u64_options);
  109. }
  110. fn ask_add_field(schema_builder: &mut SchemaBuilder) {
  111. println!("\n\n");
  112. let field_name = prompt_input("New field name ", field_name_validate);
  113. let text_or_integer = prompt_options("Text or unsigned 32-bit integer", vec!['T', 'I']);
  114. if text_or_integer == 'T' {
  115. ask_add_field_text(&field_name, schema_builder);
  116. } else {
  117. ask_add_field_u64(&field_name, schema_builder);
  118. }
  119. }
  120. fn run_new(directory: PathBuf) -> tantivy::Result<()> {
  121. println!(
  122. "\n{} ",
  123. Style::new().bold().fg(Green).paint("Creating new index")
  124. );
  125. println!(
  126. "{} ",
  127. Style::new()
  128. .bold()
  129. .fg(Green)
  130. .paint("Let's define it's schema!")
  131. );
  132. let mut schema_builder = SchemaBuilder::default();
  133. loop {
  134. ask_add_field(&mut schema_builder);
  135. if !prompt_yn("Add another field") {
  136. break;
  137. }
  138. }
  139. let schema = schema_builder.build();
  140. let schema_json = format!("{}", serde_json::to_string_pretty(&schema).unwrap());
  141. println!("\n{}\n", Style::new().fg(Green).paint(schema_json));
  142. Index::create_in_dir(&directory, schema)?;
  143. Ok(())
  144. }