style: Have OneOrMoreSeparated replace OneOrMoreCommaSeparated.

A future patch series has some values that should be separated by spaces. This
allows us to re-use the code for serialization, but the types do get a little
clunky. The separator is now indicated with an associated type.
This commit is contained in:
Jonathan Chan 2017-06-19 15:35:58 -07:00
parent e41b7d06b4
commit 26179b3660
6 changed files with 64 additions and 14 deletions

View file

@ -19,7 +19,7 @@ use std::ascii::AsciiExt;
use std::borrow::Cow;
use std::fmt;
use std::ops::Range;
use style_traits::{ToCss, OneOrMoreCommaSeparated, ParseError, StyleParseError};
use style_traits::{ToCss, OneOrMoreSeparated, CommaSeparator, ParseError, StyleParseError};
use values::CustomIdent;
/// Parse the prelude of an @counter-style rule
@ -552,7 +552,9 @@ pub struct AdditiveTuple {
pub symbol: Symbol,
}
impl OneOrMoreCommaSeparated for AdditiveTuple {}
impl OneOrMoreSeparated for AdditiveTuple {
type S = CommaSeparator;
}
impl Parse for AdditiveTuple {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {

View file

@ -20,7 +20,7 @@ use parser::{ParserContext, log_css_error, Parse};
use selectors::parser::SelectorParseError;
use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::{ToCss, OneOrMoreCommaSeparated, ParseError, StyleParseError};
use style_traits::{ToCss, OneOrMoreSeparated, CommaSeparator, ParseError, StyleParseError};
use values::specified::url::SpecifiedUrl;
/// A source for a font-face rule.
@ -34,7 +34,9 @@ pub enum Source {
Local(FamilyName),
}
impl OneOrMoreCommaSeparated for Source {}
impl OneOrMoreSeparated for Source {
type S = CommaSeparator;
}
/// A `UrlSource` represents a font-face source that has been specified with a
/// `url()` function.

View file

@ -7,7 +7,7 @@
use context::QuirksMode;
use cssparser::{Parser, SourcePosition, UnicodeRange};
use error_reporting::{ParseErrorReporter, ContextualParseError};
use style_traits::{OneOrMoreCommaSeparated, ParseError, ParsingMode};
use style_traits::{OneOrMoreSeparated, IsCommaSeparator, ParseError, ParsingMode};
#[cfg(feature = "gecko")]
use style_traits::{PARSING_MODE_DEFAULT, PARSING_MODE_ALLOW_UNITLESS_LENGTH, PARSING_MODE_ALLOW_ALL_NUMERIC_VALUES};
use stylesheets::{CssRuleType, Origin, UrlExtraData, Namespaces};
@ -161,7 +161,9 @@ pub trait Parse : Sized {
-> Result<Self, ParseError<'i>>;
}
impl<T> Parse for Vec<T> where T: Parse + OneOrMoreCommaSeparated {
impl<T> Parse for Vec<T> where T: Parse + OneOrMoreSeparated,
<T as OneOrMoreSeparated>::S: IsCommaSeparator
{
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Self, ParseError<'i>> {
input.parse_comma_separated(|input| T::parse(context, input))

View file

@ -9,7 +9,7 @@ use counter_style::{Symbols, parse_counter_style_name};
use cssparser::Parser;
use parser::{Parse, ParserContext};
use std::fmt;
use style_traits::{OneOrMoreCommaSeparated, ToCss, ParseError, StyleParseError};
use style_traits::{OneOrMoreSeparated, CommaSeparator, ToCss, ParseError, StyleParseError};
use super::CustomIdent;
use values::specified::url::SpecifiedUrl;
@ -123,7 +123,9 @@ pub struct FontSettingTag<T> {
pub value: T,
}
impl<T> OneOrMoreCommaSeparated for FontSettingTag<T> {}
impl<T> OneOrMoreSeparated for FontSettingTag<T> {
type S = CommaSeparator;
}
impl<T: ToCss> ToCss for FontSettingTag<T> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {

View file

@ -71,7 +71,7 @@ pub mod values;
#[macro_use]
pub mod viewport;
pub use values::{ToCss, OneOrMoreCommaSeparated};
pub use values::{ToCss, OneOrMoreSeparated, CommaSeparator, SpaceSeparator, IsCommaSeparator};
pub use viewport::HasViewportPercentage;
/// The error type for all CSS parsing routines.

View file

@ -55,17 +55,59 @@ impl ToCss for String {
}
}
/// Marker trait to automatically implement ToCss for Vec<T>.
pub trait OneOrMoreCommaSeparated {}
/// Type used as the associated type in the `OneOrMoreSeparated` trait on a
/// type to indicate that a serialized list of elements of this type is
/// separated by commas.
pub struct CommaSeparator;
impl OneOrMoreCommaSeparated for UnicodeRange {}
/// Type used as the associated type in the `OneOrMoreSeparated` trait on a
/// type to indicate that a serialized list of elements of this type is
/// separated by spaces.
pub struct SpaceSeparator;
impl<T> ToCss for Vec<T> where T: ToCss + OneOrMoreCommaSeparated {
/// A trait satisfied by the types corresponding to separators.
pub trait Separator {
/// The separator string that the satisfying separator type corresponds to.
fn separator() -> &'static str;
}
impl Separator for CommaSeparator {
fn separator() -> &'static str {
", "
}
}
impl Separator for SpaceSeparator {
fn separator() -> &'static str {
" "
}
}
/// Trait that indicates that satisfying separator types are comma separators.
/// This seems kind of redundant, but we aren't able to express type equality
/// constraints yet.
/// https://github.com/rust-lang/rust/issues/20041
pub trait IsCommaSeparator {}
impl IsCommaSeparator for CommaSeparator {}
/// Marker trait on T to automatically implement ToCss for Vec<T> when T's are
/// separated by some delimiter `delim`.
pub trait OneOrMoreSeparated {
/// Associated type indicating which separator is used.
type S: Separator;
}
impl OneOrMoreSeparated for UnicodeRange {
type S = CommaSeparator;
}
impl<T> ToCss for Vec<T> where T: ToCss + OneOrMoreSeparated {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
let mut iter = self.iter();
iter.next().unwrap().to_css(dest)?;
for item in iter {
dest.write_str(", ")?;
dest.write_str(<T as OneOrMoreSeparated>::S::separator())?;
item.to_css(dest)?;
}
Ok(())