reasoninglayer 0.2.1

Rust client SDK for the Reasoning Layer API
Documentation
//! Builders for [`ValueDto`] and [`FuzzyShapeDto`].
//!
//! For simple types, pass plain Rust values via the `From` implementations on [`ValueDto`] itself —
//! the `Value::*` functions below are reserved for types that cannot be expressed as plain values
//! (references, fuzzy numbers, sets).
//!
//! ```
//! use reasoninglayer::{Value, FuzzyShape};
//!
//! let reference = Value::reference("550e8400-e29b-41d4-a716-446655440000");
//! let fuzzy = Value::fuzzy_number(FuzzyShape::triangular(20.0, 22.0, 24.0));
//! let group = Value::set(vec!["a".into()], vec!["a".into(), "b".into()], None);
//! ```

use crate::types::values::{FuzzyNumber, FuzzyScalar, FuzzyShapeDto, SetValue, ValueDto};

/// Builder namespace for complex [`ValueDto`] values.
pub struct Value;

impl Value {
    /// Create a plain string value. Prefer `ValueDto::from(s)` for idiomatic code.
    pub fn string(s: impl Into<String>) -> ValueDto {
        ValueDto::String(s.into())
    }

    /// Create an integer value.
    pub fn integer(i: i64) -> ValueDto {
        ValueDto::Integer(i)
    }

    /// Create a real value.
    pub fn real(r: f64) -> ValueDto {
        ValueDto::Real(r)
    }

    /// Create a boolean value.
    pub fn boolean(b: bool) -> ValueDto {
        ValueDto::Boolean(b)
    }

    /// Create an uninstantiated value.
    pub fn uninstantiated() -> ValueDto {
        ValueDto::Uninstantiated
    }

    /// Create a list value.
    pub fn list(items: Vec<ValueDto>) -> ValueDto {
        ValueDto::List(items)
    }

    /// Create a reference to another term by UUID.
    pub fn reference(id: impl Into<String>) -> ValueDto {
        ValueDto::Reference(id.into())
    }

    /// Create a fuzzy scalar with a value and membership degree.
    pub fn fuzzy_scalar(value: f64, membership: f64) -> ValueDto {
        ValueDto::FuzzyScalar(FuzzyScalar { value, membership })
    }

    /// Create a fuzzy number defined by a membership function shape.
    pub fn fuzzy_number(shape: FuzzyShapeDto) -> ValueDto {
        ValueDto::FuzzyNumber(FuzzyNumber { shape })
    }

    /// Create a set value with lower and upper bounds and an optional sort constraint.
    pub fn set(
        lower: Vec<String>,
        upper: Vec<String>,
        sort_constraint: Option<String>,
    ) -> ValueDto {
        ValueDto::Set(SetValue {
            lower,
            upper,
            sort_constraint,
        })
    }
}

impl From<&str> for ValueDto {
    fn from(s: &str) -> Self {
        ValueDto::String(s.to_string())
    }
}

impl From<String> for ValueDto {
    fn from(s: String) -> Self {
        ValueDto::String(s)
    }
}

impl From<i64> for ValueDto {
    fn from(i: i64) -> Self {
        ValueDto::Integer(i)
    }
}

impl From<i32> for ValueDto {
    fn from(i: i32) -> Self {
        ValueDto::Integer(i.into())
    }
}

impl From<f64> for ValueDto {
    fn from(f: f64) -> Self {
        ValueDto::Real(f)
    }
}

impl From<bool> for ValueDto {
    fn from(b: bool) -> Self {
        ValueDto::Boolean(b)
    }
}

/// Builder namespace for fuzzy membership function shapes.
pub struct FuzzyShape;

impl FuzzyShape {
    /// Triangular: rises from 0 at `a` to 1 at `b`, back to 0 at `c`.
    pub fn triangular(a: f64, b: f64, c: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::Triangular { a, b, c }
    }

    /// Trapezoidal: rises from 0 at `a` to 1 at `b`, plateau to `c`, falls to 0 at `d`.
    pub fn trapezoidal(a: f64, b: f64, c: f64, d: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::Trapezoidal { a, b, c, d }
    }

    /// Gaussian bell curve centred at `mean` with the given standard deviation.
    pub fn gaussian(mean: f64, std_dev: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::Gaussian { mean, std_dev }
    }

    /// Cyclic Gaussian with periodic wrapping.
    pub fn cyclic_gaussian(mean: f64, std_dev: f64, period: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::CyclicGaussian {
            mean,
            std_dev,
            period,
        }
    }

    /// Sigmoid (logistic) S-curve. Positive steepness rises left-to-right.
    pub fn sigmoid(midpoint: f64, steepness: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::Sigmoid {
            midpoint,
            steepness,
        }
    }

    /// Generalised bell with tunable slope.
    pub fn bell(center: f64, width: f64, slope: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::Bell {
            center,
            width,
            slope,
        }
    }

    /// Difference of two sigmoids — creates a smooth bounded region.
    pub fn sigmoid_difference(
        midpoint1: f64,
        steepness1: f64,
        midpoint2: f64,
        steepness2: f64,
    ) -> FuzzyShapeDto {
        FuzzyShapeDto::SigmoidDifference {
            midpoint1,
            steepness1,
            midpoint2,
            steepness2,
        }
    }

    /// Product of two Gaussians — asymmetric flat-top bell.
    pub fn gaussian_product(mean1: f64, std_dev1: f64, mean2: f64, std_dev2: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::GaussianProduct {
            mean1,
            std_dev1,
            mean2,
            std_dev2,
        }
    }

    /// Product of two sigmoids — always non-negative bounded region.
    pub fn sigmoid_product(
        midpoint1: f64,
        steepness1: f64,
        midpoint2: f64,
        steepness2: f64,
    ) -> FuzzyShapeDto {
        FuzzyShapeDto::SigmoidProduct {
            midpoint1,
            steepness1,
            midpoint2,
            steepness2,
        }
    }

    /// Cosine with compact support.
    pub fn cosine(center: f64, width: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::Cosine { center, width }
    }

    /// Spike (Laplacian) with exponential tails.
    pub fn spike(center: f64, width: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::Spike { center, width }
    }

    /// Cauchy (Lorentzian) with heavy tails.
    pub fn cauchy(center: f64, gamma: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::Cauchy { center, gamma }
    }

    /// S-shaped: piecewise quadratic 0→1.
    pub fn s_shape(a: f64, b: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::SShape { a, b }
    }

    /// Z-shaped: piecewise quadratic 1→0.
    pub fn z_shape(a: f64, b: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::ZShape { a, b }
    }

    /// Pi-shaped: smooth flat-top bump (S · Z).
    pub fn pi_shape(a: f64, b: f64, c: f64, d: f64) -> FuzzyShapeDto {
        FuzzyShapeDto::PiShape { a, b, c, d }
    }

    /// Piecewise linear membership function defined by `(x, mu)` points.
    pub fn piecewise_linear(points: Vec<(f64, f64)>) -> FuzzyShapeDto {
        FuzzyShapeDto::PiecewiseLinear { points }
    }
}