oxblade 0.1.0

Blade templates for Laravel for Rust: Blade's markup and directives, Laravel's helpers, Rust's expressions. Community package, not affiliated with Laravel.
Documentation
use super::arguments::{Args, formatted, string, text_bytes, text_list};
use super::php_values::{PhpView, from_php};
use super::{dispatch, upstream};
use crate::error::Result;
use crate::value::Value;
use laravel_rust_format_core::{php::PhpValue, str as laravel};

pub(super) fn subject_first<'a>(
    name: &str,
    subject: Result<&str>,
    args: Args<'_, 'a, '_>,
) -> Option<Result<Value<'a>>> {
    dispatch!(name, args, {
        "after" => 1..=1, formatted(laravel::after(subject?, args.str(0)?).to_owned());
        "after_last" => 1..=1, formatted(laravel::after_last(subject?, args.str(0)?).to_owned());
        "before" => 1..=1, formatted(laravel::before(subject?, args.str(0)?).to_owned());
        "before_last" => 1..=1, formatted(laravel::before_last(subject?, args.str(0)?).to_owned());
        "between" => 2..=2, formatted(laravel::between(subject?, args.str(0)?, args.str(1)?).to_owned());
        "between_first" => 2..=2, formatted(laravel::between_first(subject?, args.str(0)?, args.str(1)?).to_owned());
        "chop_start" => 1..=1, formatted(laravel::chop_start_any(subject?, &args.string_or_list(0)?).to_owned());
        "chop_end" => 1..=1, formatted(laravel::chop_end_any(subject?, &args.string_or_list(0)?).to_owned());
        "contains" | "contains_any" => 1..=2, Ok(Value::Bool(laravel::contains_any_with(subject?, &args.string_or_list(0)?, args.bool_or(1, false)?)));
        "contains_all" => 1..=2, Ok(Value::Bool(laravel::contains_all_with(subject?, &args.strings(0)?, args.bool_or(1, false)?)));
        "doesnt_contain" => 1..=2, Ok(Value::Bool(laravel::doesnt_contain_any_with(subject?, &args.string_or_list(0)?, args.bool_or(1, false)?)));
        "starts_with" => 1..=1, Ok(Value::Bool(laravel::starts_with_any(subject?, &args.string_or_list(0)?)));
        "ends_with" => 1..=1, Ok(Value::Bool(laravel::ends_with_any(subject?, &args.string_or_list(0)?)));
        "doesnt_start_with" => 1..=1, Ok(Value::Bool(laravel::doesnt_start_with_any(subject?, &args.string_or_list(0)?)));
        "doesnt_end_with" => 1..=1, Ok(Value::Bool(laravel::doesnt_end_with_any(subject?, &args.string_or_list(0)?)));
        "start" => 1..=1, formatted(laravel::start(subject?, args.str(0)?));
        "finish" => 1..=1, formatted(laravel::finish(subject?, args.str(0)?));
        "wrap" => 1..=2, formatted(laravel::wrap_with(subject?, args.str(0)?, args.opt_str(1)?.unwrap_or(args.str(0)?)));
        "unwrap" => 1..=2, formatted(laravel::unwrap_with(subject?, args.str(0)?, args.opt_str(1)?.unwrap_or(args.str(0)?)).to_owned());
        "counted" => 1..=1, formatted(laravel::counted(subject?, args.number(0)?));
        "is_ascii" => 0..=0, Ok(Value::Bool(laravel::is_ascii(subject?)));
        "is_json" => 0..=0, Ok(Value::Bool(laravel::is_json(subject?)));
        "is_ulid" => 0..=0, Ok(Value::Bool(laravel::is_ulid(subject?)));
        "is_url" => 0..=1, Ok(Value::Bool(if args.is_null(0) {
            laravel::is_url(subject?)
        } else {
            laravel::is_url_with(subject?, &args.strings(0)?)
        }));
        "is_uuid" => 0..=1, Ok(Value::Bool(if args.is_null(0) {
            laravel::is_uuid(subject?)
        } else {
            laravel::is_uuid_with(subject?, uuid_version(args)?)
        }));
        "excerpt" => 0..=2, {
            let options = args.optional_map(1)?;
            let radius = options.number_or("radius", 100)?;
            let omission = options.str_or("omission", "...")?;
            Ok(laravel::excerpt_with(subject?, args.str_or(0, "")?, radius, omission)
                .map_or(Value::Null, Value::string))
        };
        "length" => 0..=1, length(subject?, args.opt_str(0)?);
        "position" => 1..=3, position(subject?, args.str(0)?, args.int_or(1, 0)?, args.opt_str(2)?);
        "substr" => 1..=3, {
            let subject = subject?;
            let start = args.int(0)?;
            let count = args.opt_int(1)?;
            match args.opt_str(2)? {
                Some(encoding) => text_bytes(upstream(laravel::substr_encoded(subject, start, count, encoding))?),
                None => formatted(laravel::substr_with(subject, start, count).to_owned()),
            }
        };
        "char_at" => 1..=1, Ok(laravel::char_at(subject?, args.int(0)?).map_or(Value::Bool(false), |character| Value::string(character.to_owned())));
        "take" => 1..=1, formatted(laravel::take(subject?, args.int(0)?).to_owned());
        "substr_count" => 1..=3, count(subject?, args.str(0)?, args.int_or(1, 0)?, args.opt_int(2)?);
        "explode" => 1..=2, Ok(text_list(upstream(laravel_rust_format_core::php::explode(
            args.str(0)?, subject?, args.int_or(1, i64::MAX)?
        ))?));
    })
}

pub(super) fn wildcard<'a>(
    subject: Result<&str>,
    args: Args<'_, 'a, '_>,
    pattern: usize,
    options: usize,
) -> Option<Result<Value<'a>>> {
    Some((|| {
        args.check(options, options + 1)?;
        Ok(Value::Bool(laravel::is_any_with(
            &args.string_or_list(pattern)?,
            subject?,
            args.bool_or(options, false)?,
        )))
    })())
}

pub(super) fn parse_callback<'a>(args: Args<'_, 'a, '_>) -> Option<Result<Value<'a>>> {
    Some((|| {
        args.check(1, 2)?;
        let (target, method) = laravel::parse_callback_with(args.str(0)?, args.opt_str(1)?);
        Ok(Value::List(vec![
            Value::string(target.to_owned()),
            method.map_or(Value::Null, |method| Value::string(method.to_owned())),
        ]))
    })())
}

pub(super) fn substr_replace<'a>(
    args: Args<'_, 'a, '_>,
    receiver: Option<&str>,
) -> Option<Result<Value<'a>>> {
    Some((|| {
        let start = usize::from(receiver.is_none());
        args.check(start + 1, start + 3)?;
        let php_value = |index| {
            if args.is_null(index) {
                Ok(PhpValue::Null)
            } else {
                PhpView::from_value(args.value(index)?)?.into_owned()
            }
        };
        let subject = match receiver {
            Some(receiver) => PhpValue::String(receiver.to_owned()),
            None => php_value(0)?,
        };
        let result = upstream(laravel::substr_replace_values(
            &subject,
            &php_value(start)?,
            &php_value(start + 1)?,
            &php_value(start + 2)?,
        ))?;
        Ok(from_php(&result))
    })())
}

pub(super) fn length<'a>(subject: &str, encoding: Option<&str>) -> Result<Value<'a>> {
    let len = match encoding {
        Some(encoding) => upstream(laravel::length_with(subject, encoding))?,
        None => laravel::length(subject),
    };
    Ok(Value::Int(len as i64))
}

pub(super) fn position<'a>(
    subject: &str,
    needle: &str,
    offset: i64,
    encoding: Option<&str>,
) -> Result<Value<'a>> {
    Ok(
        upstream(laravel::position_with(subject, needle, offset, encoding))?
            .map_or(Value::Bool(false), |index| Value::Int(index as i64)),
    )
}

pub(super) fn count<'a>(
    subject: &str,
    needle: &str,
    offset: i64,
    count: Option<i64>,
) -> Result<Value<'a>> {
    Ok(Value::Int(
        upstream(laravel::substr_count_with(subject, needle, offset, count))? as i64,
    ))
}

fn uuid_version(args: Args<'_, '_, '_>) -> Result<laravel::UuidVersion> {
    use laravel::UuidVersion;
    match string(args.value(0)?) {
        Some("nil") => Ok(UuidVersion::Nil),
        Some("max") => Ok(UuidVersion::Max),
        Some(_) => Err(args.invalid(0, "an integer UUID version, 'nil', or 'max'")),
        None => Ok(UuidVersion::Number(args.int(0)?)),
    }
}