2017-08-08 21:02:54 -05:00
|
|
|
// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-02-07 19:49:13 -06:00
|
|
|
//! The format string logic for ucg format expressions.
|
2017-08-08 21:02:54 -05:00
|
|
|
use std::clone::Clone;
|
|
|
|
use std::error::Error;
|
|
|
|
|
2018-12-06 12:23:52 -06:00
|
|
|
use crate::ast::*;
|
|
|
|
use crate::error;
|
2017-08-08 21:02:54 -05:00
|
|
|
|
2018-02-07 19:49:13 -06:00
|
|
|
/// Implements the logic for format strings in UCG format expressions.
|
2017-08-08 21:02:54 -05:00
|
|
|
pub struct Formatter<V: Into<String> + Clone> {
|
|
|
|
tmpl: String,
|
|
|
|
args: Vec<V>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: Into<String> + Clone> Formatter<V> {
|
2018-02-07 19:49:13 -06:00
|
|
|
/// Constructs a Formatter with a template and args.
|
2017-08-08 21:02:54 -05:00
|
|
|
pub fn new<S: Into<String>>(tmpl: S, args: Vec<V>) -> Self {
|
2017-11-05 15:26:52 -06:00
|
|
|
Formatter {
|
2017-08-08 21:02:54 -05:00
|
|
|
tmpl: tmpl.into(),
|
|
|
|
args: args,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-07 19:49:13 -06:00
|
|
|
/// Renders a formatter to a string or returns an error.
|
2018-02-15 19:55:43 -06:00
|
|
|
///
|
2018-02-07 19:49:13 -06:00
|
|
|
/// If the formatter has the wrong number of arguments for the number of replacements
|
|
|
|
/// it will return an error. Otherwise it will return the formatted string.
|
2019-01-05 13:27:51 -06:00
|
|
|
pub fn render(&self, pos: &Position) -> Result<String, Box<dyn Error>> {
|
2017-08-08 21:02:54 -05:00
|
|
|
let mut buf = String::new();
|
|
|
|
let mut should_escape = false;
|
|
|
|
let mut count = 0;
|
|
|
|
for c in self.tmpl.chars() {
|
|
|
|
if c == '@' && !should_escape {
|
|
|
|
if count == self.args.len() {
|
2018-11-07 18:24:44 -06:00
|
|
|
return Err(Box::new(error::BuildError::new(
|
2018-02-15 19:55:43 -06:00
|
|
|
"Too few arguments to string \
|
|
|
|
formatter.",
|
|
|
|
error::ErrorType::FormatError,
|
|
|
|
pos.clone(),
|
|
|
|
)));
|
2017-08-08 21:02:54 -05:00
|
|
|
}
|
|
|
|
let arg = self.args[count].clone();
|
|
|
|
let strval = arg.into();
|
|
|
|
buf.push_str(&strval);
|
|
|
|
count += 1;
|
|
|
|
} else if c == '\\' && !should_escape {
|
|
|
|
should_escape = true;
|
|
|
|
} else {
|
|
|
|
buf.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if self.args.len() != count {
|
2018-11-07 18:24:44 -06:00
|
|
|
return Err(Box::new(error::BuildError::new(
|
2018-02-15 19:55:43 -06:00
|
|
|
"Too many arguments to string \
|
|
|
|
formatter.",
|
|
|
|
error::ErrorType::FormatError,
|
|
|
|
pos.clone(),
|
|
|
|
)));
|
2017-08-08 21:02:54 -05:00
|
|
|
}
|
|
|
|
return Ok(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::Formatter;
|
2018-12-06 12:23:52 -06:00
|
|
|
use crate::ast::Position;
|
2017-08-08 21:02:54 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_happy_path() {
|
|
|
|
let formatter = Formatter::new("foo @ @ \\@", vec!["bar", "quux"]);
|
2018-11-05 21:34:12 -06:00
|
|
|
let pos = Position::new(0, 0, 0);
|
2017-12-09 10:02:45 -06:00
|
|
|
assert_eq!(formatter.render(&pos).unwrap(), "foo bar quux @");
|
2017-08-08 21:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_happy_wrong_too_few_args() {
|
|
|
|
let formatter = Formatter::new("foo @ @ \\@", vec!["bar"]);
|
2018-11-05 21:34:12 -06:00
|
|
|
let pos = Position::new(0, 0, 0);
|
2017-12-09 10:02:45 -06:00
|
|
|
assert!(formatter.render(&pos).is_err());
|
2017-08-08 21:02:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_happy_wrong_too_many_args() {
|
|
|
|
let formatter = Formatter::new("foo @ @ \\@", vec!["bar", "quux", "baz"]);
|
2018-11-05 21:34:12 -06:00
|
|
|
let pos = Position::new(0, 0, 0);
|
2017-12-09 10:02:45 -06:00
|
|
|
assert!(formatter.render(&pos).is_err());
|
2017-08-08 21:02:54 -05:00
|
|
|
}
|
|
|
|
}
|