Skip to main content

set_messages

Function set_messages 

Source
pub fn set_messages(messages: &'static dyn Messages)
Available on crate features json or toml or yaml only.
Expand description

Set the wording the process uses for a Refusal’s text and for every problem an Input reports. Without a call, the process uses EnglishMessages.

The wording changes the text and never the fault or where it is. The wording is global to the process, so this doc-test, which runs in a process of its own, is the one place that sets it.

use ctl_input::format::{Fault, Refusal};
use ctl_input::{EnglishMessages, Input, Limits, Messages, set_messages};
use serde::de::IgnoredAny;

struct Shout;

impl Messages for Shout {
    fn message(&self, fault: &Fault) -> String {
        EnglishMessages.message(fault).to_uppercase()
    }
}

static SHOUT: Shout = Shout;

let refusal = Refusal::new(Fault::MissingField { field: "port" }, Some(3..7), None);
assert_eq!(refusal.to_string(), "missing field `port`");
set_messages(&SHOUT);
assert_eq!(refusal.to_string(), "MISSING FIELD `PORT`");
assert_eq!(refusal.fault(), &Fault::MissingField { field: "port" });
assert_eq!(refusal.span(), Some(3..7));

let limits = Limits {
    max_reader_input_bytes: Some(2),
    ..Limits::NONE
};
let error = Input::new("a.yaml", "a: 1\n")
    .limits(limits)
    .parse::<IgnoredAny>()
    .unwrap_err();
assert_eq!(
    error.to_string(),
    "a.yaml: IS 5 BYTES, WHICH EXCEEDS MAX_READER_INPUT_BYTES (2); \
     RAISE LIMITS.MAX_READER_INPUT_BYTES IN .CTL/INPUT.YAML"
);