/*
Import Traits
*/
use std::error::Error;
const emptyString:&str = "";
const isKJV_false:bool = false;
/*
Declare structure Congregant
*/
#[derive(Debug)]
struct Congregant
{
name: String
, bible: String
, isKJV: bool
, err:String
}
/*
listView
*/
fn listView(list_of_Congregant:Vec<Congregant>)
{
for (i, x) in list_of_Congregant.iter().enumerate()
{
println!
(
"In position {:0?} we have value {:0?}"
, i
, x
);
}
}
/*
listEvaluate
*/
fn listEvaluate
(
list_of_Congregant:&mut Vec<Congregant>
)
{
/*
Declare function return variable
*/
let mut isKJV_context:Result<bool, Box<dyn Error>>;
let mut isKJV:bool = false;
let mut bibleNameLocal:String;
let mut errLocal:String;
/*
Iterate list
Mutatable List
iter() versus iter_mut()
*/
for x in list_of_Congregant.iter_mut()
{
//reset context variables
//set local error
errLocal = "".to_string();
//get a copy of bible name
//bibleNameLocal = x.bible;
bibleNameLocal = x.bible.to_owned();
// determine if bible is KJV?
isKJV_context = bibleIsKJV(bibleNameLocal);
/*
If return variable is OK, then it is usable
*/
if isKJV_context.is_ok()
{
// Consume the result and return the contents with `unwrap`.
isKJV = isKJV_context.unwrap();
}
else if isKJV_context.is_err()
{
isKJV = false;
//unwrap Error
let err = isKJV_context.unwrap_err();
//save errorLocal
errLocal = err.to_string()
}
//save test
x.isKJV = isKJV;
//save exception
x.err = errLocal;
} //for
}
fn bibleIsKJV
(
bibleName:String
) -> Result<bool, Box<dyn Error>>
{
let errorMessage:String;
/*
If bible name is not KJV, raise exception
*/
if bibleName != "KJV"
{
errorMessage = format!
(
"Bible is not KJV. It is '{0}'"
, bibleName
);
//raise informative error
//convert string to err type
return Err
(
errorMessage.into()
);
}
//return OK and true (as result)
return Ok(true);
}
fn main()
{
//declare list
let mut list_of_Congregant: Vec<Congregant>;
let mut name:String;
let mut bible:String;
let mut emptyStringLocal:String;
//initialize empty vector
list_of_Congregant = vec![];
// Create struct with field init shorthand
name = String::from("Peter Johnson");
bible = String::from("KJV");
emptyStringLocal = emptyString.to_owned();
//Initialize structure
let peterJohnson = Congregant
{
name: name
, bible : bible
, isKJV : isKJV_false
, err: emptyStringLocal
};
//Add structure to list
list_of_Congregant.push(peterJohnson);
// Create struct with field init shorthand
name = String::from("Sylvia Brown");
bible = String::from("Passion Edition");
emptyStringLocal = emptyString.to_owned();
//Initialize structure
let sylviaBrown = Congregant
{
name: name
, bible : bible
, isKJV : isKJV_false
, err: emptyStringLocal
};
//Add structure to list
list_of_Congregant.push(sylviaBrown);
//evaluate list of congregants
listEvaluate
(
&mut list_of_Congregant
);
//list congregants
listView
(
list_of_Congregant
);
}