Skip to main content

Section

Struct Section 

Source
pub struct Section { /* private fields */ }
Expand description

One run of rows under a heading of its own.

Columns belong to a section rather than to the view, so two sections can differ: a section of what is overdue wants a column of how late, and a section of what is merely out does not. Sections that should line up are given the same columns.

Implementations§

Source§

impl Section

Source

pub fn new(columns: impl IntoIterator<Item = Column>) -> Self

Examples found in repository?
examples/library/main.rs (line 634)
589    fn render(&self, args: &ViewArgs, ctx: &Context) -> Result<ViewData, ApiError> {
590        let books: Vec<Book> = ctx.optional_rows(BOOKS_FILE)?;
591        let branches = Self::branches(ctx)?;
592        let code = args.get_or("branch", "");
593        let branch = branches.iter().find(|b| b.code == code);
594        let named = branch.map(|b| b.name.as_str()).unwrap_or(code);
595
596        let genre = args.get_or("genre", "");
597        let subgenre = args.get_or("subgenre", "");
598        let author = args.get_or("author", "").trim().to_lowercase();
599
600        // The books this branch holds that answer the rest of the question,
601        // whether they are out or not. A parameter left empty asks nothing.
602        let held: Vec<&Book> = books
603            .iter()
604            .filter(|b| b.shelved.contains_key(code))
605            .filter(|b| genre.is_empty() || b.genre == genre)
606            .filter(|b| subgenre.is_empty() || b.subgenre == subgenre)
607            .filter(|b| {
608                author.is_empty()
609                    || format!("{} {}", b.author_first, b.author_last)
610                        .to_lowercase()
611                        .contains(&author)
612            })
613            .collect();
614
615        let now = today();
616        let mut out = Vec::new();
617        let mut overdue = Vec::new();
618        for book in held.iter().filter(|b| b.lent.unwrap_or(false)) {
619            let days = days_from_civil(&book.due).map(|due| now - due).unwrap_or(0);
620            if days > 0 {
621                overdue.push(OnLoan::row(book, days));
622            } else {
623                out.push(OnLoan::row(book, days));
624            }
625        }
626
627        let lent = out.len() + overdue.len();
628        Ok(ViewData::new()
629            .note(format!(
630                "{lent} of {} book(s) at {named} are out on loan. A title links to its catalogue entry.",
631                held.len()
632            ))
633            .section(
634                Section::new(OnLoan::columns())
635                    .heading("Out")
636                    .note("Days left before they are due.")
637                    .rows(out)?,
638            )
639            .section(
640                Section::new(OnLoan::columns())
641                    .heading("Overdue")
642                    .note("Days past due. Chase these.")
643                    .rows(overdue)?,
644            ))
645    }
Source

pub fn heading(self, heading: impl Into<String>) -> Self

Examples found in repository?
examples/library/main.rs (line 635)
589    fn render(&self, args: &ViewArgs, ctx: &Context) -> Result<ViewData, ApiError> {
590        let books: Vec<Book> = ctx.optional_rows(BOOKS_FILE)?;
591        let branches = Self::branches(ctx)?;
592        let code = args.get_or("branch", "");
593        let branch = branches.iter().find(|b| b.code == code);
594        let named = branch.map(|b| b.name.as_str()).unwrap_or(code);
595
596        let genre = args.get_or("genre", "");
597        let subgenre = args.get_or("subgenre", "");
598        let author = args.get_or("author", "").trim().to_lowercase();
599
600        // The books this branch holds that answer the rest of the question,
601        // whether they are out or not. A parameter left empty asks nothing.
602        let held: Vec<&Book> = books
603            .iter()
604            .filter(|b| b.shelved.contains_key(code))
605            .filter(|b| genre.is_empty() || b.genre == genre)
606            .filter(|b| subgenre.is_empty() || b.subgenre == subgenre)
607            .filter(|b| {
608                author.is_empty()
609                    || format!("{} {}", b.author_first, b.author_last)
610                        .to_lowercase()
611                        .contains(&author)
612            })
613            .collect();
614
615        let now = today();
616        let mut out = Vec::new();
617        let mut overdue = Vec::new();
618        for book in held.iter().filter(|b| b.lent.unwrap_or(false)) {
619            let days = days_from_civil(&book.due).map(|due| now - due).unwrap_or(0);
620            if days > 0 {
621                overdue.push(OnLoan::row(book, days));
622            } else {
623                out.push(OnLoan::row(book, days));
624            }
625        }
626
627        let lent = out.len() + overdue.len();
628        Ok(ViewData::new()
629            .note(format!(
630                "{lent} of {} book(s) at {named} are out on loan. A title links to its catalogue entry.",
631                held.len()
632            ))
633            .section(
634                Section::new(OnLoan::columns())
635                    .heading("Out")
636                    .note("Days left before they are due.")
637                    .rows(out)?,
638            )
639            .section(
640                Section::new(OnLoan::columns())
641                    .heading("Overdue")
642                    .note("Days past due. Chase these.")
643                    .rows(overdue)?,
644            ))
645    }
Source

pub fn note(self, note: impl Into<String>) -> Self

Examples found in repository?
examples/library/main.rs (line 636)
589    fn render(&self, args: &ViewArgs, ctx: &Context) -> Result<ViewData, ApiError> {
590        let books: Vec<Book> = ctx.optional_rows(BOOKS_FILE)?;
591        let branches = Self::branches(ctx)?;
592        let code = args.get_or("branch", "");
593        let branch = branches.iter().find(|b| b.code == code);
594        let named = branch.map(|b| b.name.as_str()).unwrap_or(code);
595
596        let genre = args.get_or("genre", "");
597        let subgenre = args.get_or("subgenre", "");
598        let author = args.get_or("author", "").trim().to_lowercase();
599
600        // The books this branch holds that answer the rest of the question,
601        // whether they are out or not. A parameter left empty asks nothing.
602        let held: Vec<&Book> = books
603            .iter()
604            .filter(|b| b.shelved.contains_key(code))
605            .filter(|b| genre.is_empty() || b.genre == genre)
606            .filter(|b| subgenre.is_empty() || b.subgenre == subgenre)
607            .filter(|b| {
608                author.is_empty()
609                    || format!("{} {}", b.author_first, b.author_last)
610                        .to_lowercase()
611                        .contains(&author)
612            })
613            .collect();
614
615        let now = today();
616        let mut out = Vec::new();
617        let mut overdue = Vec::new();
618        for book in held.iter().filter(|b| b.lent.unwrap_or(false)) {
619            let days = days_from_civil(&book.due).map(|due| now - due).unwrap_or(0);
620            if days > 0 {
621                overdue.push(OnLoan::row(book, days));
622            } else {
623                out.push(OnLoan::row(book, days));
624            }
625        }
626
627        let lent = out.len() + overdue.len();
628        Ok(ViewData::new()
629            .note(format!(
630                "{lent} of {} book(s) at {named} are out on loan. A title links to its catalogue entry.",
631                held.len()
632            ))
633            .section(
634                Section::new(OnLoan::columns())
635                    .heading("Out")
636                    .note("Days left before they are due.")
637                    .rows(out)?,
638            )
639            .section(
640                Section::new(OnLoan::columns())
641                    .heading("Overdue")
642                    .note("Days past due. Chase these.")
643                    .rows(overdue)?,
644            ))
645    }
Source

pub fn rows<T: Serialize>( self, rows: impl IntoIterator<Item = T>, ) -> Result<Self, ApiError>

The rows themselves, which may be a repository’s own types: whatever serializes to an object keyed by the fields the columns name.

A row that cannot be serialized is a 500 naming the section, since a page that quietly dropped a row would be worse than one that did not render.

Examples found in repository?
examples/library/main.rs (line 637)
589    fn render(&self, args: &ViewArgs, ctx: &Context) -> Result<ViewData, ApiError> {
590        let books: Vec<Book> = ctx.optional_rows(BOOKS_FILE)?;
591        let branches = Self::branches(ctx)?;
592        let code = args.get_or("branch", "");
593        let branch = branches.iter().find(|b| b.code == code);
594        let named = branch.map(|b| b.name.as_str()).unwrap_or(code);
595
596        let genre = args.get_or("genre", "");
597        let subgenre = args.get_or("subgenre", "");
598        let author = args.get_or("author", "").trim().to_lowercase();
599
600        // The books this branch holds that answer the rest of the question,
601        // whether they are out or not. A parameter left empty asks nothing.
602        let held: Vec<&Book> = books
603            .iter()
604            .filter(|b| b.shelved.contains_key(code))
605            .filter(|b| genre.is_empty() || b.genre == genre)
606            .filter(|b| subgenre.is_empty() || b.subgenre == subgenre)
607            .filter(|b| {
608                author.is_empty()
609                    || format!("{} {}", b.author_first, b.author_last)
610                        .to_lowercase()
611                        .contains(&author)
612            })
613            .collect();
614
615        let now = today();
616        let mut out = Vec::new();
617        let mut overdue = Vec::new();
618        for book in held.iter().filter(|b| b.lent.unwrap_or(false)) {
619            let days = days_from_civil(&book.due).map(|due| now - due).unwrap_or(0);
620            if days > 0 {
621                overdue.push(OnLoan::row(book, days));
622            } else {
623                out.push(OnLoan::row(book, days));
624            }
625        }
626
627        let lent = out.len() + overdue.len();
628        Ok(ViewData::new()
629            .note(format!(
630                "{lent} of {} book(s) at {named} are out on loan. A title links to its catalogue entry.",
631                held.len()
632            ))
633            .section(
634                Section::new(OnLoan::columns())
635                    .heading("Out")
636                    .note("Days left before they are due.")
637                    .rows(out)?,
638            )
639            .section(
640                Section::new(OnLoan::columns())
641                    .heading("Overdue")
642                    .note("Days past due. Chase these.")
643                    .rows(overdue)?,
644            ))
645    }

Trait Implementations§

Source§

impl Clone for Section

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Section

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Section

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.