Skip to content

Commit

Permalink
Add type notes to toml output
Browse files Browse the repository at this point in the history
  • Loading branch information
equation314 committed Dec 22, 2024
1 parent 15adaac commit ec28169
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions axconfig-gen-macros/tests/example_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
4 changes: 2 additions & 2 deletions axconfig-gen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions axconfig-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 24 additions & 10 deletions axconfig-gen/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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() {
Expand Down
23 changes: 23 additions & 0 deletions axconfig-gen/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,26 @@ fn split_tuple_items(s: &str) -> Option<Vec<&str>> {
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, "?"),
}
}
}
2 changes: 1 addition & 1 deletion example-configs/defconfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example-configs/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
40 changes: 20 additions & 20 deletions example-configs/output.toml
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
# 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"],
["0xfe00_0000", "0xc0_0000"],
["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

0 comments on commit ec28169

Please sign in to comment.