Skip to main content

TransformFunctionOptions

Struct TransformFunctionOptions 

Source
pub struct TransformFunctionOptions(/* private fields */);
Expand description

The TransformFunctionOptions struct is a wrapper for serde_json::Value, any json data is accepted. Configuration options passed to transform functions when processing inventory entities.

TransformFunctionOptions is a wrapper around a JSON value that provides flexible, schema-free configuration data for transform functions. It allows passing arbitrary structured data to transforms without requiring predefined types or schemas.

The wrapper implements Deref and DerefMut to provide direct access to the underlying serde_json::Value, enabling use of all JSON value methods for accessing and manipulating the configuration data.

§Usage in Transforms

Transform functions receive an Option<&TransformFunctionOptions> parameter that can be used to access configuration data. The options are typically set on the Inventory using InventoryBuilder::transform_function_options().

§JSON Structure

The underlying JSON value can be any valid JSON structure:

  • Object: {"key": "value", "nested": {"data": 123}}
  • Array: ["item1", "item2"]
  • Primitive: "string", 42, true, null

§Examples

§Creating Options

// Simple key-value options
let options = TransformFunctionOptions::new(serde_json::json!({
    "default_port": 2222,
    "environment": "production"
}));

// Nested configuration
let options = TransformFunctionOptions::new(serde_json::json!({
    "ssh": {
        "port": 22,
        "timeout": 30
    },
    "netconf": {
        "port": 830,
        "timeout": 60
    }
}));

§Accessing Options in Transforms

struct PortTransform;

impl Transform for PortTransform {
    fn transform_host(&self, host: &Host, options: Option<&TransformFunctionOptions>) -> Host {
        // Access options using JSON value methods
        if let Some(opts) = options {
            if let Some(port) = opts.get("default_port").and_then(|v| v.as_u64()) {
                if host.port().is_none() {
                    return host.to_builder().port(port as u16).build();
                }
            }
        }
        host.clone()
    }
}

§Using with Inventory

let transform = TransformFunction::new(|host, options| {
    if let Some(opts) = options {
        if let Some(prefix) = opts.get("hostname_prefix").and_then(|v| v.as_str()) {
            if let Some(hostname) = host.hostname() {
                return host.to_builder()
                    .hostname(format!("{}{}", prefix, hostname))
                    .build();
            }
        }
    }
    host.clone()
});

let options = TransformFunctionOptions::new(serde_json::json!({
    "hostname_prefix": "prod-"
}));

let mut hosts = Hosts::new();
hosts.add_host("router1", Host::builder().hostname("router1").build());

let inventory = Inventory::builder()
    .hosts(hosts)
    .transform_function(transform)
    .transform_function_options(options)
    .build();

Implementations§

Source§

impl TransformFunctionOptions

Source

pub fn new(options: Value) -> Self

Creates a new TransformFunctionOptions instance from a JSON value.

This constructor wraps any valid JSON value in a TransformFunctionOptions struct, providing a flexible way to pass configuration data to transform functions. The JSON value can be of any type: object, array, string, number, boolean, or null.

The options are typically accessed within transform function implementations using the Deref trait to access the underlying serde_json::Value methods like get(), as_str(), as_object(), etc.

§Parameters
  • options - A serde_json::Value containing the configuration data to be passed to transform functions. This can be any valid JSON structure created using the serde_json::json! macro or parsed from JSON text.
§Returns

Returns a new TransformFunctionOptions instance wrapping the provided JSON value.

§Examples
// Create options with an object
let options = TransformFunctionOptions::new(serde_json::json!({
    "default_port": 2222,
    "environment": "production"
}));

// Create options with an array
let options = TransformFunctionOptions::new(serde_json::json!(["item1", "item2"]));

// Create options with a primitive value
let options = TransformFunctionOptions::new(serde_json::json!("simple_string"));

Trait Implementations§

Source§

impl Clone for TransformFunctionOptions

Source§

fn clone(&self) -> TransformFunctionOptions

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TransformFunctionOptions

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for TransformFunctionOptions

Source§

type Target = <TransformFunctionOptions as DerefTarget>::Target

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for TransformFunctionOptions

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl DerefTarget for TransformFunctionOptions

Source§

impl<'de> Deserialize<'de> for TransformFunctionOptions

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl JsonSchema for TransformFunctionOptions

Source§

fn schema_name() -> Cow<'static, str>

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn inline_schema() -> bool

Whether JSON Schemas generated for this type should be included directly in parent schemas, rather than being re-used where possible using the $ref keyword. Read more
Source§

impl PartialEq for TransformFunctionOptions

Source§

fn eq(&self, other: &TransformFunctionOptions) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for TransformFunctionOptions

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for TransformFunctionOptions

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.