tphrase/substitutor.rs
1//! Substitutor
2//
3// Copyright © 2025 OOTA, Masato
4//
5// This file is part of TPhrase for Rust.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10//
11// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12//
13// OR
14//
15// Licensed under the Apache License, Version 2.0 (the "License"); you may not use TPhrase for Rust except in compliance with the License. You may obtain a copy of the License at
16//
17// http://www.apache.org/licenses/LICENSE-2.0
18//
19// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
20
21/// The substitutor implementing the gsub function in tphrase.
22/// You can replace the default into your version. [`Generator`] doesn't have [`Clone`] and [`Debug`] traits if your instance of [`Substitutor`] doesn't have [`Clone`] and [`Debug`] traits.
23///
24/// [`Generator`]: struct.Generator.html
25pub trait Substitutor {
26 /// Create a substitutor. Used in [`parse()`], [`parse_str()`], [`Generator::from_str()`], and [`Syntax::from_str()`].
27 ///
28 /// [`parse()`]: fn.parse.html
29 /// [`parse_str()`]: fn.parse_str.html
30 /// [`Generator::from_str()`]: struct.Generator.html#method.from_str
31 /// [`Syntax::from_str()`]: struct.Syntax.html#method.from_str
32 fn new() -> Self;
33 /// Substitute str. Used in [`Generator::generate()`].
34 ///
35 /// [`Generator::generate()`]: struct.Generator.html#method.generate
36 fn gsub<'a>(self: &Self, s: &'a str) -> std::borrow::Cow<'a, str>;
37 /// Add an instruction of gsub function. Used in parsing.
38 ///
39 /// The string in [`Err`] is the error message to show users.
40 fn add(
41 self: &mut Self,
42 pattern: &str,
43 repl: String,
44 limit: usize,
45 ) -> Result<(), SubstitutorAddError>;
46}
47
48/// The type that represents the error when adding the parameters to a [`Substitutor`].
49#[derive(Clone, Default, Debug)]
50pub struct SubstitutorAddError {
51 error_message: String,
52}
53impl SubstitutorAddError {
54 /// Create a new instance.
55 ///
56 /// # Note
57 /// - Against the common manner in Rust, the beginning of `msg` should be capital letter and the end is the period.
58 pub fn new(msg: String) -> Self {
59 Self { error_message: msg }
60 }
61 /// The error message.
62 pub fn error_message(self: &Self) -> &String {
63 &self.error_message
64 }
65}
66impl std::fmt::Display for SubstitutorAddError {
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 write!(f, "substitutor error in add()")?;
69 if !self.error_message.is_empty() {
70 write!(f, ": \"{}\"", self.error_message)?;
71 }
72 Ok(())
73 }
74}
75impl std::error::Error for SubstitutorAddError {}