Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: refine rust codes in tman and ten_rust #455

Merged
merged 3 commits into from
Dec 23, 2024
Merged
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
4 changes: 2 additions & 2 deletions core/src/ten_manager/src/cmd/cmd_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@ pub async fn execute_cmd(
// Case 2: tman install

let manifest = parse_manifest_in_folder(&cwd)?;
affected_pkg_type = manifest.pkg_type.parse::<PkgType>()?;
affected_pkg_name = manifest.name.clone();
affected_pkg_type = manifest.type_and_name.pkg_type;
affected_pkg_name = manifest.type_and_name.name.clone();

match affected_pkg_type {
PkgType::App => {
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/designer/graphs/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ pub async fn get_graph_nodes(
let pkg_info = pkg_info.unwrap();
resp_extensions.push(DesignerExtension {
addon: extension.addon.clone(),
name: extension.name.clone(),
name: extension.type_and_name.name.clone(),
extension_group: extension.extension_group.clone().unwrap(),
app: extension.app.as_ref().unwrap().clone(),
api: pkg_info.api.as_ref().map(|api| DesignerApi {
Expand Down
2 changes: 1 addition & 1 deletion core/src/ten_manager/src/designer/messages/compatible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl From<CompatibleExtensionAndMsg<'_>> for DesignerCompatibleMsg {
.clone()
.unwrap()
.clone(),
extension: compatible.extension.name.clone(),
extension: compatible.extension.type_and_name.name.clone(),
msg_type: compatible.msg_type,
msg_direction: compatible.msg_direction,
msg_name: compatible.msg_name,
Expand Down
4 changes: 2 additions & 2 deletions core/src/ten_manager/src/package_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ pub fn create_package_zip_file(
.map_err(|e| {
anyhow::anyhow!(
"Failed to check property.json for {}:{}, {}",
manifest.pkg_type,
manifest.name,
manifest.type_and_name.pkg_type,
manifest.type_and_name.name,
e
)
})?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
// Licensed under the Apache License, Version 2.0, with certain conditions.
// Refer to the "LICENSE" file in the root directory for more information.
//
use ten_rust::pkg_info::{graph::GraphNode, pkg_type::PkgType};
use ten_rust::pkg_info::{
graph::GraphNode, pkg_type::PkgType, pkg_type_and_name::PkgTypeAndName,
};

use crate::designer::graphs::nodes::DesignerExtension;

impl From<DesignerExtension> for GraphNode {
fn from(designer_extension: DesignerExtension) -> Self {
GraphNode {
node_type: PkgType::Extension,
name: designer_extension.name,
type_and_name: PkgTypeAndName {
pkg_type: PkgType::Extension,
name: designer_extension.name,
},
addon: designer_extension.addon,
extension_group: Some(designer_extension.extension_group.clone()),
app: Some(designer_extension.app),
Expand Down
22 changes: 5 additions & 17 deletions core/src/ten_manager/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::path::{Path, PathBuf};
use std::{env, io};

use anyhow::{anyhow, Result};
use ten_rust::pkg_info::pkg_type::PkgType;

use super::error::TmanError;

Expand Down Expand Up @@ -66,29 +67,16 @@ pub fn read_file_to_string<P: AsRef<Path>>(
pub fn check_is_app_folder(path: &Path) -> Result<()> {
let manifest =
ten_rust::pkg_info::manifest::parse_manifest_in_folder(path)?;
if manifest.pkg_type != "app" {
if manifest.type_and_name.pkg_type != PkgType::App {
return Err(anyhow!("The `type` in manifest.json is not `app`."));
}

Ok(())
}

pub fn check_is_package_folder(path: &Path) -> Result<()> {
let manifest =
ten_rust::pkg_info::manifest::parse_manifest_in_folder(path)?;
if manifest.pkg_type == "app"
|| manifest.pkg_type == "system"
|| manifest.pkg_type == "extension"
|| manifest.pkg_type == "extension_group"
|| manifest.pkg_type == "protocol"
{
return Err(TmanError::InvalidPath(
path.to_string_lossy().to_string(),
format!("package type {} is not correct", manifest.pkg_type)
.to_string(),
)
.into());
match ten_rust::pkg_info::manifest::parse_manifest_in_folder(path) {
Ok(_) => Ok(()),
Err(err) => Err(err),
}

Ok(())
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class _Msg:
extension_group: str | None,
extension: str | None,
) -> None: ...
def set_property_from_json(self, path: str, json_str: str) -> None: ...
def set_property_from_json(
self, path: str | None, json_str: str
) -> None: ...
def get_property_to_json(self, path: str | None = None) -> str: ...
def get_property_int(self, path: str) -> int: ...
def set_property_int(self, path: str, value: int) -> None: ...
Expand Down
2 changes: 0 additions & 2 deletions core/src/ten_rust/src/pkg_info/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ pub const EXTENSION_GROUP_DIR: &str = "extension_group";
pub const PROTOCOL_DIR: &str = "protocol";
pub const SYSTEM_DIR: &str = "system";

pub const APP_PKG_TYPE: &str = "app";

pub const ERR_STR_NOT_APP_DIR: &str =
"The current working directory does not belong to the `app`.";
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl Graph {
self.nodes
.iter()
.find_map(|node| {
if node.node_type == PkgType::Extension
&& node.name.as_str() == extension
if node.type_and_name.pkg_type == PkgType::Extension
&& node.type_and_name.name.as_str() == extension
&& node.get_app_uri() == app
{
Some(node.addon.as_str())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ impl Graph {

let mut all_extensions: Vec<String> = Vec::new();
for node in &self.nodes {
if node.node_type == PkgType::Extension {
let unique_ext_name =
format!("{}:{}", node.get_app_uri(), node.name);
if node.type_and_name.pkg_type == PkgType::Extension {
let unique_ext_name = format!(
"{}:{}",
node.get_app_uri(),
node.type_and_name.name
);
all_extensions.push(unique_ext_name);
}
}
Expand Down
15 changes: 9 additions & 6 deletions core/src/ten_rust/src/pkg_info/graph/check/duplicated_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ impl Graph {
let mut all_extension_groups: Vec<String> = Vec::new();

for (node_idx, node) in self.nodes.iter().enumerate() {
match node.node_type {
match node.type_and_name.pkg_type {
PkgType::Extension => {
let unique_ext_name = format!(
"{}:{}:{}",
node.get_app_uri(),
node.extension_group.as_ref().unwrap(),
node.name
node.type_and_name.name
);

if all_extensions.contains(&unique_ext_name) {
return Err(anyhow::anyhow!(
"Duplicated extension was found in nodes[{}], addon: {}, name: {}.",
node_idx, node.addon, node.name
node_idx, node.addon, node.type_and_name.name
));
}

Expand All @@ -45,13 +45,16 @@ impl Graph {
}

PkgType::ExtensionGroup => {
let unique_ext_group_name =
format!("{}:{}", node.get_app_uri(), node.name);
let unique_ext_group_name = format!(
"{}:{}",
node.get_app_uri(),
node.type_and_name.name
);

if all_extension_groups.contains(&unique_ext_group_name) {
return Err(anyhow::anyhow!(
"Duplicated extension_group was found in nodes[{}], addon: {}, name: {}.",
node_idx, node.addon, node.name
node_idx, node.addon, node.type_and_name.name
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Graph {
if !skip_if_app_not_exist {
not_installed_pkgs.push((
node_app_uri.to_string(),
node.node_type,
node.type_and_name.pkg_type,
node.addon.clone(),
));
}
Expand All @@ -41,14 +41,15 @@ impl Graph {

// Check if the graph node exists in the specified app.
let found = existed_pkgs_of_app.iter().find(|pkg| {
pkg.basic_info.type_and_name.pkg_type == node.node_type
pkg.basic_info.type_and_name.pkg_type
== node.type_and_name.pkg_type
&& pkg.basic_info.type_and_name.name == node.addon
&& pkg.is_local_installed
});
if found.is_none() {
not_installed_pkgs.push((
node_app_uri.to_string(),
node.node_type,
node.type_and_name.pkg_type,
node.addon.clone(),
));
}
Expand Down
12 changes: 6 additions & 6 deletions core/src/ten_rust/src/pkg_info/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use anyhow::Result;
use msg_conversion::MsgAndResultConversion;
use serde::{Deserialize, Serialize};

use super::{pkg_type::PkgType, PkgInfo};
use super::{pkg_type::PkgType, pkg_type_and_name::PkgTypeAndName, PkgInfo};
use crate::pkg_info::localhost;

/// The state of the 'app' field declaration in all nodes in the graph.
Expand Down Expand Up @@ -214,9 +214,9 @@ impl Graph {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GraphNode {
#[serde(rename = "type")]
pub node_type: PkgType,
pub name: String,
#[serde(flatten)]
pub type_and_name: PkgTypeAndName,

pub addon: String,

// extension group node does not contain extension_group field.
Expand All @@ -236,12 +236,12 @@ impl GraphNode {
graph_node_app_declaration: &GraphNodeAppDeclaration,
) -> Result<()> {
// Extension node must specify extension_group name.
if self.node_type == PkgType::Extension
if self.type_and_name.pkg_type == PkgType::Extension
&& self.extension_group.is_none()
{
return Err(anyhow::anyhow!(
"Node '{}' of type 'extension' must have an 'extension_group' defined.",
self.name
self.type_and_name.name
));
}

Expand Down
12 changes: 8 additions & 4 deletions core/src/ten_rust/src/pkg_info/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ use dependency::ManifestDependency;
use publish::PackageConfig;
use support::ManifestSupport;

use super::pkg_type_and_name::PkgTypeAndName;

// Define a structure that mirrors the structure of the JSON file.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Manifest {
#[serde(rename = "type")]
pub pkg_type: String,
pub name: String,
#[serde(flatten)]
pub type_and_name: PkgTypeAndName,

pub version: String,

#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -113,6 +115,8 @@ pub fn parse_manifest_in_folder(folder_path: &Path) -> Result<Manifest> {

#[cfg(test)]
mod tests {
use crate::pkg_info::pkg_type::PkgType;

use super::*;

#[test]
Expand All @@ -125,7 +129,7 @@ mod tests {
assert!(result.is_ok());

let manifest = result.unwrap();
assert_eq!(manifest.pkg_type, "extension");
assert_eq!(manifest.type_and_name.pkg_type, PkgType::Extension);

let cmd_in = manifest.api.unwrap().cmd_in.unwrap();
assert_eq!(cmd_in.len(), 1);
Expand Down
30 changes: 14 additions & 16 deletions core/src/ten_rust/src/pkg_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ use pkg_type_and_name::PkgTypeAndName;
use crate::schema::store::SchemaStore;
use api::PkgApi;
use constants::{
APP_PKG_TYPE, ERR_STR_NOT_APP_DIR, EXTENSION_DIR, EXTENSION_GROUP_DIR,
ERR_STR_NOT_APP_DIR, EXTENSION_DIR, EXTENSION_GROUP_DIR,
MANIFEST_JSON_FILENAME, PROTOCOL_DIR, SYSTEM_DIR, TEN_PACKAGES_DIR,
};
use dependencies::{get_pkg_dependencies_from_manifest, PkgDependency};
use manifest::{parse_manifest_from_file, parse_manifest_in_folder, Manifest};
use pkg_type::PkgType;
use property::{
parse_property_from_file, parse_property_in_folder,
predefined_graph::PropertyPredefinedGraph, Property,
predefined_graph::PredefinedGraph, Property,
};

pub fn localhost() -> String {
Expand Down Expand Up @@ -139,9 +139,7 @@ impl PkgInfo {
Ok(property_pkg_json == property_fs_json)
}

pub fn get_predefined_graphs(
&self,
) -> Option<&Vec<PropertyPredefinedGraph>> {
pub fn get_predefined_graphs(&self) -> Option<&Vec<PredefinedGraph>> {
if let Some(property) = &self.property {
if let Some(ten) = &property._ten {
return ten.predefined_graphs.as_ref();
Expand All @@ -151,10 +149,7 @@ impl PkgInfo {
None
}

pub fn update_predefined_graph(
&mut self,
new_graph: &PropertyPredefinedGraph,
) {
pub fn update_predefined_graph(&mut self, new_graph: &PredefinedGraph) {
if let Some(property) = &mut self.property {
if let Some(ten) = &mut property._ten {
if let Some(predefined_graphs) = &mut ten.predefined_graphs {
Expand Down Expand Up @@ -223,7 +218,7 @@ pub fn get_all_existed_pkgs_info_of_app_to_hashmap(
// Process the manifest.json file in the root path.
let app_pkg_manifest =
collect_pkg_info_from_path(app_path, &mut pkgs_info)?;
if app_pkg_manifest.pkg_type != APP_PKG_TYPE {
if app_pkg_manifest.type_and_name.pkg_type != PkgType::App {
return Err(anyhow::anyhow!(ERR_STR_NOT_APP_DIR));
}

Expand All @@ -246,28 +241,31 @@ pub fn get_all_existed_pkgs_info_of_app_to_hashmap(
parse_manifest_from_file(&manifest_path)?;

// Do some simple checks.
if manifest.name
if manifest.type_and_name.name
!= path.file_name().unwrap().to_str().unwrap()
{
return Err(anyhow::anyhow!(
"The path '{}' is not valid: {}.",
format!("{}:{}",manifest.pkg_type, manifest.name),
format!("{}:{}",manifest.type_and_name.pkg_type, manifest.type_and_name.name),
format!(
"the path '{}' and the name '{}' of the package are different",
path.file_name().unwrap().to_str().unwrap(), manifest.name
path.file_name().unwrap().to_str().unwrap(), manifest.type_and_name.name
)));
}

if manifest.pkg_type != addon_type {
if manifest.type_and_name.pkg_type.to_string()
!= addon_type
{
return Err(anyhow::anyhow!(
"The path '{}' is not valid: {}.",
format!(
"{}:{}",
manifest.pkg_type, manifest.name
manifest.type_and_name.pkg_type,
manifest.type_and_name.name
),
format!(
"the package type '{}' is not as expected '{}'",
manifest.pkg_type, addon_type)
manifest.type_and_name.pkg_type, addon_type)
));
}

Expand Down
Loading
Loading