Skip to content

Commit

Permalink
Fix getting newline happy in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
DevinR528 committed Dec 8, 2023
1 parent ee05465 commit a439956
Show file tree
Hide file tree
Showing 2 changed files with 291 additions and 8 deletions.
282 changes: 282 additions & 0 deletions examp/ru.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
[package]
name = "ruma"
categories = ["api-bindings", "web-programming"]
keywords = ["matrix", "chat", "messaging", "ruma"]
description = "Types and traits for working with the Matrix protocol."
homepage = "https://ruma.io/"
repository = "https://github.com/ruma/ruma"
readme = "README.md"
license = "MIT"
version = "0.9.4"
edition = "2021"
rust-version = { workspace = true }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
api = ["ruma-common/api"]
canonical-json = ["ruma-common/canonical-json", "ruma-events?/canonical-json"]
client = ["dep:ruma-client"]
events = ["dep:ruma-events"]
server-util = ["dep:ruma-server-util"]
signatures = ["dep:ruma-signatures", "canonical-json"]
state-res = ["dep:ruma-state-res"]

# ruma-client feature flags
client-ext-client-api = ["client", "ruma-client?/client-api"]
client-hyper = ["client", "ruma-client?/hyper"]
client-hyper-native-tls = ["client", "ruma-client?/hyper-native-tls"]
client-isahc = ["client", "ruma-client?/isahc"]
client-reqwest = ["client", "ruma-client?/reqwest"]
client-reqwest-native-tls = ["client", "ruma-client?/reqwest-native-tls"]
client-reqwest-native-tls-vendored = ["client", "ruma-client?/reqwest-native-tls-vendored"]
client-reqwest-rustls-manual-roots = ["client", "ruma-client?/reqwest-rustls-manual-roots"]
client-reqwest-rustls-webpki-roots = ["client", "ruma-client?/reqwest-rustls-webpki-roots"]
client-reqwest-rustls-native-roots = ["client", "ruma-client?/reqwest-rustls-native-roots"]

appservice-api-c = ["api", "events", "dep:ruma-appservice-api", "ruma-appservice-api?/client"]
appservice-api-s = ["api", "events", "dep:ruma-appservice-api", "ruma-appservice-api?/server"]
appservice-api = ["appservice-api-c", "appservice-api-s"]

client-api-c = ["api", "events", "dep:ruma-client-api", "ruma-client-api?/client"]
client-api-s = ["api", "events", "dep:ruma-client-api", "ruma-client-api?/server"]
client-api = ["client-api-c", "client-api-s"]

federation-api-c = ["api", "signatures", "dep:ruma-federation-api", "ruma-federation-api?/client"]
federation-api-s = ["api", "signatures", "dep:ruma-federation-api", "ruma-federation-api?/server"]
federation-api = ["federation-api-c", "federation-api-s"]

identity-service-api-c = [
"api",
"dep:ruma-identity-service-api",
"ruma-identity-service-api?/client",
]
identity-service-api-s = [
"api",
"dep:ruma-identity-service-api",
"ruma-identity-service-api?/server",
]
identity-service-api = ["identity-service-api-c", "identity-service-api-s"]

push-gateway-api-c = ["api", "dep:ruma-push-gateway-api", "ruma-push-gateway-api?/client"]
push-gateway-api-s = ["api", "dep:ruma-push-gateway-api", "ruma-push-gateway-api?/server"]
push-gateway-api = ["push-gateway-api-c", "push-gateway-api-s"]

# Required for randomness, current system time in browser environments
js = ["ruma-common/js"]

# Convenience features
rand = ["ruma-common/rand"]
markdown = ["ruma-events?/markdown"]
html = ["dep:ruma-html", "ruma-events?/html"]

# Everything except compat, js and unstable features
full = [
"api",
"client",
"client-ext-client-api",
"events",
"signatures",
"state-res",
"appservice-api",
"client-api",
"federation-api",
"identity-service-api",
"push-gateway-api",
"rand",
"markdown",
"html",
]

# Enable all compatibility hacks. Deprecated.
compat = [
"compat-key-id",
"compat-user-id",
"compat-empty-string-null",
"compat-null",
"compat-optional",
"compat-unset-avatar",
"compat-get-3pids",
"compat-signature-id",
"compat-tag-info",
]

# Allow IDs to exceed 255 bytes.
compat-arbitrary-length-ids = ["ruma-common/compat-arbitrary-length-ids"]

# Don't validate the version part in `KeyId`.
compat-key-id = ["ruma-common/compat-key-id"]

# Allow some user IDs that are invalid even with the specified historical
# user ID scheme.
compat-user-id = ["ruma-common/compat-user-id"]

# Allow some mandatory fields in requests / responses to be missing, defaulting
# them to an empty string in deserialization.
compat-empty-string-null = [
"ruma-common/compat-empty-string-null",
"ruma-client-api?/compat-empty-string-null",
"ruma-events?/compat-empty-string-null",
"ruma-federation-api?/compat-empty-string-null",
]

# Allow certain fields to be `null` for compatibility, treating that the same as
# the field being absent.
compat-null = ["ruma-common/compat-null"]

# Allow certain fields to be absent even though the spec marks them as
# mandatory. Deserialization will yield a default value like an empty string.
compat-optional = ["ruma-common/compat-optional", "ruma-events?/compat-optional"]

# Unset avatars by sending an empty string, same as what Element Web does, c.f.
# https://github.com/matrix-org/matrix-spec/issues/378#issuecomment-1055831264
compat-unset-avatar = ["ruma-client-api?/compat-unset-avatar"]

# Always serialize the threepids response field in `get_3pids::v3::Response`,
# even if its value is an empty list.
compat-get-3pids = ["ruma-client-api?/compat-get-3pids"]

# Accept `message` as an alias for `error` in `upload_signatures::v3::Failure`,
# since that's what Synapse sends.
compat-upload-signatures = ["ruma-client-api?/compat-upload-signatures"]

# Allow extra characters in signature IDs not allowed in the specification.
compat-signature-id = ["ruma-signatures?/compat-signature-id"]

# Allow TagInfo to contain a stringified floating-point value for the `order` field.
compat-tag-info = ["ruma-events?/compat-tag-info"]

# Specific compatibility for past ring public/private key documents.
ring-compat = ["dep:ruma-signatures", "ruma-signatures?/ring-compat"]

# unstable: by using any of these, you opt out of all semver guarantees Ruma
# otherwise provides!
unstable-exhaustive-types = [
"ruma-common/unstable-exhaustive-types",
"ruma-appservice-api?/unstable-exhaustive-types",
"ruma-client-api?/unstable-exhaustive-types",
"ruma-federation-api?/unstable-exhaustive-types",
"ruma-identity-service-api?/unstable-exhaustive-types",
"ruma-push-gateway-api?/unstable-exhaustive-types",
"ruma-state-res?/unstable-exhaustive-types",
"ruma-events?/unstable-exhaustive-types",
]
unstable-extensible-events = [
"unstable-msc3246",
"unstable-msc3488",
"unstable-msc3553",
"unstable-msc3954",
"unstable-msc3955",
]
unstable-msc1767 = ["ruma-events?/unstable-msc1767"]
unstable-msc2409 = ["ruma-appservice-api?/unstable-msc2409"]
unstable-msc2448 = [
"ruma-client-api?/unstable-msc2448",
"ruma-events?/unstable-msc2448",
"ruma-federation-api?/unstable-msc2448",
]
unstable-msc2654 = ["ruma-client-api?/unstable-msc2654"]
unstable-msc2666 = ["ruma-client-api?/unstable-msc2666"]
unstable-msc2747 = ["ruma-events?/unstable-msc2747"]
unstable-msc2870 = ["ruma-common/unstable-msc2870"]
unstable-msc2965 = ["ruma-client-api?/unstable-msc2965"]
unstable-msc2967 = ["ruma-client-api?/unstable-msc2967"]
unstable-msc3061 = ["ruma-events?/unstable-msc3061"]
unstable-msc3202 = ["ruma-appservice-api?/unstable-msc3202"]
unstable-msc3245 = ["ruma-events?/unstable-msc3245"]
# Support the m.room.message fallback fields from the first version of MSC3245,
# implemented in Element Web and documented at
# https://github.com/matrix-org/matrix-spec-proposals/blob/83f6c5b469c1d78f714e335dcaa25354b255ffa5/proposals/3245-voice-messages.md
unstable-msc3245-v1-compat = ["ruma-events?/unstable-msc3245-v1-compat"]
unstable-msc3246 = ["ruma-events?/unstable-msc3246"]
unstable-msc3381 = ["ruma-events?/unstable-msc3381"]
unstable-msc3401 = ["ruma-events?/unstable-msc3401"]
unstable-msc3488 = ["ruma-client-api?/unstable-msc3488", "ruma-events?/unstable-msc3488"]
unstable-msc3551 = ["ruma-events?/unstable-msc3551"]
unstable-msc3552 = ["ruma-events?/unstable-msc3552"]
unstable-msc3553 = ["ruma-events?/unstable-msc3553"]
unstable-msc3554 = ["ruma-events?/unstable-msc3554"]
unstable-msc3575 = ["ruma-client-api?/unstable-msc3575"]
unstable-msc3618 = ["ruma-federation-api?/unstable-msc3618"]
unstable-msc3723 = ["ruma-federation-api?/unstable-msc3723"]
unstable-msc3814 = ["ruma-client-api?/unstable-msc3814"]
unstable-msc3927 = ["ruma-events?/unstable-msc3927"]
unstable-msc3930 = ["ruma-common/unstable-msc3930"]
unstable-msc3931 = ["ruma-common/unstable-msc3931"]
unstable-msc3932 = ["ruma-common/unstable-msc3932"]
unstable-msc3954 = ["ruma-events?/unstable-msc3954"]
unstable-msc3955 = ["ruma-events?/unstable-msc3955"]
unstable-msc3956 = ["ruma-events?/unstable-msc3956"]
unstable-msc3983 = ["ruma-client-api?/unstable-msc3983"]
unstable-msc4075 = ["ruma-events?/unstable-msc4075"]
unstable-pdu = ["ruma-events?/unstable-pdu"]
unstable-unspecified = [
"ruma-common/unstable-unspecified",
"ruma-federation-api?/unstable-unspecified",
"ruma-push-gateway-api?/unstable-unspecified",
]

# Private feature, only used in test / benchmarking code
__ci = [
"full",
"compat-upload-signatures",
"unstable-unspecified",
"unstable-msc1767",
"unstable-msc2409",
"unstable-msc2448",
"unstable-msc2654",
"unstable-msc2666",
"unstable-msc2747",
"unstable-msc2870",
"unstable-msc2965",
"unstable-msc2967",
"unstable-msc3061",
"unstable-msc3202",
"unstable-msc3245",
"unstable-msc3245-v1-compat",
"unstable-msc3246",
"unstable-msc3381",
"unstable-msc3401",
"unstable-msc3488",
"unstable-msc3551",
"unstable-msc3552",
"unstable-msc3553",
"unstable-msc3554",
"unstable-msc3575",
"unstable-msc3618",
"unstable-msc3723",
"unstable-msc3814",
"unstable-msc3927",
"unstable-msc3930",
"unstable-msc3932",
"unstable-msc3954",
"unstable-msc3955",
"unstable-msc3956",
"unstable-msc3983",
"unstable-msc4075",
]

[dependencies]
assign = { workspace = true }
js_int = { workspace = true }
js_option = "0.1.1"

ruma-common = { workspace = true }

ruma-client = { workspace = true, optional = true }
ruma-events = { workspace = true, optional = true }
ruma-html = { workspace = true, optional = true }
ruma-server-util = { workspace = true, optional = true }
ruma-signatures = { workspace = true, optional = true }
ruma-state-res = { workspace = true, optional = true }

ruma-appservice-api = { workspace = true, optional = true }
ruma-client-api = { workspace = true, optional = true }
ruma-federation-api = { workspace = true, optional = true }
ruma-identity-service-api = { workspace = true, optional = true }
ruma-push-gateway-api = { workspace = true, optional = true }

[dev-dependencies]
serde = { workspace = true }
17 changes: 9 additions & 8 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl FromStr for Config {
max_array_line_len: toml
.get("max_array_line_len")
.and_then(toml_edit::Item::as_integer)
.unwrap_or_default() as usize,
.unwrap_or(80) as usize,
indent_count: toml
.get("indent_count")
.and_then(toml_edit::Item::as_integer)
Expand Down Expand Up @@ -173,6 +173,7 @@ fn fmt_value(value: &mut Value, config: &Config) {
match value {
Value::Array(arr) => {
if arr.to_string().len() > config.max_array_line_len {
let arr_has_trailing_newline = arr.trailing().as_str().map_or(false, |s| s.contains('\n'));
let len = arr.len();
for (i, val) in arr.iter_mut().enumerate() {
val.decor_mut().set_prefix(format!(
Expand All @@ -184,14 +185,16 @@ fn fmt_value(value: &mut Value, config: &Config) {
val.decor_mut().set_suffix(format!(
"{}{}",
if config.multiline_trailing_comma { "," } else { "" },
NEWLINE_PATTERN
if !arr_has_trailing_newline { NEWLINE_PATTERN } else { "" }
));
}
}
} else {
arr.fmt();
}
// TODO: this is most likely after an equal sign but not always...
arr.decor_mut().set_prefix(" ");
// TODO: can this be moved into the else of the above if/else
arr.set_trailing_comma(config.always_trailing_comma);
}
Value::InlineTable(table) => {
Expand Down Expand Up @@ -234,12 +237,6 @@ fn fmt_table(table: &mut Table, config: &Config) {

let keys: Vec<_> = table.iter().map(|(k, _)| k.to_owned()).collect();
for key in keys {
println!(
"key {} {} val: {:?}",
key,
table.get(&key).map_or(false, |item| item.is_value()),
table.get(&key)
);
let is_value_for_space = table.get(&key).map_or(false, |item| {
item.is_value() && item.as_inline_table().map_or(true, |t| !t.is_dotted())
});
Expand Down Expand Up @@ -379,3 +376,7 @@ mod test {
// println!("{}", toml.to_string());
}
}

// Array { trailing: "\n", trailing_comma: true, decor: Decor { prefix: " ", suffix: empty }, span: None, values: [Value(String(Formatted { value: "unstable-msc3246", repr: "\"unstable-msc3246\"", decor: Decor { prefix: "\n ", suffix: empty } })), Value(String(Formatted { value: "unstable-msc3488", repr: "\"unstable-msc3488\"", decor: Decor { prefix: "\n ", suffix: empty } })), Value(String(Formatted { value: "unstable-msc3553", repr: "\"unstable-msc3553\"", decor: Decor { prefix: "\n ", suffix: empty } })), Value(String(Formatted { value: "unstable-msc3954", repr: "\"unstable-msc3954\"", decor: Decor { prefix: "\n ", suffix: empty } })), Value(String(Formatted { value: "unstable-msc3955", repr: "\"unstable-msc3955\"", decor: Decor { prefix: "\n ", suffix: empty } }))] }

// Array { trailing: "\n", trailing_comma: true, decor: Decor { prefix: " ", suffix: empty }, span: None, values: [Value(String(Formatted { value: "unstable-msc3246", repr: "\"unstable-msc3246\"", decor: Decor { prefix: "\r\n ", suffix: empty } })), Value(String(Formatted { value: "unstable-msc3488", repr: "\"unstable-msc3488\"", decor: Decor { prefix: "\r\n ", suffix: empty } })), Value(String(Formatted { value: "unstable-msc3553", repr: "\"unstable-msc3553\"", decor: Decor { prefix: "\r\n ", suffix: empty } })), Value(String(Formatted { value: "unstable-msc3954", repr: "\"unstable-msc3954\"", decor: Decor { prefix: "\r\n ", suffix: empty } })), Value(String(Formatted { value: "unstable-msc3955", repr: "\"unstable-msc3955\"", decor: Decor { prefix: "\r\n ", suffix: ",\r\n" } }))] }

0 comments on commit a439956

Please sign in to comment.