From c7850d24e95f2e5338a02e2dfe6c425ec1ef2ae8 Mon Sep 17 00:00:00 2001 From: Matthew Hall Date: Thu, 4 Apr 2024 00:42:02 +1300 Subject: Support for literal values --- src/main.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index a96f1a5..2d7c335 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,6 +75,7 @@ type ProgResult = Result; #[derive(Debug, Clone)] enum DropIn { Any, + Literal(String), Basic(String), Var(String), OneOf(Vec), @@ -99,6 +100,16 @@ impl DropIn { return Ok(DropIn::OneOf(options)) } + // TODO: investigate viability of supporting escape sequences + if let Some(inner) = text.strip_prefix('"') + .and_then(|s| s.strip_suffix('"')) + { + // REVIEW: investigate interactions of filenames that contain '"' + if !inner.contains('"') { + return Ok(DropIn::Literal(inner.to_string())); + } + } + if let Some(var_name) = text.strip_prefix('?') { return Ok(DropIn::Var(var_name.to_string())); } @@ -149,6 +160,10 @@ impl CategorySet { } pub fn random_from_drop_in(&mut self, drop_in: &DropIn) -> ProgResult { + if let DropIn::Literal(s) = drop_in { + return Ok(s.clone()); + } + let simple_di = if let DropIn::OneOf(ds) = drop_in { ds.choose(&mut thread_rng()) .expect("got a OneOf that is empty, somehow") @@ -157,6 +172,9 @@ impl CategorySet { }; let category = match simple_di { + DropIn::Literal(s) => { + return Ok(s.clone()); + }, DropIn::Basic(s) => { OsString::from(s) }, @@ -280,6 +298,7 @@ fn read_lines(path: &PathBuf) -> ProgResult> { Ok(lines) } +// REVIEW: There has to be a better way to do this. Separate into multiple functions? fn read_files(dir_path: &str, unique: bool) -> ProgResult<(SentenceSet, CategorySet)> { let mut opt_sentences: Option = None; let mut categories = CategorySet::new(unique); -- cgit v1.2.1