Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# The image source is available at https://github.com/amredev/imaginarium
FROM ghcr.io/amredev/devcontainer-decondenser:0.6.0
FROM ghcr.io/amredev/devcontainer-decondenser:0.6.1

RUN ln -s "$CONTAINER_WORKSPACE/.devcontainer/init.sh" "$HOME/devcontainer-hooks/init.sh"
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
// Use a system-wide target directory dedicated for rust-analyzer.
// This way RA never blocks builds by locking the local target directory.
"rust-analyzer.cargo.targetDir": "/home/amredev/rust-analyzer-cache",
"rust-analyzer.imports.granularity.group": "crate",
"rust-analyzer.imports.granularity.group": "module",
"rust-analyzer.assist.preferSelf": true,
"rust-analyzer.imports.prefix": "self",

Expand Down
45 changes: 29 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ clap = { version = "4.5", default-features = false }
codespan-reporting = { version = "0.12", default-features = false }
hashlink = "0.10"
marked-yaml = "0.8"
toml_edit = { version = "0.22", default-features = false }
toml_edit = { version = "0.23", default-features = false }
unicode-width = { version = "0.2.1", default-features = false }
yaml-rust2 = "0.10"

Expand Down Expand Up @@ -58,6 +58,13 @@ type_complexity = "allow"
# Too subjective. A long function is not always a bad function.
cognitive_complexity = "allow"

# Too intrusive. Additional `Default` trait impl is not always needed.
new_without_default = "allow"

# Too intrusive. Much of our code has no side-effects, but slapping
# `#[must_use]` on every function is just too noisy for too little benefit.
must_use_candidate = "allow"

# Turn on lints that are allow by default in `clippy`.
#
# We set them to a `warn` level instead of `deny` to prevent clippy from exiting
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
- Preserve the whitespace by default
- Try using `NonZero` for more type safety?
33 changes: 13 additions & 20 deletions decondenser-cli/src/config/deser.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::{Config, Escape, Formatting, Group, Lang, Punct, Quote, Space};
use super::{Common, Config, Group, Lang, Punct, Quote, Space};
use crate::yaml::{self, Deserialize, Node, NodeExt, Object, Result};
use decondenser::BreakStyle;

impl Deserialize for Config {
fn deserialize(value: Node) -> Result<Self> {
value.object(|obj| Self {
formatting: Formatting::flattened(obj),
common: Common::flattened(obj),
langs: obj.optional("lang").unwrap_or_default(),
debug_layout: obj.optional("debug_layout").unwrap_or_default(),
debug_indent: obj.optional("debug_indent").unwrap_or_default(),
Expand All @@ -16,21 +16,20 @@ impl Deserialize for Config {
impl Deserialize for Lang {
fn deserialize(value: Node) -> Result<Self> {
value.object(|table| Self {
formatting: Formatting::flattened(table),
common: Common::flattened(table),
groups: table.optional("groups"),
quotes: table.optional("quotes"),
puncts: table.optional("puncts"),
})
}
}

impl Formatting {
impl Common {
fn flattened(table: &mut Object) -> Self {
Self {
indent: table.optional("indent"),
max_line_size: table.optional("max_line_size"),
no_break_size: table.optional("no_break_size"),
preserve_newlines: table.optional("preserve_newlines"),
}
}
}
Expand Down Expand Up @@ -107,28 +106,22 @@ impl Deserialize for Space {

struct YamlBreakStyle(BreakStyle);

yaml::impl_deserialize_for_foreign_enum! {
YamlBreakStyle(BreakStyle {
Consistent => "consistent",
Compact => "compact",
})
impl Deserialize for YamlBreakStyle {
fn deserialize(value: Node) -> Result<Self> {
value
.enumeration(&[
("consistent", BreakStyle::consistent),
("compact", BreakStyle::compact),
])
.map(Self)
}
}

impl Deserialize for Quote {
fn deserialize(value: Node) -> Result<Self> {
value.object(|obj| Self {
opening: obj.required("opening"),
closing: obj.required("closing"),
escapes: obj.optional("escapes"),
})
}
}

impl Deserialize for Escape {
fn deserialize(value: Node) -> Result<Self> {
value.object(|obj| Self {
escaped: obj.required("escaped"),
unescaped: obj.required("unescaped"),
})
}
}
45 changes: 11 additions & 34 deletions decondenser-cli/src/config/into_core.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Config, Escape, Formatting, Group, Lang, Punct, Quote, Space};
use super::{Common, Config, Group, Lang, Punct, Quote, Space};
use crate::Result;
use decondenser::Decondenser;
use std::collections::{BTreeMap, BTreeSet};
Expand All @@ -8,7 +8,7 @@ const BUILTIN_LANGS: &[(&str, fn() -> Decondenser)] = &[("generic", Decondenser:
impl Config {
pub(crate) fn into_decondenser(self, lang: &str) -> Result<Decondenser> {
let Self {
formatting,
common: formatting,
mut langs,
debug_layout,
debug_indent,
Expand Down Expand Up @@ -53,13 +53,12 @@ impl Config {
}
}

impl Formatting {
impl Common {
fn apply(self, mut decondenser: Decondenser) -> Decondenser {
let Self {
indent,
max_line_size,
no_break_size,
preserve_newlines,
} = self;

if let Some(indent) = indent {
Expand All @@ -74,18 +73,14 @@ impl Formatting {
decondenser = decondenser.no_break_size(no_break_size);
}

if let Some(preserve_newlines) = preserve_newlines {
decondenser = decondenser.preserve_newlines(preserve_newlines);
}

decondenser
}
}

impl Lang {
fn apply(self, mut decondenser: Decondenser) -> Decondenser {
let Self {
formatting,
common: formatting,
groups,
quotes,
puncts,
Expand Down Expand Up @@ -151,13 +146,13 @@ impl Punct {

impl Space {
fn into_core(self) -> decondenser::Space {
let Self { size, breakable } = self;
let mut space = decondenser::Space::new();

let mut space = size
.map(decondenser::Space::fixed)
.unwrap_or_else(decondenser::Space::preserving);
if let Some(size) = self.size {
space = space.size(size);
}

if let Some(breakable) = breakable {
if let Some(breakable) = self.breakable {
space = space.breakable(breakable);
}

Expand All @@ -167,25 +162,7 @@ impl Space {

impl Quote {
fn into_core(self) -> decondenser::Quote {
let Self {
opening,
closing,
escapes,
} = self;

let mut quote = decondenser::Quote::new(opening, closing);

if let Some(escapes) = escapes {
quote = quote.escapes(escapes.into_iter().map(Escape::into_core));
}

quote
}
}

impl Escape {
fn into_core(self) -> decondenser::Escape {
let Self { escaped, unescaped } = self;
decondenser::Escape::new(escaped, unescaped)
let Self { opening, closing } = self;
decondenser::Quote::new(opening, closing)
}
}
13 changes: 3 additions & 10 deletions decondenser-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::path::Path;

#[derive(Default)]
pub(crate) struct Config {
formatting: Formatting,
common: Common,
langs: BTreeMap<String, Lang>,

// Only used for debugging. No stability guarantees are provided for these
Expand All @@ -21,15 +21,14 @@ pub(crate) struct Config {
}

#[derive(Default)]
struct Formatting {
struct Common {
indent: Option<String>,
max_line_size: Option<usize>,
no_break_size: Option<usize>,
preserve_newlines: Option<bool>,
}

struct Lang {
formatting: Formatting,
common: Common,
groups: Option<Vec<Group>>,
quotes: Option<Vec<Quote>>,
puncts: Option<Vec<Punct>>,
Expand All @@ -56,12 +55,6 @@ struct Space {
struct Quote {
opening: String,
closing: String,
escapes: Option<Vec<Escape>>,
}

struct Escape {
escaped: String,
unescaped: String,
}

impl Config {
Expand Down
Loading
Loading