From 33547c89659cae75ef3521ac64955ebc27255186 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Sun, 28 Sep 2025 20:01:26 +0200 Subject: [PATCH] Switch to Rust. --- .gitignore | 3 +- Cargo.lock | 7 ++++ Cargo.toml | 6 ++++ out.txt | Bin 0 -> 1072 bytes src/main.rs | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 out.txt create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore index f577998..ea8c4bf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -/dbus-prettifier -*.o +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..029d755 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "dbus-prettifier" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..df85c65 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "dbus-prettifier" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/out.txt b/out.txt new file mode 100644 index 0000000000000000000000000000000000000000..9dc1b4214d75225b987787cf2d7fdddb0f9d6059 GIT binary patch literal 1072 zcma#%Qcws>EXqvGE71sasw_!$EGkN@)U;Bl21+O>n3(H;X+r}87{d_CFtmU$;7SZl gVM?HCEnzAwArb~aqiVTgxfn*lXb6mkz>o|90M%9%b^rhX literal 0 HcmV?d00001 diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..15e7a04 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2025 Nicolás Ortega Froysa All rights reserved. + * Author: Nicolás Ortega Froysa + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. + */ + +use std::env; +use std::fs; +use std::io; +use std::io::Read; +use std::io::Write; +use std::process; + +// Read 1KiB of the file at a time. +static BLOCK_SIZE:usize = 1024; + +fn main() -> io::Result<()> { + let args:Vec = env::args().collect(); + + if args.len() != 3 { + eprintln!("Usage: {} ", args[0]); + process::exit(1); + } + + let in_file = fs::File::open(&args[1])?; + let mut in_reader = io::BufReader::new(in_file); + + let out_file = fs::File::create(&args[2])?; + let mut out_writer = io::BufWriter::new(out_file); + + let mut read_buf:[u8; BLOCK_SIZE] = [0; BLOCK_SIZE]; + let mut last_ch:char = '\0'; + let mut tab_num = 0; + + while in_reader.read(&mut read_buf)? > 0 { + for i in read_buf { + let ch = i as char; + match ch { + '[' | '{' => { + tab_num += 1; + out_writer.write(format!("{}\n", ch).as_bytes())?; + for _ in 0..tab_num { + out_writer.write(b" ")?; + } + last_ch = ' '; + }, + ']' | '}' => { + tab_num -= 1; + out_writer.write(b"\n")?; + for _ in 0..tab_num { + out_writer.write(b" ")?; + } + out_writer.write(format!("{}", ch).as_bytes())?; + last_ch = ch; + }, + ',' => { + out_writer.write(format!("{}\n", ch).as_bytes())?; + for _ in 0..tab_num { + out_writer.write(b" ")?; + } + last_ch = ' '; + }, + _ => { + let ch = ch; + if ch != ' ' || last_ch != ' ' { + out_writer.write(format!("{}", ch).as_bytes())?; + last_ch = ch; + } + }, + } + } + } + + Ok(()) +}