[][src]Struct bloguen::ops::StyleElement

pub struct StyleElement { /* fields omitted */ }

A style specifier.

Can be a link or a literal, and a literal can be indirectly loaded from a file.

Consult the documentation for load() on handling filesystem interaxion.

Deserialisation

There are two serialised forms, a verbose one:

#[derive(Deserialize, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct StyleContainer {
    pub style: Vec<StyleElement>,
}

let style_toml =
    "[[style]]
     class = 'link'
     data = '//nabijaczleweli.xyz/kaschism/assets/column.css'

     [[style]]
     class = 'literal'
     data = '.indented { text-indent: 1em; }'

     [[style]]
     class = 'file'
     data = 'common.css'";

let StyleContainer { style } = toml::from_str(style_toml).unwrap();
assert_eq!(&style,
           &[StyleElement::from_link("//nabijaczleweli.xyz/kaschism/assets/column.css"),
             StyleElement::from_literal(".indented { text-indent: 1em; }"),
             StyleElement::from_path("common.css")]);

And a compact one (the "literal" tag may be omitted if the content doesn't contain any colons):

#[derive(Deserialize, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
struct StyleContainer {
    pub styles: Vec<StyleElement>,
}

let styles_toml =
    "styles = [
         'link://nabijaczleweli.xyz/kaschism/assets/column.css',
         'literal:.indented { text-indent: 1em; }',
         'file:common.css',
     ]";

let StyleContainer { styles } = toml::from_str(styles_toml).unwrap();
assert_eq!(&styles,
           &[StyleElement::from_link("//nabijaczleweli.xyz/kaschism/assets/column.css"),
             StyleElement::from_literal(".indented { text-indent: 1em; }"),
             StyleElement::from_path("common.css")]);

Methods

impl StyleElement[src]

Create a style element linking to an external resource.

Examples

let lonk = StyleElement::from_link("//nabijaczleweli.xyz/kaschism/assets/column.css");
assert_eq!(
    format!("{}{}{}", lonk.head(), lonk.content(), lonk.foot()),
    "<link href=\"//nabijaczleweli.xyz/kaschism/assets/column.css\" rel=\"stylesheet\" />\n")

pub fn from_literal<Dt: Into<Cow<'static, str>>>(literal: Dt) -> StyleElement[src]

Create a style element including the specified literal literally.

Examples

let lit = StyleElement::from_literal(".indented { text-indent: 1em; }");
assert_eq!(
    format!("{}{}{}", lit.head(), lit.content(), lit.foot()),
    "<style type=\"text/css\">\n\n.indented { text-indent: 1em; }\n\n</style>\n")

pub fn from_path<Dt: Into<Cow<'static, str>>>(path: Dt) -> StyleElement[src]

Create a style element pointing to the specified relative path.

Consult load() documentation for more data.

Examples

Given $ROOT/common.css containing:

ul, ol {
    margin-top: 0;
    margin-bottom: 0;
}

a > i.fa {
    color: black;
}

The following holds:

let root: PathBuf = /* obtained elsewhere */;

let mut lit_p = StyleElement::from_path("common.css");
assert_eq!(lit_p.load(&("$ROOT".to_string(), root.clone())), Ok(()));
assert_eq!(format!("{}{}{}", lit_p.head(), lit_p.content(), lit_p.foot()),
"<style type=\"text/css\">\n\n\
     ul, ol {\n\
         margin-top: 0;\n\
         margin-bottom: 0;\n\
     }\n\
     \n\
     a > i.fa {\n\
         color: black;\n\
     }\n\n\
\n\n</style>\n");

pub fn from_file(path: &(String, PathBuf)) -> Result<StyleElement, Error>[src]

Create a literal style element from the contents of the specified file.

Examples

Given $ROOT/common.css containing:

ul, ol {
    margin-top: 0;
    margin-bottom: 0;
}

a > i.fa {
    color: black;
}

The following holds:

let root: PathBuf = /* obtained elsewhere */;

let lit_p = StyleElement::from_file(&("$ROOT/common.css".to_string(), root.join("common.css"))).unwrap();
assert_eq!(format!("{}{}{}", lit_p.head(), lit_p.content(), lit_p.foot()),
"<style type=\"text/css\">\n\n\
     ul, ol {\n\
         margin-top: 0;\n\
         margin-bottom: 0;\n\
     }\n\
     \n\
     a > i.fa {\n\
         color: black;\n\
     }\n\n\
\n\n</style>\n");

pub fn load(&mut self, base: &(String, PathBuf)) -> Result<(), Error>[src]

Read data from the filesystem, if appropriate.

Path elements are concatenated with the specified root, then read_file()d in, becoming literals.

Non-path elements are unaffected.

Examples

Given the following directory layout:

$ROOT
  common.css
  assets
    effects.css

Given $ROOT/common.css containing:

ul, ol {
    margin-top: 0;
    margin-bottom: 0;
}

a > i.fa {
    color: black;
}

Given $ROOT/assets/effects.css containing:

.ruby {
    /* That's Ruby according to https://en.wikipedia.org/wiki/Ruby_(color). */
    color: #E0115F;
}

The following holds:

let root: PathBuf = /* obtained elsewhere */;

let mut elem = StyleElement::from_path("common.css");
assert_eq!(elem.load(&("$ROOT".to_string(), root.clone())), Ok(()));
assert_eq!(elem, StyleElement::from_literal("\
    ul, ol {\n\
        margin-top: 0;\n\
        margin-bottom: 0;\n\
    }\n\
    \n\
    a > i.fa {\n\
        color: black;\n\
    }\n
"));

let mut elem = StyleElement::from_path("assets/.././assets/effects.css");
assert_eq!(elem.load(&("$ROOT".to_string(), root.clone())), Ok(()));
assert_eq!(elem, StyleElement::from_literal("\
   .ruby {\n\
        /* That's Ruby according to https://en.wikipedia.org/wiki/Ruby_(color). */\n\
        color: #E0115F;\n\
    }\n
"));

let mut elem = StyleElement::from_path("assets/nonexistant.css");
assert_eq!(elem.load(&("$ROOT".to_string(), root.clone())), Err(Error::FileNotFound {
    who: "file style element",
    path: "$ROOT/assets/nonexistant.css".into(),
}));
assert_eq!(elem, StyleElement::from_path("assets/nonexistant.css"));

Trait Implementations

impl WrappedElement for StyleElement[src]

fn head_b(&self) -> &[u8][src]

Byte representation of pre-content.

fn content_b(&self) -> &[u8][src]

Byte representation of the content.

fn foot_b(&self) -> &[u8][src]

Byte representation of post-content.

impl Eq for StyleElement[src]

impl Clone for StyleElement[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl PartialOrd<StyleElement> for StyleElement[src]

impl PartialEq<StyleElement> for StyleElement[src]

impl Ord for StyleElement[src]

fn max(self, other: Self) -> Self1.21.0[src]

Compares and returns the maximum of two values. Read more

fn min(self, other: Self) -> Self1.21.0[src]

Compares and returns the minimum of two values. Read more

fn clamp(self, min: Self, max: Self) -> Self[src]

🔬 This is a nightly-only experimental API. (clamp)

Restrict a value to a certain interval. Read more

impl Hash for StyleElement[src]

fn hash_slice<H>(data: &[Self], state: &mut H) where
    H: Hasher
1.3.0[src]

Feeds a slice of this type into the given [Hasher]. Read more

impl Debug for StyleElement[src]

impl<'de> Deserialize<'de> for StyleElement[src]

Auto Trait Implementations

impl Unpin for StyleElement

impl Sync for StyleElement

impl Send for StyleElement

impl UnwindSafe for StyleElement

impl RefUnwindSafe for StyleElement

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 
[src]