Skip to content

Commit

Permalink
Remove rustfmt.toml (#157)
Browse files Browse the repository at this point in the history
Reasons:

- We should not expect people to use nightly rustfmt given this project
aims to support stable Rust
- I'm way too tired of copying over `rustfmt.toml` files for every
single project I start
  • Loading branch information
liquidev authored Oct 30, 2023
1 parent f16cbaa commit 242849b
Show file tree
Hide file tree
Showing 57 changed files with 931 additions and 299 deletions.
5 changes: 4 additions & 1 deletion mica-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ fn interpret<'e>(
fn engine(options: &EngineOptions) -> Engine {
Engine::with_debug_options(
mica::corelib::Lib,
mica::DebugOptions { dump_ast: options.dump_ast, dump_bytecode: options.dump_bytecode },
mica::DebugOptions {
dump_ast: options.dump_ast,
dump_bytecode: options.dump_bytecode,
},
)
}

Expand Down
8 changes: 0 additions & 8 deletions rustfmt.toml

This file was deleted.

39 changes: 27 additions & 12 deletions src/corelib/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,29 @@ pub(crate) fn define(builder: TypeBuilder<Vec<RawValue>>) -> TypeBuilder<Vec<Raw
RawFunctionKind::Foreign(Box::new(|env, _, args| {
let arguments = Arguments::new(args, env);
let v = unsafe {
arguments.raw_self().downcast_user_data_unchecked::<List>().as_slice()
arguments
.raw_self()
.downcast_user_data_unchecked::<List>()
.as_slice()
};
let index = arguments.nth(0).unwrap().ensure_number()? as usize;
Ok(v.get(index).copied().unwrap_or(RawValue::from(())))
})),
)
.add_function("set", |v: &mut Vec<RawValue>, index: usize, value: RawValue| {
if index < v.len() {
v[index] = value;
Ok(value)
} else {
Err(OutOfBounds { index, len: v.len() })
}
})
.add_function(
"set",
|v: &mut Vec<RawValue>, index: usize, value: RawValue| {
if index < v.len() {
v[index] = value;
Ok(value)
} else {
Err(OutOfBounds {
index,
len: v.len(),
})
}
},
)
.add_function("first", |v: &Vec<RawValue>| v.first().copied())
.add_function("last", |v: &Vec<RawValue>| v.last().copied())
.add_function("contains", |v: &Vec<RawValue>, x: RawValue| v.contains(&x))
Expand All @@ -44,9 +53,15 @@ pub(crate) fn define(builder: TypeBuilder<Vec<RawValue>>) -> TypeBuilder<Vec<Raw
.add_function("truncate", Vec::truncate)
.add_function("repeat", |v: &Vec<RawValue>, n: usize| v.repeat(n))
.add_function("reverse", |v: &mut Vec<RawValue>| v.reverse())
.add_function("rotate_left", |v: &mut Vec<RawValue>, n: usize| v.rotate_left(n))
.add_function("rotate_right", |v: &mut Vec<RawValue>, n: usize| v.rotate_right(n))
.add_function("swap", |v: &mut Vec<RawValue>, a: usize, b: usize| v.swap(a, b))
.add_function("rotate_left", |v: &mut Vec<RawValue>, n: usize| {
v.rotate_left(n)
})
.add_function("rotate_right", |v: &mut Vec<RawValue>, n: usize| {
v.rotate_right(n)
})
.add_function("swap", |v: &mut Vec<RawValue>, a: usize, b: usize| {
v.swap(a, b)
})
.add_function("clone", |v: &Vec<RawValue>| v.clone())
// TODO: It should be possible to implement this without raw functions in the future.
.add_raw_function(
Expand Down
36 changes: 25 additions & 11 deletions src/corelib/builtins/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ pub(crate) fn define(builder: TypeBuilder<String>) -> TypeBuilder<String> {
builder
.add_static("debug", |x: Value| format!("{x:?}"))
.add_function("cat", |s: &String, t: Gc<String>| format!("{s}{t}"))
.add_function("contains", |s: &String, sub: Gc<String>| s.contains(sub.deref().deref()))
.add_function("contains", |s: &String, sub: Gc<String>| {
s.contains(sub.deref().deref())
})
.add_function("starts_with", |s: &String, prefix: Gc<String>| {
s.starts_with(prefix.deref().deref())
})
Expand All @@ -27,11 +29,19 @@ pub(crate) fn define(builder: TypeBuilder<String>) -> TypeBuilder<String> {
.add_function("strip_suffix", |s: &String, suffix: Gc<String>| {
s.strip_suffix(suffix.deref().deref()).map(|x| x.to_owned())
})
.add_function("find", |s: &String, substr: Gc<String>| s.find(substr.deref().deref()))
.add_function("rfind", |s: &String, substr: Gc<String>| s.rfind(substr.deref().deref()))
.add_function("byte_at", |s: &String, position: usize| s.as_bytes().get(position).copied())
.add_function("find", |s: &String, substr: Gc<String>| {
s.find(substr.deref().deref())
})
.add_function("rfind", |s: &String, substr: Gc<String>| {
s.rfind(substr.deref().deref())
})
.add_function("byte_at", |s: &String, position: usize| {
s.as_bytes().get(position).copied()
})
.add_function("byte_len", |s: &String| s.len())
.add_function("nth_char", |s: &String, position: usize| s.chars().nth(position))
.add_function("nth_char", |s: &String, position: usize| {
s.chars().nth(position)
})
.add_function("nth_code_point", |s: &String, position: usize| {
s.chars().nth(position).map(u32::from)
})
Expand All @@ -40,12 +50,16 @@ pub(crate) fn define(builder: TypeBuilder<String>) -> TypeBuilder<String> {
.add_function("to_lowercase", |s: &String| s.to_lowercase())
.add_function("to_uppercase", |s: &String| s.to_uppercase())
.add_function("repeat", |s: &String, n: usize| s.repeat(n))
.add_function("replace", |s: &String, pat: Gc<String>, with: Gc<String>| {
s.replace(pat.deref().deref(), &with)
})
.add_function("replace", |s: &String, pat: Gc<String>, with: Gc<String>, n: usize| {
s.replacen(pat.deref().deref(), &with, n)
})
.add_function(
"replace",
|s: &String, pat: Gc<String>, with: Gc<String>| s.replace(pat.deref().deref(), &with),
)
.add_function(
"replace",
|s: &String, pat: Gc<String>, with: Gc<String>, n: usize| {
s.replacen(pat.deref().deref(), &with, n)
},
)
.add_function("trim", |s: &String| s.trim().to_owned())
// TODO: It should be possible to implement these without raw functions in the future.
.add_raw_function(
Expand Down
5 changes: 3 additions & 2 deletions src/corelib/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ fn error(arguments: Arguments) -> Result<(), UserError> {

fn assert(condition: Value, message: Option<Value>) -> Result<Value, Error> {
if condition.is_falsy() {
let message =
message.map(|value| value.to_string()).unwrap_or_else(|| "assertion failed".to_owned());
let message = message
.map(|value| value.to_string())
.unwrap_or_else(|| "assertion failed".to_owned());
Err(message).mica()
} else {
Ok(condition)
Expand Down
12 changes: 10 additions & 2 deletions src/corelib/iterators/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ struct CountUp {

impl CountUp {
fn with_step(min: i64, max: i64, step: i64) -> Self {
Self { current: min, max, step }
Self {
current: min,
max,
step,
}
}

fn new(min: i64, max: i64) -> Self {
Expand Down Expand Up @@ -38,7 +42,11 @@ struct CountDown {

impl CountDown {
fn with_step(max: i64, min: i64, step: i64) -> Self {
Self { current: max, min, step }
Self {
current: max,
min,
step,
}
}

fn new(max: i64, min: i64) -> Self {
Expand Down
17 changes: 14 additions & 3 deletions src/corelib/iterators/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ pub(crate) struct DictIter {
impl DictIter {
pub(crate) unsafe fn new(value: RawValue) -> Self {
let dict = value.downcast_user_data_unchecked::<Dict>();
Self { dict: value, iter: dict.raw_iter(), len: dict.len() }
Self {
dict: value,
iter: dict.raw_iter(),
len: dict.len(),
}
}

fn checked_next(
Expand All @@ -29,7 +33,10 @@ impl DictIter {
// otherwise the iterator becomes invalid.
let current_len = unsafe { dict.downcast_user_data_unchecked::<Dict>().len() };
if len != current_len {
return Err(LenChangedDuringIteration { was: len, became: current_len });
return Err(LenChangedDuringIteration {
was: len,
became: current_len,
});
}
Ok(iter.next())
}
Expand Down Expand Up @@ -63,7 +70,11 @@ struct LenChangedDuringIteration {

impl std::fmt::Display for LenChangedDuringIteration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "dict length changed during iteration (was {}, became {})", self.was, self.became)
write!(
f,
"dict length changed during iteration (was {}, became {})",
self.was, self.became
)
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/corelib/iterators/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ pub(crate) struct ListIter {
impl ListIter {
pub(crate) unsafe fn new(list: RawValue) -> Self {
let slice = unsafe { list.downcast_user_data_unchecked::<List>().as_slice() };
ListIter { list, i: 0, len: slice.len() }
ListIter {
list,
i: 0,
len: slice.len(),
}
}

fn has_next(&self) -> bool {
Expand All @@ -25,7 +29,10 @@ impl ListIter {
let list = self.list.downcast_user_data_unchecked::<List>();
let slice = list.as_slice();
if slice.len() != self.len {
return Err(LenChangedDuringIteration { was: self.len, became: slice.len() });
return Err(LenChangedDuringIteration {
was: self.len,
became: slice.len(),
});
}
if let Some(v) = slice.get(self.i) {
self.i += 1;
Expand All @@ -51,7 +58,11 @@ struct LenChangedDuringIteration {

impl std::fmt::Display for LenChangedDuringIteration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "list length changed during iteration (was {}, became {})", self.was, self.became)
write!(
f,
"list length changed during iteration (was {}, became {})",
self.was, self.became
)
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/corelib/iterators/string/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ pub(crate) struct StringBytes {

impl StringBytes {
pub unsafe fn new(s: RawValue) -> Self {
Self { string: s, index: 0 }
Self {
string: s,
index: 0,
}
}

fn has_next(&self) -> bool {
Expand Down
5 changes: 4 additions & 1 deletion src/corelib/iterators/string/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ pub(crate) struct StringChars {

impl StringChars {
pub unsafe fn new(s: RawValue) -> Self {
Self { string: s, index: 0 }
Self {
string: s,
index: 0,
}
}

fn has_next(&self) -> bool {
Expand Down
5 changes: 4 additions & 1 deletion src/corelib/iterators/string/code_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ pub(crate) struct StringCodePoints {

impl StringCodePoints {
pub unsafe fn new(s: RawValue) -> Self {
Self { string: s, index: 0 }
Self {
string: s,
index: 0,
}
}

fn has_next(&self) -> bool {
Expand Down
5 changes: 4 additions & 1 deletion src/corelib/iterators/string/lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ pub(crate) struct StringLines {

impl StringLines {
pub unsafe fn new(s: RawValue) -> Self {
Self { string: s, index: 0 }
Self {
string: s,
index: 0,
}
}

fn has_next(&self) -> bool {
Expand Down
6 changes: 5 additions & 1 deletion src/corelib/iterators/string/rsplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ pub(crate) struct StringRSplit {

impl StringRSplit {
pub unsafe fn new(s: RawValue, separator: Gc<String>) -> Self {
Self { string: s, separator, index: unsafe { s.get_raw_string_unchecked().get().len() } }
Self {
string: s,
separator,
index: unsafe { s.get_raw_string_unchecked().get().len() },
}
}

fn has_next(&self) -> bool {
Expand Down
6 changes: 5 additions & 1 deletion src/corelib/iterators/string/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ pub(crate) struct StringSplit {

impl StringSplit {
pub unsafe fn new(s: RawValue, separator: Gc<String>) -> Self {
Self { string: s, separator, index: 0 }
Self {
string: s,
separator,
index: 0,
}
}

fn has_next(&self) -> bool {
Expand Down
Loading

0 comments on commit 242849b

Please sign in to comment.