Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- // My quick & bad representation of excel rows
- let sce = vec![String::from("ItemTypeA"), String::from("1"),
- String::from("Name"), String::from("Foo"),
- String::from("PropertyA"), String::from("foo"),
- String::from("PropertyB"), String::from("bar"),
- String::from("ItemTypeB"), String::from("2"),
- String::from("Name"), String::from("Foo"),
- String::from("PropertyB"), String::from("foo")];
- // Others elements in a list
- let catalog = Catalog::new(sce);
- catalog.parser();
- }
- pub struct Catalog {
- range: Vec<String>,
- }
- impl Catalog {
- pub fn new(range: Vec<String>) -> Self {
- Catalog { range }
- }
- pub fn parser(&self) {
- let mut rows = self.range.iter();
- while let Some(row) = rows.next() {
- if row == "ItemTypeA" {
- // if I do rows.next() here, the compiler is happy
- // if I call it inside of ItemTypeA::new I cannot find any way to make the compiler happy
- // /!\ Uncomment this line (invert the comment with the next ones)
- //items::ItemTypeA::new(row.to_string(), rows);
- let id = rows.next().unwrap(); // Get the id (it works here)
- items::ItemTypeA::new(id.to_string());
- } else if row == "ItemTypeB" {
- // Do the same but on the B item type
- } else {
- eprintln!("Skipping unknown item type: {}", row);
- }
- }
- }
- }
- pub mod items {
- pub struct ItemTypeA {
- id: i64,
- name: String,
- property_a: String,
- property_z: String,
- }
- impl ItemTypeA {
- // Which types should be written there ?
- // /!\ Uncomment this line (invert the comment with the next one)
- //pub fn new(row: String, rows: std::slice::Iter<String>) -> Self {
- pub fn new(row: String) -> Self {
- //// rows.next(); // trigger a compile time error.
- let id: i64 = row.parse().unwrap();
- // /!\ Uncomment this lines
- //rows.next(); // discard first element for this bad example
- //let name = rows.next().unwrap();
- //rows.next(); // discard first element for this bad example
- //let propertyA = rows.next().unwrap();
- //rows.next(); // discard first element for this bad example
- //let propertyZ = rows.next().unwrap();
- ItemTypeA {
- id,
- name: "name".to_string(), // : name.to_string(),
- property_a: "propertyA".to_string(), // : propertyA.to_string(),
- property_z: "propertyZ".to_string(), // : propertyZ.to_string(),
- }
- }
- }
- // impl the same for ItemTypeB
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement