cli.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use clap::{ArgAction, Parser};
  2. use crate::tiles::TileType;
  3. use crate::tiling::{MissingTilePolicy, VacancyPolicy};
  4. /// Program to generate a larger image made up of different image tiles of the
  5. /// same size
  6. #[derive(Parser)]
  7. #[command(author, version, about)]
  8. #[deny(missing_docs)]
  9. pub struct Arguments {
  10. /// Number of rows
  11. pub rows: u8,
  12. /// Number of columns
  13. pub cols: u8,
  14. /// Optional connector name that tiles must exhibit towards the border of
  15. /// the image; if this option is omitted, bordering tiles may have any
  16. /// connection towards the border.
  17. #[arg(short, long, value_name = "CONNECTOR_NAME")]
  18. pub border: Option<String>,
  19. /// Optional path to a tile configuration file; overrides any configuration
  20. /// file in the tile directory.
  21. #[arg(short, long, group = "config_arg_group", value_name = "PATH")]
  22. pub config: Option<String>,
  23. /// Optional path to a tile directory; if omitted, assume the current
  24. /// working directory.
  25. ///
  26. /// The directory may contain a tile configuration file named “config.yaml”.
  27. #[arg(short = 'd', long, value_name = "PATH")]
  28. pub tile_directory: Option<String>,
  29. #[rustfmt::skip]
  30. /// Optional policy name that specifies how to behave in case there is no
  31. /// tile with a required connectors list.
  32. ///
  33. /// MISSING_TILE_POLICY may have one of the following values:
  34. /// - avoid: Try to avoid leaving empty positions in the tile grid. This may
  35. /// take a considerable amount of time and memory to finish due to
  36. /// backtracking. The run may terminate unsuccessfully in case
  37. /// there is no solution.
  38. /// - exit: (default) Exit immediately if there is no fitting tile; this
  39. /// may be useful for debugging tile configurations.
  40. #[arg(short, long, default_value_t = MissingTilePolicy::Exit,
  41. verbatim_doc_comment)]
  42. pub missing_tile_policy: MissingTilePolicy,
  43. /// Do not use any user defined tile configuration; Instead, assume all
  44. /// tiles have the connectors
  45. /// { north: “c”, south: “c”, east: “c”, west: “c” }
  46. /// and a weight of 1.
  47. #[arg(short, long, group = "config_arg_group", action = ArgAction::SetTrue)]
  48. pub no_config: bool,
  49. /// Path to the output file
  50. #[arg(short, long, value_name = "PATH", default_value = "output/tiled")]
  51. pub output: String,
  52. #[rustfmt::skip]
  53. /// Optional type of tiles; if omitted, the type is guessed by inspecting
  54. /// the tile configuration (see option “--config”).
  55. ///
  56. /// TILE_TYPE may have one of the following values:
  57. /// - raster: raster graphics tiles, e.g. PNG, JPEG or GIF
  58. /// - svg: SVG (Scalable Vector Graphics) tiles
  59. /// - text: text based tiles, e.g. for ASCII art
  60. #[arg(short, long, verbatim_doc_comment)]
  61. pub tile_type: Option<TileType>,
  62. #[rustfmt::skip]
  63. /// Optional policy name that specifies how to handle vacant areas without
  64. /// adjacent open connections.
  65. ///
  66. /// VACANCY_POLICY may have one of the following values:
  67. /// - avoid: Try to avoid leaving vacant areas in the tile grid.
  68. /// This may take a considerable amount of time and memory to
  69. /// finish due to backtracking. The run may terminate
  70. /// unsuccessfully in case there is no solution.
  71. /// - ignore: Ignore and vacant areas in the tile grid untouched.
  72. #[arg(short, long, default_value_t = VacancyPolicy::Ignore, verbatim_doc_comment)]
  73. pub vacancy_policy: VacancyPolicy,
  74. }
  75. #[cfg(test)]
  76. mod tests {
  77. use super::*;
  78. #[test]
  79. fn test_initialization() {
  80. let arguments = Arguments {
  81. rows: 1,
  82. cols: 2,
  83. config: Some("config.yaml".to_string()),
  84. border: Some("brd".to_string()),
  85. missing_tile_policy: MissingTilePolicy::Avoid,
  86. no_config: false,
  87. output: "file".to_string(),
  88. tile_directory: Some("/dir".to_string()),
  89. tile_type: Some(TileType::Text),
  90. vacancy_policy: VacancyPolicy::Ignore,
  91. };
  92. assert_eq!(1, arguments.rows);
  93. assert_eq!(2, arguments.cols);
  94. assert_eq!(Some("config.yaml".to_string()), arguments.config);
  95. assert_eq!(Some("brd".to_string()), arguments.border);
  96. assert_eq!(MissingTilePolicy::Avoid, arguments.missing_tile_policy);
  97. assert_eq!(arguments.no_config, false);
  98. assert_eq!(arguments.output, "file".to_string());
  99. assert_eq!(arguments.tile_directory, Some("/dir".to_string()));
  100. assert_eq!(arguments.tile_type, Some(TileType::Text));
  101. assert_eq!(arguments.vacancy_policy, VacancyPolicy::Ignore);
  102. }
  103. }