Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ufo::new_layer(name) method #84

Closed
madig opened this issue Feb 15, 2021 · 2 comments · Fixed by #106
Closed

Add Ufo::new_layer(name) method #84

madig opened this issue Feb 15, 2021 · 2 comments · Fixed by #106

Comments

@madig
Copy link
Collaborator

madig commented Feb 15, 2021

Should insert an empty new named layer and return a mutable reference to it. UX problem: what if your chosen layer name exists already? Return Result<Error, &mut Layer>?

Maybe even a combination method like get_or_insert_new(name)? Or something mirroring https://doc.rust-lang.org/std/option/enum.Option.html#method.get_or_insert. Use case is mutably grabbing e.g. the public.background layer or creating it if it doesn't exist yet. For when you just want to add stuff into it.

@cmyr
Copy link
Member

cmyr commented Feb 17, 2021

When we create a new UFO we always add public.background, I think.

I guess I Would want to think about this from an actual user's perspective: what are they trying to achieve? I don't use layers in norad right now, so I'm not sure. The problem with get_or_insert is that you need to pass an owned key to the getter every time you want to get the layer, which might be annoying?

I think that a simple and reasonable approach might be to just expect the caller to check if a given layer exists, and create it if not; this will not be heavily used API, so I don't think we should get too fancy. I think that returning an error if the layer already exists is a simple and correct solution. 🤷

@madig
Copy link
Collaborator Author

madig commented Apr 7, 2021

We only create the default layer.

I just hit this again where I iterate over a data source and sometimes write a background layer for a glyph. The code now looks like this:

if let Some(parent_name) = layer.name.strip_prefix("mask.") {
    if let Some(drawn_instance) = drawn_instances.get_mut(parent_name) {
        match drawn_instance.find_layer_mut(|l| l.name == "public.background") {
            // If we already have a background layer...
            Some(background) => // ...,
            // Otherwise make a new one and insert the glyph.
            None => {
                // creation dance...
            }
        };
    }
} else {
    // ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants