From 3a37819950b91d980590833246d31e7885d3dd27 Mon Sep 17 00:00:00 2001 From: Kazuya Takei Date: Sat, 6 Apr 2024 02:18:49 +0900 Subject: [PATCH] fix: parse for pattern includeed replacement targets --- src/config/cargo_toml.rs | 13 +++++++++---- src/config/pyproject_toml.rs | 13 +++++++++---- .../single-line-with-cargo-toml/after/Cargo.toml | 16 ++++++++++++++++ .../after/example.txt | 1 + .../before/Cargo.toml | 16 ++++++++++++++++ .../before/example.txt | 1 + .../after/example.txt | 1 + .../after/pyproject.toml | 11 +++++++++++ .../before/example.txt | 1 + .../before/pyproject.toml | 11 +++++++++++ 10 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 tests/return-0/single-line-with-cargo-toml/after/Cargo.toml create mode 100644 tests/return-0/single-line-with-cargo-toml/after/example.txt create mode 100644 tests/return-0/single-line-with-cargo-toml/before/Cargo.toml create mode 100644 tests/return-0/single-line-with-cargo-toml/before/example.txt create mode 100644 tests/return-0/single-line-with-pyproject-toml/after/example.txt create mode 100644 tests/return-0/single-line-with-pyproject-toml/after/pyproject.toml create mode 100644 tests/return-0/single-line-with-pyproject-toml/before/example.txt create mode 100644 tests/return-0/single-line-with-pyproject-toml/before/pyproject.toml diff --git a/src/config/cargo_toml.rs b/src/config/cargo_toml.rs index 482597c..4bb1b32 100644 --- a/src/config/cargo_toml.rs +++ b/src/config/cargo_toml.rs @@ -3,6 +3,7 @@ use std::io::prelude::*; use std::path::PathBuf; use anyhow::{anyhow, Result}; +use toml::{de::Error, Table}; use toml_edit::{value, DocumentMut}; use super::{Config, ParseAvaliable}; @@ -37,15 +38,19 @@ impl ParseAvaliable for Property { } fn get_config(&self) -> Result { - let mut item = self.doc.as_item(); + let mut item = &self.doc.to_string().parse::().unwrap(); for k in ["package", "metadata", "age"] { let child = item.get(k); - if child.is_none() || !child.unwrap().is_table() { + if child.is_none() { return Err(anyhow!("It does not have valid values.")); } - item = child.unwrap(); + let child = child.unwrap(); + if !child.is_table() { + return Err(anyhow!("It does not have valid values.")); + } + item = child.as_table().unwrap(); } - let config = toml::from_str::(item.to_string().as_str()); + let config: Result = toml::from_str(item.to_string().as_str()); if config.is_err() { return Err(anyhow!(config.unwrap_err())); } diff --git a/src/config/pyproject_toml.rs b/src/config/pyproject_toml.rs index 69394db..9c29b9b 100644 --- a/src/config/pyproject_toml.rs +++ b/src/config/pyproject_toml.rs @@ -3,6 +3,7 @@ use std::io::prelude::*; use std::path::PathBuf; use anyhow::{anyhow, Result}; +use toml::{de::Error, Table}; use toml_edit::{value, DocumentMut}; use super::{Config, ParseAvaliable}; @@ -37,15 +38,19 @@ impl ParseAvaliable for Property { } fn get_config(&self) -> Result { - let mut item = self.doc.as_item(); + let mut item = &self.doc.to_string().parse::
().unwrap(); for k in ["tool", "age"] { let child = item.get(k); - if child.is_none() || !child.unwrap().is_table() { + if child.is_none() { return Err(anyhow!("It does not have valid values.")); } - item = child.unwrap(); + let child = child.unwrap(); + if !child.is_table() { + return Err(anyhow!("It does not have valid values.")); + } + item = child.as_table().unwrap(); } - let config = toml::from_str::(item.to_string().as_str()); + let config: Result = toml::from_str(item.to_string().as_str()); if config.is_err() { return Err(anyhow!(config.unwrap_err())); } diff --git a/tests/return-0/single-line-with-cargo-toml/after/Cargo.toml b/tests/return-0/single-line-with-cargo-toml/after/Cargo.toml new file mode 100644 index 0000000..f34e4ea --- /dev/null +++ b/tests/return-0/single-line-with-cargo-toml/after/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "dummy" +version = "0.1.0" +edition = "2021" + +[package.metadata.age] +current_version = "0.2.0" + +[[package.metadata.age.files]] +path = "example.txt" +search = "version = '{{current_version}}'" +replace = "version = '{{new_version}}'" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/return-0/single-line-with-cargo-toml/after/example.txt b/tests/return-0/single-line-with-cargo-toml/after/example.txt new file mode 100644 index 0000000..60fb6a5 --- /dev/null +++ b/tests/return-0/single-line-with-cargo-toml/after/example.txt @@ -0,0 +1 @@ +version = '0.2.0' diff --git a/tests/return-0/single-line-with-cargo-toml/before/Cargo.toml b/tests/return-0/single-line-with-cargo-toml/before/Cargo.toml new file mode 100644 index 0000000..c7bc7ae --- /dev/null +++ b/tests/return-0/single-line-with-cargo-toml/before/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "dummy" +version = "0.1.0" +edition = "2021" + +[package.metadata.age] +current_version = "0.1.0" + +[[package.metadata.age.files]] +path = "example.txt" +search = "version = '{{current_version}}'" +replace = "version = '{{new_version}}'" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tests/return-0/single-line-with-cargo-toml/before/example.txt b/tests/return-0/single-line-with-cargo-toml/before/example.txt new file mode 100644 index 0000000..75b8a57 --- /dev/null +++ b/tests/return-0/single-line-with-cargo-toml/before/example.txt @@ -0,0 +1 @@ +version = '0.1.0' diff --git a/tests/return-0/single-line-with-pyproject-toml/after/example.txt b/tests/return-0/single-line-with-pyproject-toml/after/example.txt new file mode 100644 index 0000000..60fb6a5 --- /dev/null +++ b/tests/return-0/single-line-with-pyproject-toml/after/example.txt @@ -0,0 +1 @@ +version = '0.2.0' diff --git a/tests/return-0/single-line-with-pyproject-toml/after/pyproject.toml b/tests/return-0/single-line-with-pyproject-toml/after/pyproject.toml new file mode 100644 index 0000000..8ced22f --- /dev/null +++ b/tests/return-0/single-line-with-pyproject-toml/after/pyproject.toml @@ -0,0 +1,11 @@ +[project] +name = "dummy" +version = "0.1.0" + +[tool.age] +current_version = "0.2.0" + +[[tool.age.files]] +path = "example.txt" +search = "version = '{{current_version}}'" +replace = "version = '{{new_version}}'" diff --git a/tests/return-0/single-line-with-pyproject-toml/before/example.txt b/tests/return-0/single-line-with-pyproject-toml/before/example.txt new file mode 100644 index 0000000..75b8a57 --- /dev/null +++ b/tests/return-0/single-line-with-pyproject-toml/before/example.txt @@ -0,0 +1 @@ +version = '0.1.0' diff --git a/tests/return-0/single-line-with-pyproject-toml/before/pyproject.toml b/tests/return-0/single-line-with-pyproject-toml/before/pyproject.toml new file mode 100644 index 0000000..ce74991 --- /dev/null +++ b/tests/return-0/single-line-with-pyproject-toml/before/pyproject.toml @@ -0,0 +1,11 @@ +[project] +name = "dummy" +version = "0.1.0" + +[tool.age] +current_version = "0.1.0" + +[[tool.age.files]] +path = "example.txt" +search = "version = '{{current_version}}'" +replace = "version = '{{new_version}}'"