1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use cssparser::*;
use crate::traits::{Parse, ToCss};
use crate::printer::Printer;
use crate::error::{ParserError, PrinterError};
#[derive(Debug, Clone, PartialEq)]
pub struct Ratio(pub f32, pub f32);
impl Parse for Ratio {
fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let first = f32::parse(input)?;
let second = if input.try_parse(|input| input.expect_delim('/')).is_ok() {
f32::parse(input)?
} else {
1.0
};
Ok(Ratio(first, second))
}
}
impl Ratio {
pub fn parse_required<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let first = f32::parse(input)?;
input.expect_delim('/')?;
let second = f32::parse(input)?;
Ok(Ratio(first, second))
}
}
impl ToCss for Ratio {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
self.0.to_css(dest)?;
if self.1 != 1.0 {
dest.delim('/', true)?;
self.1.to_css(dest)?;
}
Ok(())
}
}
impl std::ops::Add<f32> for Ratio {
type Output = Self;
fn add(self, other: f32) -> Ratio {
Ratio(self.0 + other, self.1)
}
}