Skip to content

Commit 24825fd

Browse files
committed
refactor: remove PromptNew trait and complete Prompt proc macro
1 parent b9b4d71 commit 24825fd

File tree

39 files changed

+174
-161
lines changed

39 files changed

+174
-161
lines changed

goldboot-image/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ impl ImageHandle {
459459
pub fn change_password(&self, _old_password: String, new_password: String) -> Result<()> {
460460
// Create the cipher and a RNG for the nonces
461461
let _cipher = new_key(new_password);
462-
let _rng = rand::thread_rng();
463462

464463
todo!()
465464
}

goldboot-image/src/qcow/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use anyhow::{bail, Result};
2-
use binrw::{io::SeekFrom, BinRead, BinReaderExt};
1+
use anyhow::{Result, bail};
2+
use binrw::{BinRead, BinReaderExt, io::SeekFrom};
33
use snapshot::Snapshot;
44
use std::{
55
fs::File,

goldboot-linux/src/gui/select_image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use goldboot::library::ImageLibrary;
44
use goldboot_image::ImageHandle;
55
use gtk::glib;
66
use gtk4 as gtk;
7-
use gtk4::{prelude::*, EventControllerKey};
7+
use gtk4::{EventControllerKey, prelude::*};
88
use tracing::info;
99
use ubyte::ToByteUnit;
1010

goldboot-macros/src/lib.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use proc_macro::TokenStream;
22
use quote::quote;
3-
use syn::{self, DataStruct};
3+
use syn::{self};
44

55
/// Automatically implement "Prompt" for all fields in a struct.
66
#[proc_macro_derive(Prompt)]
@@ -11,30 +11,70 @@ pub fn prompt(input: TokenStream) -> TokenStream {
1111

1212
fn impl_prompt(ast: &syn::DeriveInput) -> TokenStream {
1313
let name = &ast.ident;
14-
let fields: Vec<String> = match &ast.data {
14+
15+
let fields_to_prompt: Vec<_> = match &ast.data {
1516
syn::Data::Struct(data) => match &data.fields {
1617
syn::Fields::Named(fields) => fields
1718
.named
1819
.iter()
19-
.map(|f| f.ident.clone().unwrap().to_string())
20+
.filter_map(|f| {
21+
// Skip fields with #[serde(flatten)]
22+
let is_flattened = f.attrs.iter().any(|attr| {
23+
attr.path().is_ident("serde")
24+
&& attr
25+
.parse_args::<syn::Ident>()
26+
.map(|i| i == "flatten")
27+
.unwrap_or(false)
28+
});
29+
30+
if is_flattened {
31+
None
32+
} else {
33+
Some((f.ident.clone().unwrap(), f.ty.clone()))
34+
}
35+
})
2036
.collect(),
21-
_ => panic!(),
37+
_ => panic!("Prompt derive only works on structs with named fields"),
2238
},
23-
_ => panic!(),
39+
_ => panic!("Prompt derive only works on structs"),
2440
};
2541

42+
let prompt_calls = fields_to_prompt.iter().map(|(field, ty)| {
43+
// Check if the type is an Option
44+
if is_option_type(ty) {
45+
quote! {
46+
if let Some(ref mut value) = self.#field {
47+
value.prompt(foundry)?;
48+
}
49+
}
50+
} else {
51+
quote! {
52+
self.#field.prompt(foundry)?;
53+
}
54+
}
55+
});
56+
2657
let syntax = quote! {
27-
impl Prompt for #name {
58+
impl crate::cli::prompt::Prompt for #name {
2859
fn prompt(
2960
&mut self,
30-
_: &Foundry,
31-
theme: impl dialoguer::theme::Theme,
61+
foundry: &crate::foundry::Foundry,
3262
) -> anyhow::Result<()> {
33-
todo!()
63+
#(#prompt_calls)*
64+
Ok(())
3465
}
3566
}
3667
};
3768
syntax.into()
3869
}
3970

71+
fn is_option_type(ty: &syn::Type) -> bool {
72+
if let syn::Type::Path(type_path) = ty {
73+
if let Some(segment) = type_path.path.segments.last() {
74+
return segment.ident == "Option";
75+
}
76+
}
77+
false
78+
}
79+
4080
// TODO probably need a macro for ImageMold and Fabricator

goldboot-registry/src/api/image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::extract::ImageHandle;
22
use axum::{
3+
Json,
34
extract::{Path, State},
45
http::StatusCode,
5-
Json,
66
};
77
use goldboot::registry::api::image::ImageInfoResponse;
88

goldboot-registry/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::cmd::Commands;
2-
use axum::{routing::get, Router};
2+
use axum::{Router, routing::get};
33
use clap::Parser;
44
use std::{env, process::ExitCode};
55

goldboot/src/cli/cmd/deploy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use console::Style;
2-
use dialoguer::{theme::ColorfulTheme, Confirm};
2+
use dialoguer::{Confirm, theme::ColorfulTheme};
33
use goldboot_image::ImageHandle;
44
use std::{path::Path, process::ExitCode};
55
use tracing::error;

goldboot/src/cli/cmd/image.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ pub fn run(cmd: super::Commands) -> ExitCode {
1111
super::ImageCommands::List {} => {
1212
let images = ImageLibrary::find_all().unwrap();
1313

14-
println!("Image Name Image Size Build Date Image ID Description");
14+
println!(
15+
"Image Name Image Size Build Date Image ID Description"
16+
);
1517
for image in images {
1618
println!(
1719
"{:15} {:12} {:31} {:12} {}",

goldboot/src/cli/cmd/init.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ fn print_banner() {
2626
}
2727
}
2828

29+
/// Get the current theme for prompts.
30+
pub fn theme() -> ColorfulTheme {
31+
ColorfulTheme {
32+
values_style: Style::new().yellow().dim(),
33+
..ColorfulTheme::default()
34+
}
35+
}
36+
2937
pub fn run(cmd: super::Commands) -> ExitCode {
3038
match cmd {
3139
super::Commands::Init {
@@ -75,10 +83,7 @@ pub fn run(cmd: super::Commands) -> ExitCode {
7583
// If no OS was given, begin interactive config
7684
print_banner();
7785

78-
let theme = ColorfulTheme {
79-
values_style: Style::new().yellow().dim(),
80-
..ColorfulTheme::default()
81-
};
86+
let theme = theme();
8287

8388
println!("Get ready to create a new image configuration!");
8489
println!("(it can be further edited later)");
@@ -160,8 +165,7 @@ pub fn run(cmd: super::Commands) -> ExitCode {
160165
.unwrap()
161166
{
162167
// TODO show some kind of banner
163-
os.prompt(&foundry, Box::new(ColorfulTheme::default()))
164-
.unwrap();
168+
os.prompt(&foundry).unwrap();
165169
}
166170

167171
if let Ok(source) = os.default_source(foundry.arch) {

goldboot/src/cli/cmd/liveusb.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use console::Style;
2-
use dialoguer::{theme::ColorfulTheme, Confirm};
3-
use goldboot_image::ImageHandle;
2+
use dialoguer::{Confirm, theme::ColorfulTheme};
43
use std::{path::Path, process::ExitCode};
54
use tracing::error;
65

0 commit comments

Comments
 (0)