Add write! macro

pull/6/head
sgoudham 3 years ago
parent 526f42cf98
commit cf1ff5455e
Signed by: hammy
GPG Key ID: 44E818FD5457EEA4

@ -49,6 +49,12 @@ macro_rules! random_int {
}; };
} }
macro_rules! write {
($out:expr, $bytes:expr) => {
$out.write_all($bytes)
};
}
#[derive(Debug)] #[derive(Debug)]
pub struct UwUify<'a> { pub struct UwUify<'a> {
text: &'a str, text: &'a str,
@ -181,46 +187,50 @@ impl<'a> UwUify<'a> {
if !self.is_runtime { if !self.is_runtime {
if random_value <= self.faces { if random_value <= self.faces {
if self.ascii { if self.ascii {
out.write_all( write!(
ASCII_FACES[random_int!(&mut seeder, 0..ASCII_FACES_SIZE)], out,
ASCII_FACES[random_int!(&mut seeder, 0..ASCII_FACES_SIZE)]
)?; )?;
} else { } else {
out.write_all( write!(
MIXED_FACES[random_int!(&mut seeder, 0..MIXED_FACES_SIZE)], out,
MIXED_FACES[random_int!(&mut seeder, 0..MIXED_FACES_SIZE)]
)?; )?;
} }
} else if random_value <= self.actions { } else if random_value <= self.actions {
out.write_all(ACTIONS[random_int!(&mut seeder, 0..ACTIONS_SIZE)])?; write!(out, ACTIONS[random_int!(&mut seeder, 0..ACTIONS_SIZE)])?;
} else if random_value <= self.stutters { } else if random_value <= self.stutters {
match word[0] { match word[0] {
b'L' | b'R' => out.write_all(b"W"), b'L' | b'R' => write!(out, b"W"),
b'l' | b'r' => out.write_all(b"w"), b'l' | b'r' => write!(out, b"w"),
byte => out.write_all(&[byte]), byte => write!(out, &[byte]),
}?; }?;
out.write_all(b"-")?; write!(out, b"-")?;
} }
} else { } else {
if random_value <= self.faces { if random_value <= self.faces {
if self.ascii { if self.ascii {
out.write_all( write!(
ASCII_FACES[random_int!(&mut seeder, 0..ASCII_FACES_SIZE)], out,
ASCII_FACES[random_int!(&mut seeder, 0..ASCII_FACES_SIZE)]
)?; )?;
} else { } else {
out.write_all( write!(
MIXED_FACES[random_int!(&mut seeder, 0..MIXED_FACES_SIZE)], out,
MIXED_FACES[random_int!(&mut seeder, 0..MIXED_FACES_SIZE)]
)?; )?;
} }
} }
if random_value <= self.actions { if random_value <= self.actions {
out.write_all(ACTIONS[random_int!(&mut seeder, 0..ACTIONS_SIZE)])?; write!(out, ACTIONS[random_int!(&mut seeder, 0..ACTIONS_SIZE)])?;
} }
if random_value <= self.stutters { if random_value <= self.stutters {
match word[0] { match word[0] {
b'L' | b'R' => out.write_all(b"W"), b'L' | b'R' => write!(out, b"W"),
b'l' | b'r' => out.write_all(b"w"), b'l' | b'r' => write!(out, b"w"),
byte => out.write_all(&[byte]), byte => write!(out, &[byte]),
}?; }?;
out.write_all(b"-")?; write!(out, b"-")?;
} }
} }
@ -231,26 +241,27 @@ impl<'a> UwUify<'a> {
> 0 > 0
|| random_value > self.words || random_value > self.words
{ {
out.write_all(word)?; write!(out, word)?;
} else { } else {
(0..word.len()).try_for_each(|index| match word[index] { (0..word.len()).try_for_each(|index| match word[index] {
b'L' | b'R' => out.write_all(b"W"), b'L' | b'R' => write!(out, b"W"),
b'l' | b'r' => out.write_all(b"w"), b'l' | b'r' => write!(out, b"w"),
b'A' | b'E' | b'I' | b'O' | b'U' | b'a' | b'e' | b'i' | b'o' | b'u' => { b'A' | b'E' | b'I' | b'O' | b'U' | b'a' | b'e' | b'i' | b'o' | b'u' => {
match word.get(index - 1).unwrap_or(&word[0]) { match word.get(index - 1).unwrap_or(&word[0]) {
b'N' | b'n' => out.write_all(&[b'y', word[index]]), b'N' | b'n' => write!(out, &[b'y', word[index]]),
_ => out.write_all(&[word[index]]), _ => write!(out, &[word[index]]),
} }
} }
byte => out.write_all(&[byte]), byte => write!(out, &[byte]),
})?; })?;
} }
out.write_all(b" ") write!(out, b" ")
})?; })?;
out.write_all(b"\n") write!(out, b"\n")
}) })
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[cfg(feature = "bench")] #[cfg(feature = "bench")]

Loading…
Cancel
Save