Skip to main content

PlaceholderResolver

Struct PlaceholderResolver 

Source
pub struct PlaceholderResolver;
Expand description

PlaceholderResolver - プレースホルダー抽出・置換ユーティリティ

依存関係を持たない純粋な文字列処理ユーティリティ。 値の解決は呼び出し側の責務。

設計方針:

  • エスケープは不要(${} は予約語、YAML DSLとして割り切る)
  • 再帰置換を防止(置換後の値が再度置換されない)
  • ドット記法は不要(フラットキーで十分、ParameterBuilderの責務)

Implementations§

Source§

impl PlaceholderResolver

Source

pub fn extract_placeholders(template: &str) -> Vec<String>

テンプレート文字列からプレースホルダ名を抽出

ドット記法を含む placeholder にも対応(例: ${connection.tenant})

§Examples
use state_engine::common::placeholder_resolver::PlaceholderResolver;

let template = "user:${sso_user_id}:${tenant_id}";
let result = PlaceholderResolver::extract_placeholders(template);
assert_eq!(result, vec!["sso_user_id", "tenant_id"]);

let template2 = "db:${connection.tenant}";
let result2 = PlaceholderResolver::extract_placeholders(template2);
assert_eq!(result2, vec!["connection.tenant"]);
Source

pub fn replace(template: &str, params: &HashMap<String, String>) -> String

プレースホルダを値で置換(再帰置換を防止)

置換は一度のみ実行され、置換後の値が再度置換されることはない。 未定義のプレースホルダーはそのまま残される。

§Examples
use state_engine::common::placeholder_resolver::PlaceholderResolver;
use std::collections::HashMap;

let template = "user:${sso_user_id}:${tenant_id}";
let mut params = HashMap::new();
params.insert("sso_user_id".to_string(), "user001".to_string());
params.insert("tenant_id".to_string(), "1".to_string());

let result = PlaceholderResolver::replace(template, &params);
assert_eq!(result, "user:user001:1");
§再帰置換の防止
use state_engine::common::placeholder_resolver::PlaceholderResolver;
use std::collections::HashMap;

let template = "${a}";
let mut params = HashMap::new();
params.insert("a".to_string(), "${b}".to_string());
params.insert("b".to_string(), "final".to_string());

let result = PlaceholderResolver::replace(template, &params);
// 'final' にはならず '${b}' のまま(意図的)
assert_eq!(result, "${b}");
Source

pub fn replace_in_map(value: Value, params: &HashMap<String, String>) -> Value

配列の値でプレースホルダを一括置換(再帰的)

§Examples
use state_engine::common::placeholder_resolver::PlaceholderResolver;
use std::collections::HashMap;
use serde_yaml_ng::Value;

let mut values = HashMap::new();
values.insert("key1".to_string(), Value::String("${value1}".to_string()));
values.insert("key2".to_string(), Value::String("${value2}".to_string()));

let mut params = HashMap::new();
params.insert("value1".to_string(), "a".to_string());
params.insert("value2".to_string(), "b".to_string());

let result = PlaceholderResolver::replace_in_map(Value::Mapping(
    values.into_iter().map(|(k, v)| (Value::String(k), v)).collect()
), &params);

// result["key1"] == "a", result["key2"] == "b"
Source

pub fn resolve_typed<F>(value: Value, resolver: &mut F) -> Value
where F: FnMut(&str) -> Option<Value>,

型付きプレースホルダー解決

callback を使って値を解決し、型を保持する。 値全体が ${…} のみの場合は型を保持、文字列の一部なら文字列置換。

§Arguments
  • value - 解決対象の値
  • resolver - プレースホルダー名から値を解決する callback
§Returns
  • 解決後の値(型保持)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.