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

Make it easier to update multiple gist files #692

Open
mkrasnitski opened this issue Sep 9, 2024 · 0 comments
Open

Make it easier to update multiple gist files #692

mkrasnitski opened this issue Sep 9, 2024 · 0 comments

Comments

@mkrasnitski
Copy link
Contributor

mkrasnitski commented Sep 9, 2024

The UpdateGistBuilder API has a pain point if you want to edit multiple files and don't know the quantity of files to edit until runtime. As an example, if we have a Vec<(String, String)> containing pairs of filenames and contents to update the gist with, the best way I found to create a builder is the following:

let builder = crab.gists().update(id);

let data: Vec<(String, String)> = todo!();

let iter = data.into_iter();
let mut builder_with_files = None;
if let Some((file, content)) = iter.next() {
    builder_with_files = Some(builder.file(file).with_content(content));
}

while let Some((file, content)) = iter.next() {
    builder_with_files = Some(builder_with_files.unwrap().file(file).with_content(content));
}

// Manual .map() due to async-ness
let gist = match builder_with_files {
    Some(builder) => Some(builder.send().await?),
    None => None,
};

This is really ugly IMO and duplicates code more than it should. The problem here is that adding the first file is a type transformation of UpdateGistBuilder -> UpdateGistFileBuilder, whereas adding any subsequent files operates only on a UpdateGistFileBuilder. One way to fix this would require adding a way to obtain the original UpdateGistBuilder after adding a file, potentially by making UpdateGistFileBuilder::build public. However, there may also be other valid approaches.

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

No branches or pull requests

1 participant