fn read_expr(stream: &mut Vec<Token>) -> Result<UnitExpr, ParseError> {
    if let Some(token) = stream.pop() {
        match token {
            Token::Value(unit) => {
                match CONVERSION_FACTORS.get(&*unit) {
                    Some(&value) => Ok(UnitExpr::Val(value)),
                    None => Err(ParseError::NotFound { unit: unit }),
                }
            }
            Token::Mul => {
                let rhs = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the right of '*': {}", err))
                })?;
                let lhs = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the left of '*': {}", err))
                })?;
                Ok(UnitExpr::Mul(Box::new(lhs), Box::new(rhs)))
            }
            Token::Div => {
                let rhs = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the right of '/': {}", err))
                })?;
                let lhs = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the left of '/': {}", err))
                })?;
                Ok(UnitExpr::Div(Box::new(lhs), Box::new(rhs)))
            }
            Token::Pow => {
                let pow = match stream.pop() {
                    Some(pow) => {
                        match pow {
                            Token::Value(value) => value.parse()?,
                            _ => {
                                return Err(ParseError::MalformedExpr(
                                    format!("Invalid value after ^: {}", pow.as_str()),
                                ))
                            }
                        }
                    }
                    None => {
                        return Err(
                            ParseError::MalformedExpr(String::from("Missing value after '^'")),
                        )
                    }
                };
                let expr = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the left of '*': {}", err))
                })?;
                Ok(UnitExpr::Pow(Box::new(expr), pow))
            }
            Token::LParen | Token::RParen => {
                unreachable!("there should not be any parenthese here")
            }
        }
    } else {
        Err(ParseError::MalformedExpr(String::from("missing a value")))
    }
}
/// Read and pop (recursively) a single expression from the `stream`.
/// The `stream` must be in reverse polish notation.
Read and pop (recursively) a single expression from the `stream`.
The `stream` must be in reverse polish notation.
Read and pop ( recursively ) a single expression from the ` stream ` . The ` stream ` must be in reverse polish notation .
fn read_expr(stream: &mut Vec<Token>) -> Result<UnitExpr, ParseError> {
    if let Some(token) = stream.pop() {
        match token {
            Token::Value(unit) => {
                match CONVERSION_FACTORS.get(&*unit) {
                    Some(&value) => Ok(UnitExpr::Val(value)),
                    None => Err(ParseError::NotFound { unit: unit }),
                }
            }
            Token::Mul => {
                let rhs = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the right of '*': {}", err))
                })?;
                let lhs = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the left of '*': {}", err))
                })?;
                Ok(UnitExpr::Mul(Box::new(lhs), Box::new(rhs)))
            }
            Token::Div => {
                let rhs = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the right of '/': {}", err))
                })?;
                let lhs = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the left of '/': {}", err))
                })?;
                Ok(UnitExpr::Div(Box::new(lhs), Box::new(rhs)))
            }
            Token::Pow => {
                let pow = match stream.pop() {
                    Some(pow) => {
                        match pow {
                            Token::Value(value) => value.parse()?,
                            _ => {
                                return Err(ParseError::MalformedExpr(
                                    format!("Invalid value after ^: {}", pow.as_str()),
                                ))
                            }
                        }
                    }
                    None => {
                        return Err(
                            ParseError::MalformedExpr(String::from("Missing value after '^'")),
                        )
                    }
                };
                let expr = read_expr(stream).map_err(|err| {
                    ParseError::MalformedExpr(format!("Error in unit at the left of '*': {}", err))
                })?;
                Ok(UnitExpr::Pow(Box::new(expr), pow))
            }
            Token::LParen | Token::RParen => {
                unreachable!("there should not be any parenthese here")
            }
        }
    } else {
        Err(ParseError::MalformedExpr(String::from("missing a value")))
    }
}
fn read_expr ( stream : & mut Vec < Token > ) -> Result < UnitExpr , ParseError > { if let Some ( token ) = stream . pop ( ) { match token { Token :: Value ( unit ) => { match CONVERSION_FACTORS . get ( & * unit ) { Some ( & value ) => Ok ( UnitExpr :: Val ( value ) ) , None => Err ( ParseError :: NotFound { unit : unit } ) , } } Token :: Mul => { let rhs = read_expr ( stream ) . map_err ( | err | { ParseError :: MalformedExpr ( format ! ( " " err ) ) } ) ? ; let lhs = read_expr ( stream ) . map_err ( | err | { ParseError :: MalformedExpr ( format ! ( " " err ) ) } ) ? ; Ok ( UnitExpr :: Mul ( Box :: new ( lhs ) , Box :: new ( rhs ) ) ) } Token :: Div => { let rhs = read_expr ( stream ) . map_err ( | err | { ParseError :: MalformedExpr ( format ! ( " " err ) ) } ) ? ; let lhs = read_expr ( stream ) . map_err ( | err | { ParseError :: MalformedExpr ( format ! ( " " err ) ) } ) ? ; Ok ( UnitExpr :: Div ( Box :: new ( lhs ) , Box :: new ( rhs ) ) ) } Token :: Pow => { let pow = match stream . pop ( ) { Some ( pow ) => { match pow { Token :: Value ( value ) => value . parse ( ) ? , _ => { return Err ( ParseError :: MalformedExpr ( format ! ( " " pow as_str ( ) ) , ) ) } } } None => { return Err ( ParseError :: MalformedExpr ( String :: from ( " " ) ) , ) } } ; let expr = read_expr ( stream ) . map_err ( | err | { ParseError :: MalformedExpr ( format ! ( " " err ) ) } ) ? ; Ok ( UnitExpr :: Pow ( Box :: new ( expr ) , pow ) ) } Token :: LParen | Token :: RParen => { unreachable ! ( " " ) } } } else { Err ( ParseError :: MalformedExpr ( String :: from ( " " ) ) ) } }
Read and pop (recursively) a single expression from the `stream`.
Read and pop ( recursively ) a single expression from the ` stream ` .
stream
&mut Vec<Token>