Skip to content

Commit

Permalink
image: refactor API return types
Browse files Browse the repository at this point in the history
Changed API from "&mut self" to "mut self" to allow method
chaining.
  • Loading branch information
jmcnamara committed Jun 17, 2024
1 parent 75567c3 commit 9029a10
Show file tree
Hide file tree
Showing 102 changed files with 209 additions and 341 deletions.
6 changes: 3 additions & 3 deletions examples/app_headers_footers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ fn main() -> Result<(), XlsxError> {
worksheet3.set_view_page_layout();
worksheet3.write_string(0, 0, "Some text")?;

let mut image = Image::new("examples/rust_logo.png")?;
image.set_scale_height(0.5);
image.set_scale_width(0.5);
let image = Image::new("examples/rust_logo.png")?
.set_scale_height(0.5)
.set_scale_width(0.5);

worksheet3.set_header("&L&[Picture]");
worksheet3.set_header_image(&image, HeaderImagePosition::Left)?;
Expand Down
2 changes: 1 addition & 1 deletion examples/app_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() -> Result<(), XlsxError> {

// Insert an image with scaling.
worksheet.write_string(15, 0, "Insert a scaled image:")?;
image.set_scale_width(0.75).set_scale_height(0.75);
image = image.set_scale_width(0.75).set_scale_height(0.75);
worksheet.insert_image(15, 1, &image)?;

// Save the file to disk.
Expand Down
11 changes: 4 additions & 7 deletions examples/doc_image_set_alt_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ fn main() -> Result<(), XlsxError> {
// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();

// Create a new image object.
let mut image = Image::new("examples/rust_logo.png")?;

// Set the alternative text.
image.set_alt_text(
// Create a new image object and set the alternative text.
let image = Image::new("examples/rust_logo.png")?.set_alt_text(
"A circular logo with gear teeth on the outside \
and a large letter R on the inside.\n\n\
The logo of the Rust programming language.",
and a large letter R on the inside.\n\n\
The logo of the Rust programming language.",
);

// Insert the image.
Expand Down
4 changes: 1 addition & 3 deletions examples/doc_image_set_decorative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ fn main() -> Result<(), XlsxError> {
let worksheet = workbook.add_worksheet();

// Create a new image object.
let mut image = Image::new("examples/rust_logo.png")?;

image.set_decorative(true);
let image = Image::new("examples/rust_logo.png")?.set_decorative(true);

// Insert the image.
worksheet.insert_image(1, 2, &image)?;
Expand Down
8 changes: 3 additions & 5 deletions examples/doc_image_set_object_movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ fn main() -> Result<(), XlsxError> {
// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();

// Create a new image object.
let mut image = Image::new("examples/rust_logo.png")?;

// Set the object movement/positioning options.
image.set_object_movement(ObjectMovement::MoveButDontSizeWithCells);
// Create a new image and set the object movement/positioning options.
let image = Image::new("examples/rust_logo.png")?
.set_object_movement(ObjectMovement::MoveButDontSizeWithCells);

// Insert the image.
worksheet.insert_image(1, 2, &image)?;
Expand Down
4 changes: 2 additions & 2 deletions examples/doc_image_set_scale_to_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ fn main() -> Result<(), XlsxError> {
worksheet.insert_image(0, 1, &image)?;

// Scale the image to fit the entire cell.
image.set_scale_to_size(200, 140, false);
image = image.set_scale_to_size(200, 140, false);
worksheet.write_with_format(2, 0, "Image scaled to fit cell:", &center)?;
worksheet.insert_image(2, 1, &image)?;

// Scale the image to fit the defined size region while maintaining the
// aspect ratio. In this case it is scaled to the smaller of the width or
// height scales.
image.set_scale_to_size(200, 140, true);
image = image.set_scale_to_size(200, 140, true);
worksheet.write_with_format(4, 0, "Image scaled with a fixed aspect ratio:", &center)?;
worksheet.insert_image(4, 1, &image)?;

Expand Down
10 changes: 4 additions & 6 deletions examples/doc_image_set_scale_width.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ fn main() -> Result<(), XlsxError> {
// Add a worksheet to the workbook.
let worksheet = workbook.add_worksheet();

// Create a new image object.
let mut image = Image::new("examples/rust_logo.png")?;

// Set the image scale.
image.set_scale_height(0.75);
image.set_scale_width(0.75);
// Create a new image object and set the image scale.
let image = Image::new("examples/rust_logo.png")?
.set_scale_height(0.75)
.set_scale_width(0.75);

// Insert the image.
worksheet.insert_image(1, 2, &image)?;
Expand Down
6 changes: 3 additions & 3 deletions examples/doc_worksheet_set_header_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn main() -> Result<(), XlsxError> {
let worksheet = workbook.add_worksheet();

// Scale the image so it fits in the header.
let mut image = Image::new("examples/rust_logo.png")?;
image.set_scale_height(0.5);
image.set_scale_width(0.5);
let image = Image::new("examples/rust_logo.png")?
.set_scale_height(0.5)
.set_scale_width(0.5);

// Insert the watermark image in the header.
worksheet.set_header("&C&[Picture]");
Expand Down
8 changes: 4 additions & 4 deletions src/cookbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,7 @@ fn main() -> Result<(), XlsxError> {
// Insert an image with scaling.
worksheet.write_string(15, 0, "Insert a scaled image:")?;
image.set_scale_width(0.75).set_scale_height(0.75);
image = image.set_scale_width(0.75).set_scale_height(0.75);
worksheet.insert_image(15, 1, &image)?;
// Save the file to disk.
Expand Down Expand Up @@ -5886,9 +5886,9 @@ fn main() -> Result<(), XlsxError> {
worksheet3.set_view_page_layout();
worksheet3.write_string(0, 0, "Some text")?;
let mut image = Image::new("examples/rust_logo.png")?;
image.set_scale_height(0.5);
image.set_scale_width(0.5);
let image = Image::new("examples/rust_logo.png")?
.set_scale_height(0.5)
.set_scale_width(0.5);
worksheet3.set_header("&L&[Picture]");
worksheet3.set_header_image(&image, HeaderImagePosition::Left)?;
Expand Down
66 changes: 27 additions & 39 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,10 @@ impl Image {
/// # // Add a worksheet to the workbook.
/// # let worksheet = workbook.add_worksheet();
/// #
/// // Create a new image object.
/// let mut image = Image::new("examples/rust_logo.png")?;
///
/// // Set the image scale.
/// image.set_scale_height(0.75);
/// image.set_scale_width(0.75);
/// // Create a new image object and set the image scale.
/// let image = Image::new("examples/rust_logo.png")?
/// .set_scale_height(0.75)
/// .set_scale_width(0.75);
///
/// // Insert the image.
/// worksheet.insert_image(1, 2, &image)?;
Expand All @@ -327,7 +325,7 @@ impl Image {
/// <img
/// src="https://rustxlsxwriter.github.io/images/image_set_scale_width.png">
///
pub fn set_scale_height(&mut self, scale: f64) -> &mut Image {
pub fn set_scale_height(mut self, scale: f64) -> Image {
if scale <= 0.0 {
return self;
}
Expand All @@ -345,7 +343,7 @@ impl Image {
///
/// * `scale` - The scale ratio.
///
pub fn set_scale_width(&mut self, scale: f64) -> &mut Image {
pub fn set_scale_width(mut self, scale: f64) -> Image {
if scale <= 0.0 {
return self;
}
Expand Down Expand Up @@ -420,14 +418,14 @@ impl Image {
/// worksheet.insert_image(0, 1, &image)?;
///
/// // Scale the image to fit the entire cell.
/// image.set_scale_to_size(200, 140, false);
/// image = image.set_scale_to_size(200, 140, false);
/// worksheet.write_with_format(2, 0, "Image scaled to fit cell:", &center)?;
/// worksheet.insert_image(2, 1, &image)?;
///
/// // Scale the image to fit the defined size region while maintaining the
/// // aspect ratio. In this case it is scaled to the smaller of the width or
/// // height scales.
/// image.set_scale_to_size(200, 140, true);
/// image = image.set_scale_to_size(200, 140, true);
/// worksheet.write_with_format(4, 0, "Image scaled with a fixed aspect ratio:", &center)?;
/// worksheet.insert_image(4, 1, &image)?;
/// #
Expand All @@ -443,12 +441,7 @@ impl Image {
/// <img src="https://rustxlsxwriter.github.io/images/image_set_scale_to_size.png">
///
///
pub fn set_scale_to_size<T>(
&mut self,
width: T,
height: T,
keep_aspect_ratio: bool,
) -> &mut Image
pub fn set_scale_to_size<T>(mut self, width: T, height: T, keep_aspect_ratio: bool) -> Image
where
T: Into<f64> + Copy,
{
Expand All @@ -467,8 +460,8 @@ impl Image {
}
}

self.set_scale_width(scale_width);
self.set_scale_height(scale_height);
self = self.set_scale_width(scale_width);
self = self.set_scale_height(scale_height);

self
}
Expand Down Expand Up @@ -505,13 +498,12 @@ impl Image {
/// # // Add a worksheet to the workbook.
/// # let worksheet = workbook.add_worksheet();
/// #
/// // Create a new image object.
/// let mut image = Image::new("examples/rust_logo.png")?;
///
/// // Set the alternative text.
/// image.set_alt_text("A circular logo with gear teeth on the outside \
/// and a large letter R on the inside.\n\n\
/// The logo of the Rust programming language.");
/// // Create a new image object and set the alternative text.
/// let image = Image::new("examples/rust_logo.png")?.set_alt_text(
/// "A circular logo with gear teeth on the outside \
/// and a large letter R on the inside.\n\n\
/// The logo of the Rust programming language.",
/// );
///
/// # // Insert the image.
/// # worksheet.insert_image(1, 2, &image)?;
Expand All @@ -528,7 +520,7 @@ impl Image {
/// <img
/// src="https://rustxlsxwriter.github.io/images/image_set_alt_text.png">
///
pub fn set_alt_text(&mut self, alt_text: impl Into<String>) -> &mut Image {
pub fn set_alt_text(mut self, alt_text: impl Into<String>) -> Image {
self.alt_text = alt_text.into();
self
}
Expand Down Expand Up @@ -563,10 +555,8 @@ impl Image {
/// # // Add a worksheet to the workbook.
/// # let worksheet = workbook.add_worksheet();
/// #
/// # // Create a new image object.
/// let mut image = Image::new("examples/rust_logo.png")?;
///
/// image.set_decorative(true);
/// // Create a new image object.
/// let image = Image::new("examples/rust_logo.png")?.set_decorative(true);
///
/// # // Insert the image.
/// # worksheet.insert_image(1, 2, &image)?;
Expand All @@ -583,7 +573,7 @@ impl Image {
/// <img
/// src="https://rustxlsxwriter.github.io/images/image_set_decorative.png">
///
pub fn set_decorative(&mut self, enable: bool) -> &mut Image {
pub fn set_decorative(mut self, enable: bool) -> Image {
self.decorative = enable;
self
}
Expand Down Expand Up @@ -629,11 +619,9 @@ impl Image {
/// # // Add a worksheet to the workbook.
/// # let worksheet = workbook.add_worksheet();
/// #
/// // Create a new image object.
/// let mut image = Image::new("examples/rust_logo.png")?;
///
/// // Set the object movement/positioning options.
/// image.set_object_movement(ObjectMovement::MoveButDontSizeWithCells);
/// // Create a new image and set the object movement/positioning options.
/// let image = Image::new("examples/rust_logo.png")?
/// .set_object_movement(ObjectMovement::MoveButDontSizeWithCells);
///
/// // Insert the image.
/// worksheet.insert_image(1, 2, &image)?;
Expand All @@ -650,7 +638,7 @@ impl Image {
/// <img
/// src="https://rustxlsxwriter.github.io/images/image_set_object_movement.png">
///
pub fn set_object_movement(&mut self, option: ObjectMovement) -> &mut Image {
pub fn set_object_movement(mut self, option: ObjectMovement) -> Image {
self.object_movement = option;
self
}
Expand All @@ -677,7 +665,7 @@ impl Image {
/// * [`XlsxError::ParameterError`] - URL mouseover tool tip exceeds Excel's
/// limit of 255 characters.
///
pub fn set_url(&mut self, link: impl Into<Url>) -> Result<&Image, XlsxError> {
pub fn set_url(mut self, link: impl Into<Url>) -> Result<Image, XlsxError> {
let mut url = link.into();
url.initialize()?;

Expand Down Expand Up @@ -797,7 +785,7 @@ impl Image {
/// `name` - The VML object name/description.
///
#[doc(hidden)]
pub fn set_vml_name(&mut self, name: impl Into<String>) -> &mut Image {
pub fn set_vml_name(mut self, name: impl Into<String>) -> Image {
self.vml_name = name.into();
self
}
Expand Down
11 changes: 6 additions & 5 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4923,8 +4923,9 @@ impl Worksheet {
let width = self.column_pixel_width(col, image.object_movement);
let height = self.row_pixel_height(row, image.object_movement);

let mut image = image.clone();
image.set_scale_to_size(width, height, keep_aspect_ratio);
let image = image
.clone()
.set_scale_to_size(width, height, keep_aspect_ratio);

self.images.insert((row, col), image);

Expand Down Expand Up @@ -10119,9 +10120,9 @@ impl Worksheet {
/// # let worksheet = workbook.add_worksheet();
/// #
/// # // Scale the image so it fits in the header.
/// let mut image = Image::new("examples/rust_logo.png")?;
/// # image.set_scale_height(0.5);
/// # image.set_scale_width(0.5);
/// let image = Image::new("examples/rust_logo.png")?
/// .set_scale_height(0.5)
/// .set_scale_width(0.5);
/// #
/// // Insert the watermark image in the header.
/// worksheet.set_header("&C&[Picture]");
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/bootstrap53.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();

let mut image = Image::new("tests/input/images/rust_logo.png")?;
image.set_alt_text("Line 1.\n\nLine 2.");
let image = Image::new("tests/input/images/rust_logo.png")?.set_alt_text("Line 1.\n\nLine 2.");

worksheet.insert_image(8, 4, &image)?;

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/bootstrap54.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {

let worksheet = workbook.add_worksheet();

let mut image = Image::new("tests/input/images/red.jpg")?;
image.set_scale_width(2.0);
image.set_scale_height(0.5);
let image = Image::new("tests/input/images/red.jpg")?
.set_scale_width(2.0)
.set_scale_height(0.5);

worksheet.set_header("&L&G");
worksheet.set_header_image(&image, HeaderImagePosition::Left)?;
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/bootstrap55.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let worksheet3 = workbook.add_worksheet();
worksheet3.set_portrait();

let mut image = Image::new("tests/input/images/red.png")?;
image.set_alt_text("red.png");
let image = Image::new("tests/input/images/red.png")?.set_alt_text("red.png");

worksheet3.insert_image(8, 4, &image)?;

Expand Down
3 changes: 1 addition & 2 deletions tests/integration/embed_image06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let image = Image::new("tests/input/images/red.png")?;
worksheet.embed_image(0, 0, &image)?;

let mut image = Image::new("tests/input/images/red.png")?;
image.set_alt_text("red.png");
let image = Image::new("tests/input/images/red.png")?.set_alt_text("red.png");
worksheet.insert_image(8, 4, &image)?;

workbook.save(filename)?;
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/embed_image07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {
let image = Image::new("tests/input/images/blue.png")?;
worksheet.embed_image(2, 0, &image)?;

let mut image = Image::new("tests/input/images/red.png")?;
image.set_alt_text("red.png");
let image = Image::new("tests/input/images/red.png")?.set_alt_text("red.png");
worksheet.insert_image(8, 4, &image)?;

workbook.save(filename)?;
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/embed_image08.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ fn create_new_xlsx_file(filename: &str) -> Result<(), XlsxError> {

let worksheet = workbook.add_worksheet();

let mut image = Image::new("tests/input/images/red.png")?;
image.set_alt_text("Some alt text");
let image = Image::new("tests/input/images/red.png")?.set_alt_text("Some alt text");

worksheet.embed_image(0, 0, &image)?;

Expand Down
Loading

0 comments on commit 9029a10

Please sign in to comment.