Skip to content

Commit

Permalink
fix(dynamic string): always sending partial string on initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Jan 25, 2024
1 parent af7e037 commit 63304a9
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/dynamic_value/dynamic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn dynamic_string<F>(input: &str, mut f: F)
where
F: FnMut(String) + 'static,
{
let tokens = parse_input(input);
let (tokens, is_static) = parse_input(input);

let label_parts = arc_mut!(vec![]);
let (tx, rx) = mpsc::channel(32);
Expand Down Expand Up @@ -91,17 +91,17 @@ where
glib_recv_mpsc!(rx , val => f(val));

// initialize
{
if is_static {
let label_parts = lock!(label_parts).join("");
try_send!(tx, label_parts);
}
}

/// Parses the input string into static and dynamic segments
fn parse_input(input: &str) -> Vec<DynamicStringSegment> {
fn parse_input(input: &str) -> (Vec<DynamicStringSegment>, bool) {
// short-circuit parser if it's all static
if !input.contains("{{") && !input.contains('#') {
return vec![DynamicStringSegment::Static(input.to_string())];
return (vec![DynamicStringSegment::Static(input.to_string())], true);
}

let mut tokens = vec![];
Expand Down Expand Up @@ -129,7 +129,7 @@ fn parse_input(input: &str) -> Vec<DynamicStringSegment> {
chars.drain(..skip);
}

tokens
(tokens, false)
}

fn parse_script(chars: &[char]) -> (DynamicStringSegment, usize) {
Expand Down Expand Up @@ -190,7 +190,7 @@ mod tests {
#[test]
fn test_static() {
const INPUT: &str = "hello world";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 1);
assert!(matches!(&tokens[0], DynamicStringSegment::Static(value) if value == INPUT))
Expand All @@ -199,7 +199,7 @@ mod tests {
#[test]
fn test_static_odd_char_count() {
const INPUT: &str = "hello";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 1);
assert!(matches!(&tokens[0], DynamicStringSegment::Static(value) if value == INPUT))
Expand All @@ -208,7 +208,7 @@ mod tests {
#[test]
fn test_script() {
const INPUT: &str = "{{echo hello}}";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 1);
assert!(
Expand All @@ -219,7 +219,7 @@ mod tests {
#[test]
fn test_variable() {
const INPUT: &str = "#variable";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 1);
assert!(
Expand All @@ -230,7 +230,7 @@ mod tests {
#[test]
fn test_static_script() {
const INPUT: &str = "hello {{echo world}}";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 2);
assert!(matches!(&tokens[0], DynamicStringSegment::Static(str) if str == "hello "));
Expand All @@ -242,7 +242,7 @@ mod tests {
#[test]
fn test_static_variable() {
const INPUT: &str = "hello #subject";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 2);
assert!(matches!(&tokens[0], DynamicStringSegment::Static(str) if str == "hello "));
Expand All @@ -254,7 +254,7 @@ mod tests {
#[test]
fn test_static_script_static() {
const INPUT: &str = "hello {{echo world}} foo";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 3);
assert!(matches!(&tokens[0], DynamicStringSegment::Static(str) if str == "hello "));
Expand All @@ -267,7 +267,7 @@ mod tests {
#[test]
fn test_static_variable_static() {
const INPUT: &str = "hello #subject foo";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 3);
assert!(matches!(&tokens[0], DynamicStringSegment::Static(str) if str == "hello "));
Expand All @@ -280,7 +280,7 @@ mod tests {
#[test]
fn test_static_script_variable() {
const INPUT: &str = "hello {{echo world}} #foo";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 4);
assert!(matches!(&tokens[0], DynamicStringSegment::Static(str) if str == "hello "));
Expand All @@ -296,7 +296,7 @@ mod tests {
#[test]
fn test_escape_hash() {
const INPUT: &str = "number ###num";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 3);
assert!(matches!(&tokens[0], DynamicStringSegment::Static(str) if str == "number "));
Expand All @@ -309,7 +309,7 @@ mod tests {
#[test]
fn test_script_with_hash() {
const INPUT: &str = "{{echo #hello}}";
let tokens = parse_input(INPUT);
let (tokens, _) = parse_input(INPUT);

assert_eq!(tokens.len(), 1);
assert!(
Expand Down

0 comments on commit 63304a9

Please sign in to comment.