use std::cell::RefCell;
use std::rc::Rc;

#[derive(Debug)]
enum Widget {
    Window { 
        title: String,
        children: RefCell<Vec<Rc<Widget>>>,
    },
    Button {
        label: String,
    },
}

impl Widget {
    fn add_child(parent: &Rc<Widget>, child: Rc<Widget>) {
        if let Widget::Window { children, .. } = parent.as_ref() {
            children.borrow_mut().push(child);
        }
    }
}

fn main() {
    let window = Rc::new(Widget::Window {
        title: "メイン".to_string(),
        children: RefCell::new(vec![]),
    });

    let ok = Rc::new(Widget::Button {
        label: "OK".to_string(),
    });

    let cancel = Rc::new(Widget::Button {
        label: "キャンセル".to_string(),
    });

    Widget::add_child(&window, ok);
    Widget::add_child(&window, cancel);

    println!("{:#?}", window);
}