use fieldwork_borrow::with_;

struct Database {
    models: Vec<Model>,
}

struct Model {
    objects: Vec<Object>,
}

struct Object {
    matrix: f64,
}

struct Viewer {
    db: Database,
    model_id: usize,
    object_mats: Vec<f64>,
}

impl Viewer {
    fn problem(&mut self) {
        with_!(self, {
            methods {
                cur_model(&db, copy model_id) -> &Model {
                    &db.models[model_id]
                }
            }
            fields {
                mut object_mats
            }
            body {
                for obj in &self.cur_model().objects {
                    self.object_mats.push(obj.matrix);
                }
            }
        });
    }
}

#[test]
fn test_viewer() {
    let mut viewer = Viewer {
        db: Database {
            models: vec![Model {
                objects: vec![
                    Object { matrix: 1.0 },
                    Object { matrix: 2.0 },
                ],
            }],
        },
        model_id: 0,
        object_mats: vec![],
    };

    viewer.problem();

    assert_eq!(viewer.object_mats, vec![1.0, 2.0]);
}