Skip to content

Commit 17330c7

Browse files
authored
Merge pull request drivercraft#18 from Josen-B/main
features: self_features and depend_features displayed and selected in jkconfig ui
2 parents aac8b3d + 5aa365a commit 17330c7

File tree

24 files changed

+1553
-144
lines changed

24 files changed

+1553
-144
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fitimage/src/fit/standard_dt_builder.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use crate::error::Result;
66
use crate::fit::config::{ComponentConfig, FitImageConfig};
77
use crate::fit::{FdtHeader, FdtToken, FdtTokenUtils, MemReserveEntry, StringTable};
8-
use crate::hash::{calculate_hashes, default_hash_algorithms};
98

109
/// Standard FDT builder that creates U-Boot compatible FIT images
1110
pub struct StandardFdtBuilder {
@@ -303,30 +302,6 @@ impl StandardFdtBuilder {
303302
Ok(())
304303
}
305304

306-
/// Add hash nodes for component data
307-
fn add_hash_nodes(&mut self, data: &[u8]) -> Result<()> {
308-
let hash_algorithms = default_hash_algorithms();
309-
let hash_results = calculate_hashes(data, &hash_algorithms);
310-
311-
for (i, hash_result) in hash_results.iter().enumerate() {
312-
// Create hash node name (hash-1, hash-2, etc.)
313-
let hash_node_name = format!("hash-{}", i + 1);
314-
self.begin_node(&hash_node_name)?;
315-
316-
// Add algorithm property
317-
self.add_property_string("algo", hash_result.algorithm_name())?;
318-
319-
// Add value property - convert hex string to bytes
320-
let hash_value = hash_result.value();
321-
let hash_bytes = hex::decode(hash_value).unwrap_or_default();
322-
self.add_property_data("value", &hash_bytes)?;
323-
324-
self.end_node()?;
325-
}
326-
327-
Ok(())
328-
}
329-
330305
/// Begin a node
331306
fn begin_node(&mut self, name: &str) -> Result<()> {
332307
FdtToken::BeginNode.write_to_buffer(&mut self.struct_buffer);

jkconfig/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ anyhow = {workspace = true}
2323
clap = {workspace = true, features = ["derive"]}
2424
serde = {workspace = true, features = ["derive"]}
2525
serde_json = {workspace = true}
26-
26+
schemars = {workspace = true, features = ["derive"]}
27+
cargo_metadata = "0.23"
2728
toml = {workspace = true}
2829

2930
# Error handling
@@ -42,6 +43,7 @@ tower-http = {version = "0.6", features = ["fs", "cors"], optional = true}
4243
[features]
4344
default = ["web"]
4445
web = ["axum", "tokio", "tower", "tower-http", "chrono"]
46+
logging = []
4547

4648
[dev-dependencies]
4749
env_logger = "0.11"

jkconfig/src/data/app_data.rs

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
use std::{
2+
collections::HashMap,
23
fs,
34
path::{Path, PathBuf},
5+
sync::Arc,
46
time::SystemTime,
57
};
68

79
use anyhow::bail;
10+
use cursive::Cursive;
811

912
use crate::data::{menu::MenuRoot, types::ElementType};
1013

14+
pub type FeaturesCallback = Arc<dyn Fn() -> Vec<String> + Send + Sync>;
15+
16+
pub type HockCallback = Arc<dyn Fn(&mut Cursive, &str) + Send + Sync>;
17+
18+
#[derive(Clone)]
19+
pub struct ElemHock {
20+
pub path: String,
21+
pub callback: HockCallback,
22+
}
23+
1124
#[derive(Clone)]
1225
pub struct AppData {
1326
pub root: MenuRoot,
1427
pub current_key: Vec<String>,
1528
pub needs_save: bool,
1629
pub config: PathBuf,
30+
pub user_data: HashMap<String, String>,
31+
pub temp_data: Option<(String, serde_json::Value)>,
32+
pub elem_hocks: Vec<ElemHock>,
1733
}
1834

1935
const DEFAULT_CONFIG_PATH: &str = ".config.toml";
@@ -39,10 +55,7 @@ impl AppData {
3955
config: Option<impl AsRef<Path>>,
4056
schema: Option<impl AsRef<Path>>,
4157
) -> anyhow::Result<Self> {
42-
let mut init_value_path = PathBuf::from(DEFAULT_CONFIG_PATH);
43-
if let Some(cfg) = config {
44-
init_value_path = cfg.as_ref().to_path_buf();
45-
}
58+
let init_value_path = Self::init_value_path(config);
4659

4760
let schema_path = if let Some(sch) = schema {
4861
sch.as_ref().to_path_buf()
@@ -56,8 +69,60 @@ impl AppData {
5669

5770
let schema_content = fs::read_to_string(&schema_path)?;
5871
let schema_json: serde_json::Value = serde_json::from_str(&schema_content)?;
72+
Self::new_with_schema(Some(init_value_path), &schema_json)
73+
}
74+
75+
fn init_value_path(config: Option<impl AsRef<Path>>) -> PathBuf {
76+
let mut init_value_path = PathBuf::from(DEFAULT_CONFIG_PATH);
77+
if let Some(cfg) = config {
78+
init_value_path = cfg.as_ref().to_path_buf();
79+
}
80+
init_value_path
81+
}
82+
83+
pub fn new_with_init_and_schema(
84+
init: &str,
85+
init_value_path: &Path,
86+
schema: &serde_json::Value,
87+
) -> anyhow::Result<Self> {
88+
let mut root = MenuRoot::try_from(schema)?;
89+
90+
if !init.trim().is_empty() {
91+
let init_json: serde_json::Value = match init_value_path
92+
.extension()
93+
.and_then(|s| s.to_str())
94+
.unwrap_or("")
95+
{
96+
"json" => serde_json::from_str(init)?,
97+
"toml" => {
98+
let v: toml::Value = toml::from_str(init)?;
99+
serde_json::to_value(v)?
100+
}
101+
ext => {
102+
bail!("Unsupported config file extension: {ext:?}");
103+
}
104+
};
105+
root.update_by_value(&init_json)?;
106+
}
107+
108+
Ok(AppData {
109+
root,
110+
current_key: Vec::new(),
111+
needs_save: false,
112+
config: init_value_path.into(),
113+
temp_data: None,
114+
elem_hocks: Vec::new(),
115+
user_data: HashMap::new(),
116+
})
117+
}
118+
119+
pub fn new_with_schema(
120+
config: Option<impl AsRef<Path>>,
121+
schema: &serde_json::Value,
122+
) -> anyhow::Result<Self> {
123+
let init_value_path = Self::init_value_path(config);
59124

60-
let mut root = MenuRoot::try_from(&schema_json)?;
125+
let mut root = MenuRoot::try_from(schema)?;
61126

62127
if init_value_path.exists() {
63128
let init_content = fs::read_to_string(&init_value_path)?;
@@ -85,6 +150,9 @@ impl AppData {
85150
current_key: Vec::new(),
86151
needs_save: false,
87152
config: init_value_path,
153+
temp_data: None,
154+
elem_hocks: Vec::new(),
155+
user_data: HashMap::new(),
88156
})
89157
}
90158

jkconfig/src/data/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub enum ItemType {
3030
Array(ArrayItem),
3131
}
3232

33-
#[derive(Debug, Clone)]
33+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
3434
pub struct ArrayItem {
3535
/// Array element type (e.g., "string", "integer")
3636
pub element_type: String,

jkconfig/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
// #[macro_use]
2+
// extern crate log;
3+
14
#[macro_use]
2-
extern crate log;
5+
mod log;
36

47
pub mod data;
58
// UI模块暂时注释掉,使用主程序中的 MenuView
9+
pub mod run;
610
pub mod ui;
711

812
// Web服务器模块(需要web feature)
913
#[cfg(feature = "web")]
1014
pub mod web;
1115

16+
pub use run::*;
1217
pub use serde_json::Value;

jkconfig/src/log.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// #[cfg(feature = "logging")]
2+
// macro_rules! debug {
3+
// ($($arg:tt)*) => {
4+
// log::debug!($($arg)*);
5+
// };
6+
// }
7+
8+
// #[cfg(not(feature = "logging"))]
9+
// macro_rules! debug {
10+
// ($($arg:tt)*) => {};
11+
// }
12+
13+
#[cfg(feature = "logging")]
14+
macro_rules! info {
15+
($($arg:tt)*) => {
16+
log::info!($($arg)*);
17+
};
18+
}
19+
20+
#[cfg(not(feature = "logging"))]
21+
macro_rules! info {
22+
($($arg:tt)*) => {};
23+
}
24+
25+
#[cfg(feature = "logging")]
26+
macro_rules! warn {
27+
($($arg:tt)*) => {
28+
log::warn!($($arg)*);
29+
};
30+
}
31+
32+
#[cfg(not(feature = "logging"))]
33+
macro_rules! warn {
34+
($($arg:tt)*) => {};
35+
}

jkconfig/src/main.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
extern crate log;
2-
31
use clap::{Parser, Subcommand};
4-
use cursive::{Cursive, CursiveExt, event::Key, views::Dialog};
2+
use cursive::{Cursive, CursiveExt, event::Key};
53
use std::path::PathBuf;
64

75
use jkconfig::{
86
data::AppData,
9-
ui::{components::menu::menu_view, handle_back, handle_quit},
7+
ui::{components::menu::menu_view, handle_back, handle_quit, handle_save},
108
};
119

1210
// mod menu_view;
@@ -106,19 +104,3 @@ fn run_tui(app_data: AppData) -> anyhow::Result<()> {
106104

107105
Ok(())
108106
}
109-
110-
/// 处理保存 - S键
111-
fn handle_save(siv: &mut Cursive) {
112-
siv.add_layer(
113-
Dialog::text("Save and exit?")
114-
.title("Save")
115-
.button("Ok", |s| {
116-
let app = s.user_data::<AppData>().unwrap();
117-
app.needs_save = true;
118-
s.quit();
119-
})
120-
.button("Cancel", |s| {
121-
s.pop_layer();
122-
}),
123-
);
124-
}

0 commit comments

Comments
 (0)