Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow borrowed dictionary in replace_text #26

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/document/body.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use derive_more::From;
use hard_xml::{XmlRead, XmlWrite};
use std::borrow::Borrow;

use crate::__xml_test_suites;
use crate::document::{Paragraph, Run, Table, TableCell};
Expand Down Expand Up @@ -45,15 +46,14 @@
where
S: AsRef<str>,
{
let dic = (old, new);
let dic = vec![dic];
let _d = self.replace_text(&dic);
let _d = self.replace_text(&[(old, new)]);
}

pub fn replace_text<'b, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
pub fn replace_text<'b, I, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
where
S: AsRef<str> + 'b,
T: IntoIterator<Item = &'b (S, S)> + std::marker::Copy,
T: IntoIterator<Item = I> + Copy,
I: Borrow<(S, S)>,
{
for content in self.content.iter_mut() {
match content {
Expand Down Expand Up @@ -94,7 +94,7 @@
/// A set of elements that can be contained in the body
#[derive(Debug, From, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum BodyContent<'a> {

Check warning on line 97 in src/document/body.rs

View workflow job for this annotation

GitHub Actions / test

large size difference between variants
#[xml(tag = "w:p")]
Paragraph(Paragraph<'a>),
#[xml(tag = "w:tbl")]
Expand Down
10 changes: 5 additions & 5 deletions src/document/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//!

use hard_xml::{XmlRead, XmlResult, XmlWrite, XmlWriter};
use std::borrow::Borrow;
use std::io::Write;

use crate::__xml_test_suites;
Expand All @@ -29,15 +30,14 @@ impl<'a> Header<'a> {
where
S: AsRef<str>,
{
let dic = (old, new);
let dic = vec![dic];
let _d = self.replace_text(&dic);
let _d = self.replace_text(&[(old, new)]);
}

pub fn replace_text<'b, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
pub fn replace_text<'b, I, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
where
S: AsRef<str> + 'b,
T: IntoIterator<Item = &'b (S, S)> + std::marker::Copy,
T: IntoIterator<Item = I> + Copy,
I: Borrow<(S, S)>,
{
for content in self.content.iter_mut() {
match content {
Expand Down
7 changes: 4 additions & 3 deletions src/document/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused_must_use)]
use derive_more::From;
use hard_xml::{XmlRead, XmlWrite};
use std::borrow::Cow;
use std::borrow::{Borrow, Cow};

use crate::{
__setter, __xml_test_suites,
Expand Down Expand Up @@ -110,13 +110,14 @@
.flatten()
}

pub fn replace_text<'b, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
pub fn replace_text<'b, I, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
where
S: AsRef<str> + 'b,
T: IntoIterator<Item = &'b (S, S)> + std::marker::Copy,
T: IntoIterator<Item = I> + Copy,
I: Borrow<(S, S)>,
{
for content in self.content.iter_mut() {
match content {

Check warning on line 120 in src/document/paragraph.rs

View workflow job for this annotation

GitHub Actions / test

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
ParagraphContent::Run(r) => {
r.replace_text(dic)?;
}
Expand Down
13 changes: 6 additions & 7 deletions src/document/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(unused_must_use)]
use derive_more::From;
use hard_xml::{XmlRead, XmlWrite};
use std::borrow::Cow;
use std::borrow::{Borrow, Cow};

use crate::{
__setter, __xml_test_suites,
Expand Down Expand Up @@ -151,22 +151,21 @@
where
S: AsRef<str>,
{
let dic = (old, new);
let dic = vec![dic];
self.replace_text(&dic);
self.replace_text(&[(old, new)]);
}

pub fn replace_text<'b, T, S>(&mut self, dic: T) -> DocxResult<()>
pub fn replace_text<'b, I, T, S>(&mut self, dic: T) -> DocxResult<()>
where
S: AsRef<str> + 'b,
T: IntoIterator<Item = &'b (S, S)> + std::marker::Copy,
T: IntoIterator<Item = I> + Copy,
I: Borrow<(S, S)>,
{
for c in self.content.iter_mut() {
match c {

Check warning on line 164 in src/document/run.rs

View workflow job for this annotation

GitHub Actions / test

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
RunContent::Text(t) => {
let mut tc = t.text.to_string();
for p in dic {
tc = tc.replace(p.0.as_ref(), p.1.as_ref());
tc = tc.replace(p.borrow().0.as_ref(), p.borrow().1.as_ref());
}
t.text = tc.into();
}
Expand All @@ -181,7 +180,7 @@
/// A set of elements that can be contained as the content of a run.
#[derive(Debug, From, XmlRead, XmlWrite, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub enum RunContent<'a> {

Check warning on line 183 in src/document/run.rs

View workflow job for this annotation

GitHub Actions / test

large size difference between variants
#[xml(tag = "w:br")]
Break(Break),
#[xml(tag = "w:t")]
Expand Down
7 changes: 4 additions & 3 deletions src/document/table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unused_must_use)]
use hard_xml::{XmlRead, XmlWrite};
use std::borrow::Cow;
use std::borrow::{Borrow, Cow};

use crate::{
__setter, __xml_test_suites,
Expand Down Expand Up @@ -48,10 +48,11 @@ impl<'a> Table<'a> {
.flat_map(|content| content.iter_text_mut())
}

pub fn replace_text<'b, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
pub fn replace_text<'b, I, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
where
S: AsRef<str> + 'b,
T: IntoIterator<Item = &'b (S, S)> + std::marker::Copy,
T: IntoIterator<Item = I> + Copy,
I: Borrow<(S, S)>,
{
for row in self.rows.iter_mut() {
row.replace_text(dic)?;
Expand Down
7 changes: 4 additions & 3 deletions src/document/table_cell.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(unused_must_use)]
use std::borrow::Cow;
use std::borrow::{Borrow, Cow};

use derive_more::From;
use hard_xml::{XmlRead, XmlWrite};
Expand Down Expand Up @@ -38,7 +38,7 @@
}

pub fn iter_text(&self) -> impl Iterator<Item = &Cow<'a, str>> {
self.content

Check warning on line 41 in src/document/table_cell.rs

View workflow job for this annotation

GitHub Actions / test

this `.filter_map` can be written more simply using `.map`
.iter()
.filter_map(|content| match content {
TableCellContent::Paragraph(p) => Some(p.iter_text()),
Expand All @@ -47,7 +47,7 @@
}

pub fn iter_text_mut(&mut self) -> impl Iterator<Item = &mut Cow<'a, str>> {
self.content

Check warning on line 50 in src/document/table_cell.rs

View workflow job for this annotation

GitHub Actions / test

this `.filter_map` can be written more simply using `.map`
.iter_mut()
.filter_map(|content| match content {
TableCellContent::Paragraph(p) => Some(p.iter_text_mut()),
Expand All @@ -55,10 +55,11 @@
.flatten()
}

pub fn replace_text<'b, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
pub fn replace_text<'b, I, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
where
S: AsRef<str> + 'b,
T: IntoIterator<Item = &'b (S, S)> + std::marker::Copy,
T: IntoIterator<Item = I> + Copy,
I: Borrow<(S, S)>,
{
for content in self.content.iter_mut() {
match content {
Expand Down
7 changes: 4 additions & 3 deletions src/document/table_row.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(unused_must_use)]
use std::borrow::Cow;
use std::borrow::{Borrow, Cow};

use super::SDT;

Expand Down Expand Up @@ -83,10 +83,11 @@ impl<'a> TableRow<'a> {
.flatten()
}

pub fn replace_text<'b, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
pub fn replace_text<'b, I, T, S>(&mut self, dic: T) -> crate::DocxResult<()>
where
S: AsRef<str> + 'b,
T: IntoIterator<Item = &'b (S, S)> + std::marker::Copy,
T: IntoIterator<Item = I> + Copy,
I: Borrow<(S, S)>,
{
for cell in self.cells.iter_mut() {
if let TableRowContent::TableCell(c) = cell {
Expand Down
29 changes: 28 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
extern crate docx_rust;
use std::fs::read_dir;

use docx_rust::{
document::{BodyContent, ParagraphContent, RunContent},
rels::TargetMode,
DocxFile,
};
use std::collections::HashMap;
use std::fs::read_dir;

#[test]
fn read_and_replace() {
Expand All @@ -19,6 +20,32 @@ fn read_and_replace() {
let _d = docx.write_file(path2).unwrap();
}

#[test]
fn replace_text_multiple() {
// reader
let path = std::path::Path::new("./tests/aaa/aa.docx");
let book = DocxFile::from_file(path).unwrap();
let mut docx = book.parse().unwrap();

let map = HashMap::from([("好日子", "好天气")]);
docx.document.body.replace_text(&map).unwrap();

let map = HashMap::from([("好日子".to_string(), "好天气".to_string())]);
docx.document.body.replace_text(&map).unwrap();

let slice = [("好日子", "好天气")];
docx.document.body.replace_text(&slice).unwrap();

let slice = [("好日子".to_string(), "好天气".to_string())];
docx.document.body.replace_text(&slice).unwrap();

let vec = vec![("好日子", "好天气")];
docx.document.body.replace_text(&vec).unwrap();

let vec = vec![("好日子".to_string(), "好天气".to_string())];
docx.document.body.replace_text(&vec).unwrap();
}

#[test]
fn read_list() {
let path = std::path::Path::new("./tests/aaa/aa_list.docx");
Expand Down
Loading