From ec28169b2b692aa4f733045310b0d6e23deb5ac8 Mon Sep 17 00:00:00 2001 From: Yuekai Jia Date: Mon, 23 Dec 2024 01:32:56 +0800 Subject: [PATCH] Add type notes to toml output --- README.md | 4 +-- axconfig-gen-macros/tests/example_config.rs | 12 +++---- axconfig-gen/README.md | 4 +-- axconfig-gen/src/main.rs | 5 +++ axconfig-gen/src/output.rs | 34 ++++++++++++------ axconfig-gen/src/ty.rs | 23 ++++++++++++ example-configs/defconfig.toml | 2 +- example-configs/output.rs | 2 +- example-configs/output.toml | 40 ++++++++++----------- 9 files changed, 84 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 867aa37..07be40a 100644 --- a/README.md +++ b/README.md @@ -49,8 +49,8 @@ tuple = [1, "abc", 3] let config = Config::from_toml(config_toml).unwrap(); let rust_code = config.dump(OutputFormat::Rust).unwrap(); -assert_eq!(rust_code, r#" -pub const ARE_YOU_OK: bool = true; +assert_eq!(rust_code, +r#"pub const ARE_YOU_OK: bool = true; pub const ONE_TWO_THREE: usize = 123; pub mod hello { diff --git a/axconfig-gen-macros/tests/example_config.rs b/axconfig-gen-macros/tests/example_config.rs index 4a26630..fd90dc3 100644 --- a/axconfig-gen-macros/tests/example_config.rs +++ b/axconfig-gen-macros/tests/example_config.rs @@ -20,13 +20,13 @@ macro_rules! mod_cmp { assert_eq!($mod1::PLAT, $mod2::PLAT); assert_eq!($mod1::SMP, $mod2::SMP); - assert_eq!($mod1::device::MMIO_REGIONS, $mod2::device::MMIO_REGIONS); - assert_eq!($mod1::device::PCI_BUS_END, $mod2::device::PCI_BUS_END); - assert_eq!($mod1::device::PCI_ECAM_BASE, $mod2::device::PCI_ECAM_BASE); - assert_eq!($mod1::device::PCI_RANGES, $mod2::device::PCI_RANGES); + assert_eq!($mod1::devices::MMIO_REGIONS, $mod2::devices::MMIO_REGIONS); + assert_eq!($mod1::devices::PCI_BUS_END, $mod2::devices::PCI_BUS_END); + assert_eq!($mod1::devices::PCI_ECAM_BASE, $mod2::devices::PCI_ECAM_BASE); + assert_eq!($mod1::devices::PCI_RANGES, $mod2::devices::PCI_RANGES); assert_eq!( - $mod1::device::VIRTIO_MMIO_REGIONS, - $mod2::device::VIRTIO_MMIO_REGIONS + $mod1::devices::VIRTIO_MMIO_REGIONS, + $mod2::devices::VIRTIO_MMIO_REGIONS ); assert_eq!( diff --git a/axconfig-gen/README.md b/axconfig-gen/README.md index c7ac99e..7819de5 100644 --- a/axconfig-gen/README.md +++ b/axconfig-gen/README.md @@ -46,8 +46,8 @@ tuple = [1, "abc", 3] let config = Config::from_toml(config_toml).unwrap(); let rust_code = config.dump(OutputFormat::Rust).unwrap(); -assert_eq!(rust_code, r#" -pub const ARE_YOU_OK: bool = true; +assert_eq!(rust_code, +r#"pub const ARE_YOU_OK: bool = true; pub const ONE_TWO_THREE: usize = 123; pub mod hello { diff --git a/axconfig-gen/src/main.rs b/axconfig-gen/src/main.rs index 04a88ce..e20c562 100644 --- a/axconfig-gen/src/main.rs +++ b/axconfig-gen/src/main.rs @@ -118,6 +118,11 @@ fn main() -> io::Result<()> { let output = unwrap!(config.dump(args.fmt)); if let Some(path) = args.output { + if let Ok(oldconfig) = std::fs::read_to_string(&path) { + if oldconfig == output { + return Ok(()); + } + } std::fs::write(path, output)?; } else { println!("{}", output); diff --git a/axconfig-gen/src/output.rs b/axconfig-gen/src/output.rs index fa828d6..751d98f 100644 --- a/axconfig-gen/src/output.rs +++ b/axconfig-gen/src/output.rs @@ -59,15 +59,26 @@ impl Output { self.println_fmt(format_args!("{}", s)); } + pub fn print_lines(&mut self, s: &str, line_op: impl Fn(&str) -> String) { + for line in s.lines() { + let line = line_op(line); + if !line.is_empty() { + self.println(&line); + } + } + } + pub fn table_begin(&mut self, name: &str, comments: &str) { + if !self.result.is_empty() { + self.println(""); + } match self.fmt { OutputFormat::Toml => { - self.println_fmt(format_args!("{}[{}]", comments, name)); + self.print_lines(comments, |l| l.trim().into()); + self.println(&format!("[{}]", name)); } OutputFormat::Rust => { - for line in comments.lines() { - self.println(&line.replacen("#", "///", 1)); - } + self.print_lines(comments, |l| l.trim().replacen("#", "///", 1)); self.println_fmt(format_args!("pub mod {} {{", mod_name(name))); self.indent += 4; } @@ -84,17 +95,20 @@ impl Output { pub fn write_item(&mut self, item: &ConfigItem) -> ConfigResult<()> { match self.fmt { OutputFormat::Toml => { + self.print_lines(item.comments(), |l| l.trim().into()); self.println_fmt(format_args!( - "{}{} = {}", - item.comments(), + "{} = {}{}", item.key(), - item.value().to_toml_value() + item.value().to_toml_value(), + if let Some(ty) = item.value().ty() { + format!(" # {}", ty) + } else { + "".into() + }, )); } OutputFormat::Rust => { - for line in item.comments().lines() { - self.println(&line.replacen("#", "///", 1)); - } + self.print_lines(item.comments(), |l| l.trim().replacen("#", "///", 1)); let key = const_name(item.key()); let val = item.value(); let ty = if let Some(ty) = val.ty() { diff --git a/axconfig-gen/src/ty.rs b/axconfig-gen/src/ty.rs index 75845e4..9ed2aaf 100644 --- a/axconfig-gen/src/ty.rs +++ b/axconfig-gen/src/ty.rs @@ -109,3 +109,26 @@ fn split_tuple_items(s: &str) -> Option> { None } } + +impl std::fmt::Display for ConfigType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Bool => write!(f, "bool"), + Self::Int => write!(f, "int"), + Self::Uint => write!(f, "uint"), + Self::String => write!(f, "str"), + Self::Tuple(items) => { + write!(f, "(")?; + for (i, item) in items.iter().enumerate() { + if i > 0 { + write!(f, ", ")?; + } + write!(f, "{}", item)?; + } + write!(f, ")") + } + Self::Array(ty) => write!(f, "[{}]", ty), + Self::Unknown => write!(f, "?"), + } + } +} diff --git a/example-configs/defconfig.toml b/example-configs/defconfig.toml index fdcc859..be2c494 100644 --- a/example-configs/defconfig.toml +++ b/example-configs/defconfig.toml @@ -43,7 +43,7 @@ timer-frequency = 0 # uint # # Device specifications # -[device] +[devices] # MMIO regions with format (`base_paddr`, `size`). mmio-regions = [ ["0xb000_0000", "0x1000_0000"], # PCI config space diff --git a/example-configs/output.rs b/example-configs/output.rs index 175bfe2..7a13ecc 100644 --- a/example-configs/output.rs +++ b/example-configs/output.rs @@ -8,7 +8,7 @@ pub const SMP: usize = 1; /// /// Device specifications /// -pub mod device { +pub mod devices { /// MMIO regions with format (`base_paddr`, `size`). pub const MMIO_REGIONS: &[(usize, usize)] = &[ (0xb000_0000, 0x1000_0000), diff --git a/example-configs/output.toml b/example-configs/output.toml index 144ebe0..f32d771 100644 --- a/example-configs/output.toml +++ b/example-configs/output.toml @@ -1,14 +1,14 @@ # Architecture identifier. -arch = "x86_64" +arch = "x86_64" # str # Platform identifier. -plat = "x86_64-qemu-q35" +plat = "x86_64-qemu-q35" # str # Number of CPUs. -smp = 1 +smp = 1 # uint # # Device specifications # -[device] +[devices] # MMIO regions with format (`base_paddr`, `size`). mmio-regions = [ ["0xb000_0000", "0x1000_0000"], @@ -16,47 +16,47 @@ mmio-regions = [ ["0xfec0_0000", "0x1000"], ["0xfed0_0000", "0x1000"], ["0xfee0_0000", "0x1000"] -] +] # [(uint, uint)] # End PCI bus number. -pci-bus-end = 0 +pci-bus-end = 0 # uint # Base physical address of the PCIe ECAM space (should read from ACPI 'MCFG' table). -pci-ecam-base = 0 +pci-ecam-base = 0 # uint # PCI device memory ranges (not used on x86). -pci-ranges = [] +pci-ranges = [] # [(uint, uint)] # VirtIO MMIO regions with format (`base_paddr`, `size`). -virtio-mmio-regions = [] +virtio-mmio-regions = [] # [(uint, uint)] # # Kernel configs # [kernel] # Stack size of each task. -task-stack-size = 0 +task-stack-size = 0 # uint # Number of timer ticks per second (Hz). A timer tick may contain several timer # interrupts. -ticks-per-sec = 0 +ticks-per-sec = 0 # uint # # Platform configs # [platform] # Kernel address space base. -kernel-aspace-base = "0xffff_ff80_0000_0000" +kernel-aspace-base = "0xffff_ff80_0000_0000" # uint # Kernel address space size. -kernel-aspace-size = "0x0000_007f_ffff_f000" +kernel-aspace-size = "0x0000_007f_ffff_f000" # uint # Base physical address of the kernel image. -kernel-base-paddr = 0x20_0000 +kernel-base-paddr = 0x20_0000 # uint # Base virtual address of the kernel image. -kernel-base-vaddr = "0xffff_ff80_0020_0000" +kernel-base-vaddr = "0xffff_ff80_0020_0000" # uint # Offset of bus address and phys address. some boards, the bus address is # different from the physical address. -phys-bus-offset = 0 +phys-bus-offset = 0 # uint # Base address of the whole physical memory. -phys-memory-base = 0 +phys-memory-base = 0 # uint # Size of the whole physical memory. -phys-memory-size = 0x800_0000 +phys-memory-size = 0x800_0000 # uint # Linear mapping offset, for quick conversions between physical and virtual # addresses. -phys-virt-offset = "0xffff_ff80_0000_0000" +phys-virt-offset = "0xffff_ff80_0000_0000" # uint # Timer interrupt frequencyin Hz. -timer-frequency = 0 +timer-frequency = 0 # uint