use crate::parser::{Parse, ParserContext};
use crate::values::generics::size::Size2D;
use crate::values::specified::length::NonNegativeLength;
use crate::values::{generics, CustomIdent};
use cssparser::Parser;
use style_traits::ParseError;
pub use generics::page::PageOrientation;
pub use generics::page::PageSizeOrientation;
pub use generics::page::PaperSize;
pub type PageSize = generics::page::PageSize<Size2D<NonNegativeLength>>;
impl Parse for PageSize {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if let Ok(paper_size) = input.try_parse(PaperSize::parse) {
let orientation = input
.try_parse(PageSizeOrientation::parse)
.unwrap_or(PageSizeOrientation::Portrait);
return Ok(PageSize::PaperSize(paper_size, orientation));
}
if let Ok(orientation) = input.try_parse(PageSizeOrientation::parse) {
if let Ok(paper_size) = input.try_parse(PaperSize::parse) {
return Ok(PageSize::PaperSize(paper_size, orientation));
}
return Ok(PageSize::Orientation(orientation));
}
if let Ok(size) =
input.try_parse(|i| Size2D::parse_with(context, i, NonNegativeLength::parse))
{
return Ok(PageSize::Size(size));
}
input.expect_ident_matching("auto")?;
Ok(PageSize::Auto)
}
}
#[derive(
Clone,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToCss,
ToComputedValue,
ToResolvedValue,
ToShmem,
)]
#[repr(C, u8)]
pub enum PageName {
Auto,
PageName(CustomIdent),
}
impl Parse for PageName {
fn parse<'i, 't>(
_context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let location = input.current_source_location();
let ident = input.expect_ident()?;
Ok(match_ignore_ascii_case! { ident,
"auto" => PageName::auto(),
_ => PageName::PageName(CustomIdent::from_ident(location, ident, &[])?),
})
}
}
impl PageName {
#[inline]
pub fn auto() -> Self {
PageName::Auto
}
#[inline]
pub fn is_auto(&self) -> bool {
matches!(*self, PageName::Auto)
}
}