Skip to content
Closed
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
53 changes: 53 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,56 @@ simd_json = ["dep:simd-json"]
[[example]]
name = "demo"
path = "examples/demo/src/main.rs"

[lints.rust]
unsafe_code = "deny"
deprecated = "warn"
elided_lifetimes_in_paths = "warn"
future_incompatible = { level = "warn", priority = -1 }
nonstandard_style = { level = "warn", priority = -1 }
rust_2018_idioms = { level = "warn", priority = -1 }
rust_2021_prelude_collisions = "warn"
semicolon_in_expressions_from_macros = "warn"
trivial_numeric_casts = "warn"
unsafe_op_in_unsafe_fn = "warn" # `unsafe_op_in_unsafe_fn` may become the default in future Rust versions: https://github.com/rust-lang/rust/issues/71668
unused_extern_crates = "warn"
unused_import_braces = "warn"
unused_lifetimes = "warn"
trivial_casts = "allow"
unused_qualifications = "allow"

[lints.rustdoc]
all = "warn"
missing_crate_level_docs = "warn"

[lints.clippy]
all = { level = "warn", priority = -1 }
correctness = { level = "warn", priority = -1 }
suspicious = { level = "warn", priority = -1 }
style = { level = "warn", priority = -1 }
complexity = { level = "warn", priority = -1 }
perf = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }

# allow some lints
cast_possible_truncation = "allow"
cast_sign_loss = "allow"
cast_precision_loss = "allow"
module_name_repetitions = "allow"
too_many_lines = "allow"

# Additional lints from https://rust-lang.github.io/rust-clippy/master/index.html?groups=restriction
allow_attributes = "warn"
allow_attributes_without_reason = "warn"
assertions_on_result_states = "warn"
create_dir = "warn"
clone_on_ref_ptr = "warn"
expect_used = "warn"
missing_assert_message = "warn"
panic_in_result_fn = "warn"
# shadow_reuse = "warn"
str_to_string = "warn"
todo = "warn"
unimplemented = "warn"
unwrap_used = "warn"
wildcard_enum_match_arm = "warn"
6 changes: 3 additions & 3 deletions examples/demo/src/apps/copy_to_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct CopyToClipboardExample {
}

impl CopyToClipboardExample {
pub fn new(value: Value) -> Self {
pub const fn new(value: Value) -> Self {
Self { value }
}
}
Expand All @@ -33,15 +33,15 @@ impl Show for CopyToClipboardExample {
let pointer = context.pointer().to_json_pointer_string();
if !pointer.is_empty() && ui.button("Copy path").clicked() {
ui.output_mut(|o| {
println!("{}", pointer);
println!("{pointer}");
o.copied_text = pointer;
});
ui.close_menu();
}

if ui.button("Copy contents").clicked() {
if let Ok(pretty_str) = serde_json::to_string_pretty(context.value()) {
println!("{}", pretty_str);
println!("{pretty_str}");
ui.output_mut(|o| o.copied_text = pretty_str);
}
ui.close_menu();
Expand Down
2 changes: 2 additions & 0 deletions examples/demo/src/apps/custom_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct CustomExample {
}

impl CustomExample {
#[expect(clippy::unwrap_used, reason = "this is an example function")]
pub fn new() -> Self {
Self {
input: serde_json::to_string_pretty(&json!({"foo": "bar"})).unwrap(),
Expand All @@ -21,6 +22,7 @@ impl Show for CustomExample {
"Custom Input"
}

#[expect(clippy::unwrap_used, reason = "this is an example function")]
fn show(&mut self, ui: &mut Ui) {
ui.hyperlink_to("Source", "https://github.com/dmackdev/egui_json_tree/blob/master/examples/demo/src/apps/custom_input.rs");
ui.label("Enter raw JSON in the text box to see the visualisation below.");
Expand Down
36 changes: 18 additions & 18 deletions examples/demo/src/apps/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl JsonEditorExample {
pub fn new(value: Value) -> Self {
Self {
value,
editor: Default::default(),
editor: Editor::default(),
}
}
}
Expand All @@ -41,10 +41,10 @@ impl Editor {
fn show(&mut self, ui: &mut Ui, document: &Value, context: RenderContext<'_, '_, Value>) {
match self.state.as_mut() {
Some(EditState::EditObjectKey(state)) => {
Self::show_edit_object_key(ui, document, context, state, &mut self.edit_events)
Self::show_edit_object_key(ui, document, &context, state, &mut self.edit_events);
}
Some(EditState::EditValue(state)) => {
Self::show_edit_value(ui, context, state, &mut self.edit_events);
Self::show_edit_value(ui, &context, state, &mut self.edit_events);
}
None => {
self.show_with_context_menus(ui, context);
Expand All @@ -55,7 +55,7 @@ impl Editor {
fn show_edit_object_key(
ui: &mut Ui,
document: &Value,
context: RenderContext<Value>,
context: &RenderContext<'_, '_, Value>,
state: &mut EditObjectKeyState,
edit_events: &mut Vec<EditEvent>,
) {
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Editor {
if state.is_new_key {
edit_events.push(EditEvent::DeleteFromObject {
object_pointer: state.object_pointer.to_string(),
key: key.to_string(),
key: key.to_owned(),
});
}
edit_events.push(EditEvent::CloseObjectKeyEdit);
Expand All @@ -108,7 +108,7 @@ impl Editor {

fn show_edit_value(
ui: &mut Ui,
context: RenderContext<Value>,
context: &RenderContext<'_, '_, Value>,
state: &mut EditValueState,
edit_events: &mut Vec<EditEvent>,
) {
Expand Down Expand Up @@ -137,16 +137,16 @@ impl Editor {
context.render_default(ui);
}

fn show_with_context_menus(&mut self, ui: &mut Ui, context: RenderContext<Value>) {
fn show_with_context_menus(&mut self, ui: &mut Ui, context: RenderContext<'_, '_, Value>) {
match context {
RenderContext::Property(context) => {
self.show_property_context_menu(ui, context);
}
RenderContext::BaseValue(context) => {
self.show_value_context_menu(ui, context);
self.show_value_context_menu(ui, &context);
}
RenderContext::ExpandableDelimiter(context) => {
self.show_expandable_delimiter_context_menu(ui, context);
RenderContext::ExpandableDelimiter(mut context) => {
self.show_expandable_delimiter_context_menu(ui, &mut context);
}
};
}
Expand Down Expand Up @@ -184,21 +184,21 @@ impl Editor {
if let JsonPointerSegment::Key(key) = &context.property {
if ui.button("Edit key").clicked() {
self.state = Some(EditState::EditObjectKey(EditObjectKeyState {
key: key.to_string(),
key: (*key).to_owned(),
object_pointer: parent.to_json_pointer_string(),
new_key_input: key.to_string(),
new_key_input: (*key).to_owned(),
request_focus: true,
is_new_key: false,
}));
ui.close_menu()
ui.close_menu();
}
}

if ui.button("Delete").clicked() {
let event = match context.property {
JsonPointerSegment::Key(key) => EditEvent::DeleteFromObject {
object_pointer: parent.to_json_pointer_string(),
key: key.to_string(),
key: key.to_owned(),
},
JsonPointerSegment::Index(idx) => EditEvent::DeleteFromArray {
array_pointer: parent.to_json_pointer_string(),
Expand All @@ -215,7 +215,7 @@ impl Editor {
fn show_value_context_menu(
&mut self,
ui: &mut Ui,
context: RenderBaseValueContext<'_, '_, Value>,
context: &RenderBaseValueContext<'_, '_, Value>,
) {
context
.render_default(ui)
Expand All @@ -235,7 +235,7 @@ impl Editor {
if ui.button("Delete").clicked() {
self.edit_events.push(EditEvent::DeleteFromObject {
object_pointer: parent.to_json_pointer_string(),
key: key.to_string(),
key: (*key).to_owned(),
});
ui.close_menu();
}
Expand All @@ -257,7 +257,7 @@ impl Editor {
fn show_expandable_delimiter_context_menu(
&mut self,
ui: &mut Ui,
context: RenderExpandableDelimiterContext<'_, '_, Value>,
context: &mut RenderExpandableDelimiterContext<'_, '_, Value>,
) {
match context.delimiter {
ExpandableDelimiter::OpeningArray
Expand Down Expand Up @@ -350,7 +350,7 @@ impl Editor {
.and_then(|value| value.as_object_mut())
{
let mut counter = 0;
let mut new_key = "new_key".to_string();
let mut new_key = "new_key".to_owned();

while obj.contains_key(&new_key) {
counter += 1;
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/apps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Example {
}

impl Example {
pub fn new(title: &'static str, value: Value) -> Self {
pub const fn new(title: &'static str, value: Value) -> Self {
Self { title, value }
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/demo/src/apps/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub struct SearchExample {
}

impl SearchExample {
pub fn new(value: Value) -> Self {
pub const fn new(value: Value) -> Self {
Self {
value,
search_input: "".to_string(),
search_input: String::new(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/apps/toggle_buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl ToggleButtonsCustomisationDemo {
pub fn new(value: Value) -> Self {
Self {
value,
toggle_buttons_state: Default::default(),
toggle_buttons_state: ToggleButtonsState::default(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/apps/wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct WrappingExample {
}

impl WrappingExample {
pub fn new(value: Value) -> Self {
pub const fn new(value: Value) -> Self {
Self {
value,
wrap: JsonTreeWrapping {
Expand Down
34 changes: 16 additions & 18 deletions examples/demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use apps::{
editor::JsonEditorExample, search::SearchExample,
toggle_buttons::ToggleButtonsCustomisationDemo, wrapping::WrappingExample, Example, Show,
};
use egui::Direction;
use serde_json::json;

mod apps;
Expand Down Expand Up @@ -115,25 +116,22 @@ impl eframe::App for DemoApp {
}

egui::CentralPanel::default().show(ctx, |ui| {
match example {
Some(example) => {
egui::ScrollArea::vertical()
.auto_shrink([false, false])
.show(ui, |ui| {
example.show(ui);
});
}
None => {
if !self.left_sidebar_expanded {
collapsible_sidebar_button_ui(ui, &mut self.left_sidebar_expanded);
}
ui.with_layout(
egui::Layout::centered_and_justified(egui::Direction::LeftToRight),
|ui| {
ui.heading("Select an example.");
},
);
if let Some(example) = example {
egui::ScrollArea::vertical()
.auto_shrink([false, false])
.show(ui, |ui| {
example.show(ui);
});
} else {
if !self.left_sidebar_expanded {
collapsible_sidebar_button_ui(ui, &mut self.left_sidebar_expanded);
}
ui.with_layout(
egui::Layout::centered_and_justified(Direction::LeftToRight),
|ui| {
ui.heading("Select an example.");
},
);
};
});
}
Expand Down
22 changes: 11 additions & 11 deletions src/delimiters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ pub(crate) enum SpacingDelimiter {
impl AsRef<str> for SpacingDelimiter {
fn as_ref(&self) -> &str {
match self {
SpacingDelimiter::Empty => " ",
SpacingDelimiter::Comma => ", ",
SpacingDelimiter::Colon => ": ",
Self::Empty => " ",
Self::Comma => ", ",
Self::Colon => ": ",
}
}
}
Expand All @@ -54,14 +54,14 @@ pub enum ExpandableDelimiter {
impl AsRef<str> for ExpandableDelimiter {
fn as_ref(&self) -> &str {
match self {
ExpandableDelimiter::CollapsedArray => "[...]",
ExpandableDelimiter::CollapsedEmptyArray => "[]",
ExpandableDelimiter::OpeningArray => "[",
ExpandableDelimiter::ClosingArray => "]",
ExpandableDelimiter::CollapsedObject => "{...}",
ExpandableDelimiter::CollapsedEmptyObject => "{}",
ExpandableDelimiter::OpeningObject => "{",
ExpandableDelimiter::ClosingObject => "}",
Self::CollapsedArray => "[...]",
Self::CollapsedEmptyArray => "[]",
Self::OpeningArray => "[",
Self::ClosingArray => "]",
Self::CollapsedObject => "{...}",
Self::CollapsedEmptyObject => "{}",
Self::OpeningObject => "{",
Self::ClosingObject => "}",
}
}
}
9 changes: 5 additions & 4 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ use crate::{
DefaultExpand, JsonTree, JsonTreeStyle, ToggleButtonsState,
};

pub(crate) struct JsonTreeNode<'a, 'b, T: ToJsonTreeValue> {
pub struct JsonTreeNode<'a, 'b, T: ToJsonTreeValue> {
value: &'a T,
parent: Option<JsonPointerSegment<'a>>,
make_persistent_id: &'b dyn Fn(&[JsonPointerSegment]) -> Id,
make_persistent_id: &'b dyn Fn(&[JsonPointerSegment<'_>]) -> Id,
config: &'b JsonTreeNodeConfig,
}

impl<'a, 'b, T: ToJsonTreeValue> JsonTreeNode<'a, 'b, T> {
pub(crate) fn show(tree: JsonTree<'a, T>, ui: &mut Ui) -> JsonTreeResponse {
let persistent_id = ui.id();
let tree_id = tree.id;
let make_persistent_id =
|path_segments: &[JsonPointerSegment]| persistent_id.with(tree_id.with(path_segments));
let make_persistent_id = |path_segments: &[JsonPointerSegment<'_>]| {
persistent_id.with(tree_id.with(path_segments))
};

let style = tree.config.style.unwrap_or_default();
let default_expand = tree.config.default_expand.unwrap_or_default();
Expand Down
Loading