Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
archer884 committed Aug 18, 2018
1 parent 336f875 commit fe4b099
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl App {
canvas.add_annotation(
&scaled_annotation.annotation,
&font,
scaled_annotation.scale_multiplier
scaled_annotation.scale_multiplier,
);
}

Expand Down
16 changes: 12 additions & 4 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,27 @@ fn read_command() -> Result<Options> {
let parser = ScaledAnnotationParser::new();

if let Some(caption) = matches.value_of("caption") {
options.annotations.push(parser.bottom(options.scale_mult, caption));
options
.annotations
.push(parser.bottom(options.scale_mult, caption));
}

if let Some(caption) = matches.value_of("top") {
options.annotations.push(parser.top(options.scale_mult, caption));
options
.annotations
.push(parser.top(options.scale_mult, caption));
}

if let Some(caption) = matches.value_of("middle") {
options.annotations.push(parser.middle(options.scale_mult, caption));
options
.annotations
.push(parser.middle(options.scale_mult, caption));
}

if let Some(caption) = matches.value_of("bottom") {
options.annotations.push(parser.bottom(options.scale_mult, caption));
options
.annotations
.push(parser.bottom(options.scale_mult, caption));
}

options.output_format = {
Expand Down
38 changes: 29 additions & 9 deletions src/config/scaled_annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub struct ScaledAnnotation {

impl ScaledAnnotation {
fn new(scale_multiplier: f32, annotation: Annotation) -> Self {
Self { scale_multiplier, annotation }
Self {
scale_multiplier,
annotation,
}
}
}

Expand All @@ -20,29 +23,38 @@ pub struct ScaledAnnotationParser {
impl ScaledAnnotationParser {
pub fn new() -> Self {
Self {
pattern: Regex::new(r#"\\(?P<scale>\d+(\.\d+)?)\s+(?P<caption>.+)"#).unwrap()
pattern: Regex::new(r#"\\(?P<scale>\d+(\.\d+)?)\s+(?P<caption>.+)"#).unwrap(),
}
}

pub fn bottom(&self, scale: f32, s: &str) -> ScaledAnnotation {
let (annotation_scale, annotation) = parse_scaled_annotation(&self.pattern, s);
ScaledAnnotation::new(annotation_scale.unwrap_or(scale), Annotation::bottom(annotation))
ScaledAnnotation::new(
annotation_scale.unwrap_or(scale),
Annotation::bottom(annotation),
)
}

pub fn middle(&self, scale: f32, s: &str) -> ScaledAnnotation {
let (annotation_scale, annotation) = parse_scaled_annotation(&self.pattern, s);
ScaledAnnotation::new(annotation_scale.unwrap_or(scale), Annotation::middle(annotation))
ScaledAnnotation::new(
annotation_scale.unwrap_or(scale),
Annotation::middle(annotation),
)
}

pub fn top(&self, scale: f32, s: &str) -> ScaledAnnotation {
let (annotation_scale, annotation) = parse_scaled_annotation(&self.pattern, s);
ScaledAnnotation::new(annotation_scale.unwrap_or(scale), Annotation::top(annotation))
ScaledAnnotation::new(
annotation_scale.unwrap_or(scale),
Annotation::top(annotation),
)
}
}

fn parse_scaled_annotation(pattern: &Regex, s: &str) -> (Option<f32>, String) {
// The plan here is to provide in-band scaling per-annotation via the following format: we
// check the beginning of any given annotation for \<float>. If we find that, we treat it
// check the beginning of any given annotation for \<float>. If we find that, we treat it
// as entirely separate from the annotation, up to and including all trailing white space.
//
// For instance, an annotation of the form `\1.2 frenchmen can't spell` would equate to
Expand All @@ -61,9 +73,14 @@ fn parse_scaled_annotation(pattern: &Regex, s: &str) -> (Option<f32>, String) {
// These unwrap assumptions may look horrendously unsafe, but the design of the regular
// expression itself makes failure here very unlikely.
Some(captures) => (
Some(captures.name("scale").and_then(|s| s.as_str().parse().ok()).unwrap()),
Some(
captures
.name("scale")
.and_then(|s| s.as_str().parse().ok())
.unwrap(),
),
captures.name("caption").map(|s| s.as_str().into()).unwrap(),
)
),
}
}

Expand All @@ -75,7 +92,10 @@ mod tests {
fn scaled_annotation_parser_works() {
let parser = ScaledAnnotationParser::new();
let caption = "\\2.0 Hello, world!";
let ScaledAnnotation { scale_multiplier, annotation } = parser.top(1.0, caption);
let ScaledAnnotation {
scale_multiplier,
annotation,
} = parser.top(1.0, caption);

assert_eq!(2.0, scale_multiplier);
assert_eq!("Hello, world!", annotation.text);
Expand Down

0 comments on commit fe4b099

Please sign in to comment.