Repository: sxyazi/yazi
File: yazi-plugin/src/elements/rect.rs (Rust)

Code before:

impl From<ratatui::layout::Rect> for Rect {
	fn from(rect: ratatui::layout::Rect) -> Self { Self(rect) }
}

impl From<ratatui::layout::Size> for Rect {
	fn from(size: ratatui::layout::Size) -> Self {
		Self(ratatui::layout::Rect { x: 0, y: 0, width: size.width, height: size.height })
	}
}

impl Rect {
	pub fn compose(lua: &Lua) -> mlua::Result<Value> {
		let new = lua.create_function(|_, (_, args): (Table, Table)| {
			Ok(Self(ratatui::layout::Rect {
				x:      args.raw_get("x").unwrap_or_default(),
				y:      args.raw_get("y").unwrap_or_default(),
				width:  args.raw_get("w").unwrap_or_default(),
				height: args.raw_get("h").unwrap_or_default(),
			}))
		})?;

		let index = lua.create_function(move |lua, (_, key): (Table, mlua::String)| {
			Ok(match key.as_bytes().as_ref() {
				b"default" => {
					deprecate!(lua, "`ui.Rect.default` is deprecated, use `ui.Rect{{}}` instead, in your {}");
					Some(Self(Default::default()))
				}
				_ => None,
			})
		})?;

		let rect = lua.create_table()?;
		rect.set_metatable(Some(
			lua.create_table_from([(MetaMethod::Call.name(), new), (MetaMethod::Index.name(), index)])?,
		));
		rect.into_lua(lua)
	}

	pub(super) fn pad(self, pad: Pad) -> Self {
		let mut r = *self;
		r.x = r.x.saturating_add(pad.left);
		r.y = r.y.saturating_add(pad.top);

		r.width = r.width.saturating_sub(pad.left + pad.right);
		r.height = r.height.saturating_sub(pad.top + pad.bottom);
		Self(r)
	}
}

impl UserData for Rect {

Review comment (suggestion):
The deprecation message includes an unused placeholder '{}' that might confuse users. Consider removing the placeholder or providing a proper argument for formatting.
```suggestion
					deprecate!(lua, "`ui.Rect.default` is deprecated, use `ui.Rect` instead.");
```

Code after:

impl From<ratatui::layout::Rect> for Rect {
	fn from(rect: ratatui::layout::Rect) -> Self { Self(rect) }
}

impl From<ratatui::layout::Size> for Rect {
	fn from(size: ratatui::layout::Size) -> Self {
		Self(ratatui::layout::Rect { x: 0, y: 0, width: size.width, height: size.height })
	}
}

impl Rect {
	pub fn compose(lua: &Lua) -> mlua::Result<Value> {
		let new = lua.create_function(|_, (_, args): (Table, Table)| {
			Ok(Self(ratatui::layout::Rect {
				x:      args.raw_get("x").unwrap_or_default(),
				y:      args.raw_get("y").unwrap_or_default(),
				width:  args.raw_get("w").unwrap_or_default(),
				height: args.raw_get("h").unwrap_or_default(),
			}))
		})?;

		let index = lua.create_function(move |lua, (_, key): (Table, mlua::String)| {
			Ok(match key.as_bytes().as_ref() {
				b"default" => {
					deprecate!(lua, "`ui.Rect.default` is deprecated, use `ui.Rect{{}}` instead, in your {}\nSee #2927 for more details: https://github.com/sxyazi/yazi/pull/2927");
					Some(Self(Default::default()))
				}
				_ => None,
			})
		})?;

		let rect = lua.create_table()?;
		rect.set_metatable(Some(
			lua.create_table_from([(MetaMethod::Call.name(), new), (MetaMethod::Index.name(), index)])?,
		));
		rect.into_lua(lua)
	}

	pub(super) fn pad(self, pad: Pad) -> Self {
		let mut r = *self;
		r.x = r.x.saturating_add(pad.left);
		r.y = r.y.saturating_add(pad.top);

		r.width = r.width.saturating_sub(pad.left + pad.right);
		r.height = r.height.saturating_sub(pad.top + pad.bottom);
		Self(r)
	}
}

impl UserData for Rect {