~ireas/public-inbox

genpdf-rs: Forgot to run cargo fmt v2 PROPOSED

~frankenapps
This is a patch on top of the one I sent earlier, because I forgot to
run rustfmt.

Sorry if I did this wrong, but this is my first time
using sourcehut and I am a bit confused by the process of sending
patches upstream.

Ferdinand Schäffler (2):
  Implement horizontal direction for LinearLayout.
  Make cargo fmt happy.

 src/elements.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++----
 src/lib.rs      | 11 +++++++++++
 2 files changed, 55 insertions(+), 4 deletions(-)

-- 
2.30.2
#525461 archlinux-msrv.yml success
#525462 archlinux.yml success
builds.sr.ht
genpdf-rs/patches: SUCCESS in 7m0s

[Forgot to run cargo fmt][0] v2 from [~frankenapps][1]

[0]: https://lists.sr.ht/~ireas/public-inbox/patches/23307
[1]: mailto:de.frankenapps@gmail.com

✓ #525461 SUCCESS genpdf-rs/patches/archlinux-msrv.yml https://builds.sr.ht/~ireas/job/525461
✓ #525462 SUCCESS genpdf-rs/patches/archlinux.yml      https://builds.sr.ht/~ireas/job/525462
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~ireas/public-inbox/patches/23307/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH genpdf-rs v2 1/2] Implement horizontal direction for LinearLayout. Export this patch

~frankenapps
From: Ferdinand Schäffler <de.frankenapps@gmail.com>

---
 src/elements.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++----
 src/lib.rs      | 11 +++++++++++
 2 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/src/elements.rs b/src/elements.rs
index 1ff2dea..14fbea7 100644
--- a/src/elements.rs
+++ b/src/elements.rs
@@ -56,6 +56,15 @@ use crate::{Alignment, Context, Element, Margins, Mm, Position, RenderResult, Si
#[cfg(feature = "images")]
pub use images::Image;

/// Defines the direction for [LinearLayout].
enum LinearLayoutDirection {
    /// Arrange the elements horzontally.
    Horizontal,

    /// Arrange the elements vertically.
    Vertical,
}

/// Arranges a list of elements sequentially.
///
/// Currently, elements can only be arranged vertically.
@@ -81,19 +90,26 @@ pub use images::Image;
pub struct LinearLayout {
    elements: Vec<Box<dyn Element>>,
    render_idx: usize,
    direction: LinearLayoutDirection,
}

impl LinearLayout {
    fn new() -> LinearLayout {
    fn new(direction: LinearLayoutDirection) -> LinearLayout {
        LinearLayout {
            elements: Vec::new(),
            render_idx: 0,
            direction
        }
    }

    /// Creates a new linear layout that arranges its elements vertically.
    pub fn vertical() -> LinearLayout {
        LinearLayout::new()
        LinearLayout::new(LinearLayoutDirection::Vertical)
    }

    /// Creates a new linear layout that arranges its elements horizontally.
    pub fn horizontal() -> LinearLayout {
        LinearLayout::new(LinearLayoutDirection::Horizontal)
    }

    /// Adds the given element to this layout.
@@ -128,6 +144,28 @@ impl LinearLayout {
        result.has_more = self.render_idx < self.elements.len();
        Ok(result)
    }

    fn render_horizontal(
        &mut self,
        context: &Context,
        mut area: render::Area<'_>,
        style: Style,
    ) -> Result<RenderResult, Error> {
        let mut result = RenderResult::default();
        while area.size().width > Mm(0.0) && self.render_idx < self.elements.len() {
            let element_result =
                self.elements[self.render_idx].render(context, area.clone(), style)?;
            area.add_offset(Position::new(element_result.size.width, 0));
            result.size = result.size.stack_horizontal(element_result.size);
            if element_result.has_more {
                result.has_more = true;
                return Ok(result);
            }
            self.render_idx += 1;
        }
        result.has_more = self.render_idx < self.elements.len();
        Ok(result)
    }
}

impl Element for LinearLayout {
@@ -137,8 +175,10 @@ impl Element for LinearLayout {
        area: render::Area<'_>,
        style: Style,
    ) -> Result<RenderResult, Error> {
        // TODO: add horizontal layout
        self.render_vertical(context, area, style)
        match self.direction {
            LinearLayoutDirection::Horizontal => self.render_horizontal(context, area, style),
            LinearLayoutDirection::Vertical => self.render_vertical(context, area, style),
        }
    }
}

diff --git a/src/lib.rs b/src/lib.rs
index 1c6d81f..ae6771c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -435,6 +435,17 @@ impl Size {
        self.height += other.height;
        self
    }

    /// Stacks the given size horizontally on this size and returns the result.
    ///
    /// This means that the height is set to the maximum of the heights and the width is set to the
    /// sum of the widths.
    #[must_use]
    pub fn stack_horizontal(mut self, other: Size) -> Size {
        self.width += other.width;
        self.height = self.height.max(other.height);
        self
    }
}

impl<W: Into<Mm>, H: Into<Mm>> From<(W, H)> for Size {
-- 
2.30.2

[PATCH genpdf-rs v2 2/2] Make cargo fmt happy. Export this patch

~frankenapps
From: Ferdinand Schäffler <de.frankenapps@gmail.com>

---
 src/elements.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/elements.rs b/src/elements.rs
index 14fbea7..199ad08 100644
--- a/src/elements.rs
+++ b/src/elements.rs
@@ -98,7 +98,7 @@ impl LinearLayout {
        LinearLayout {
            elements: Vec::new(),
            render_idx: 0,
            direction
            direction,
        }
    }

-- 
2.30.2
builds.sr.ht
genpdf-rs/patches: SUCCESS in 7m0s

[Forgot to run cargo fmt][0] v2 from [~frankenapps][1]

[0]: https://lists.sr.ht/~ireas/public-inbox/patches/23307
[1]: mailto:de.frankenapps@gmail.com

✓ #525461 SUCCESS genpdf-rs/patches/archlinux-msrv.yml https://builds.sr.ht/~ireas/job/525461
✓ #525462 SUCCESS genpdf-rs/patches/archlinux.yml      https://builds.sr.ht/~ireas/job/525462