Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. fn main() {
  2. // My quick & bad representation of excel rows
  3. let sce = vec![String::from("ItemTypeA"), String::from("1"),
  4. String::from("Name"), String::from("Foo"),
  5. String::from("PropertyA"), String::from("foo"),
  6. String::from("PropertyB"), String::from("bar"),
  7. String::from("ItemTypeB"), String::from("2"),
  8. String::from("Name"), String::from("Foo"),
  9. String::from("PropertyB"), String::from("foo")];
  10. // Others elements in a list
  11.  
  12. let catalog = Catalog::new(sce);
  13. catalog.parser();
  14. }
  15.  
  16. pub struct Catalog {
  17. range: Vec<String>,
  18. }
  19.  
  20. impl Catalog {
  21. pub fn new(range: Vec<String>) -> Self {
  22. Catalog { range }
  23. }
  24.  
  25. pub fn parser(&self) {
  26. let mut rows = self.range.iter();
  27. while let Some(row) = rows.next() {
  28. if row == "ItemTypeA" {
  29. // if I do rows.next() here, the compiler is happy
  30. // if I call it inside of ItemTypeA::new I cannot find any way to make the compiler happy
  31.  
  32. // /!\ Uncomment this line (invert the comment with the next ones)
  33. //items::ItemTypeA::new(row.to_string(), rows);
  34. let id = rows.next().unwrap(); // Get the id (it works here)
  35. items::ItemTypeA::new(id.to_string());
  36.  
  37. } else if row == "ItemTypeB" {
  38. // Do the same but on the B item type
  39. } else {
  40. eprintln!("Skipping unknown item type: {}", row);
  41. }
  42. }
  43. }
  44. }
  45.  
  46. pub mod items {
  47. pub struct ItemTypeA {
  48. id: i64,
  49. name: String,
  50. property_a: String,
  51. property_z: String,
  52. }
  53.  
  54. impl ItemTypeA {
  55. // Which types should be written there ?
  56.  
  57. // /!\ Uncomment this line (invert the comment with the next one)
  58. //pub fn new(row: String, rows: std::slice::Iter<String>) -> Self {
  59. pub fn new(row: String) -> Self {
  60. //// rows.next(); // trigger a compile time error.
  61. let id: i64 = row.parse().unwrap();
  62. // /!\ Uncomment this lines
  63. //rows.next(); // discard first element for this bad example
  64. //let name = rows.next().unwrap();
  65. //rows.next(); // discard first element for this bad example
  66. //let propertyA = rows.next().unwrap();
  67. //rows.next(); // discard first element for this bad example
  68. //let propertyZ = rows.next().unwrap();
  69.  
  70. ItemTypeA {
  71. id,
  72. name: "name".to_string(), // : name.to_string(),
  73. property_a: "propertyA".to_string(), // : propertyA.to_string(),
  74. property_z: "propertyZ".to_string(), // : propertyZ.to_string(),
  75. }
  76. }
  77. }
  78.  
  79. // impl the same for ItemTypeB
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement