expression

Validate external records before creating domain values

An adapter at an input boundary should reject values outside the domain rather than silently truncating them. TryFrom represents a consuming conversion that may fail. Here the input is a synthetic percentage and the domain accepts only 0 through 100. No real external service is contacted. Expected result (NOT RUN): valid: 75 rejected: true Common mistake: Using a narrowing cast before checking the range. Static Rust example; not a verified ByteMind executable assessment.

Formal Syntax & Structure

rust syntaxrust
impl TryFrom<External> for Reading

Working Example

Example Snippetrust
struct External(i32);struct Reading(u8);impl TryFrom<External> for Reading {    type Error = &'static str;    fn try_from(value: External) -> Result<Self, Self::Error> {        if !(0..=100).contains(&value.0) { return Err("outside percentage range"); }        Ok(Self(value.0 as u8))    }}fn main() {    println!("valid: {}", Reading::try_from(External(75)).unwrap().0);    println!("rejected: {}", Reading::try_from(External(101)).is_err());}
Rust Reference