From 52795ff74c3bd15ee7f7b642d5a9a8da98023f7e Mon Sep 17 00:00:00 2001 From: cryptoAtwill <108330426+cryptoAtwill@users.noreply.github.com> Date: Fri, 24 Feb 2023 19:38:21 +0800 Subject: [PATCH] move to lib (#30) * move to lib * Update src/config/mod.rs Co-authored-by: Akosh Farkash * rename config function name * import path * rename crate --------- Co-authored-by: Akosh Farkash --- Cargo.toml | 2 +- src/config/mod.rs | 16 ++++++++-------- src/jsonrpc/mod.rs | 4 ++-- src/lib.rs | 7 +++++++ src/main.rs | 7 +------ src/server/jsonrpc.rs | 6 +++--- 6 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 5afe0516..9dd89477 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" license-file = "LICENSE" [package] -name = "ipc-client" +name = "ipc_agent" version = "0.1.0" edition.workspace = true license-file.workspace = true diff --git a/src/config/mod.rs b/src/config/mod.rs index 5bca34f3..4baac8db 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -9,6 +9,7 @@ mod subnet; use std::collections::HashMap; use std::fs; +use std::path::Path; use anyhow::Result; use serde::Deserialize; @@ -21,29 +22,28 @@ pub const JSON_RPC_VERSION: &str = "2.0"; /// The top-level struct representing the config. Calls to [`Config::from_file`] deserialize into /// this struct. #[derive(Deserialize)] -pub(crate) struct Config { +pub struct Config { pub server: Server, pub subnets: HashMap, } impl Config { /// Reads a TOML configuration in the `s` string and returns a [`Config`] struct. - pub fn from_str(s: &str) -> Result { - let config = toml::from_str(&s)?; + pub fn from_toml_str(s: &str) -> Result { + let config = toml::from_str(s)?; Ok(config) } /// Reads a TOML configuration file specified in the `path` and returns a [`Config`] struct. - pub fn from_file(path: &str) -> Result { + pub fn from_file(path: impl AsRef) -> Result { let contents = fs::read_to_string(path)?; - let config: Config = Config::from_str(contents.as_str())?; + let config: Config = Config::from_toml_str(contents.as_str())?; Ok(config) } } #[cfg(test)] mod tests { - use std::collections::HashMap; use std::net::SocketAddr; use std::str::FromStr; @@ -52,7 +52,7 @@ mod tests { use ipc_sdk::subnet_id::{ROOTNET_ID, SubnetID}; use url::Url; - use crate::config::{Config, Server, Subnet}; + use crate::config::Config; // Arguments for the config's fields const SERVER_JSON_RPC_ADDR: &str = "127.0.0.1:3030"; @@ -134,6 +134,6 @@ mod tests { "# ); - Config::from_str(config_str.as_str()).unwrap() + Config::from_toml_str(config_str.as_str()).unwrap() } } diff --git a/src/jsonrpc/mod.rs b/src/jsonrpc/mod.rs index b49ea5b6..92f665a8 100644 --- a/src/jsonrpc/mod.rs +++ b/src/jsonrpc/mod.rs @@ -43,7 +43,7 @@ pub trait JsonRpcClient { /// /// # Examples /// ```no_run -/// use ipc_client::{JsonRpcClientImpl, LotusClient, LotusJsonRPCClient}; +/// use ipc_agent::{jsonrpc::JsonRpcClientImpl, lotus::LotusClient, lotus::LotusJsonRPCClient}; /// /// #[tokio::main] /// async fn main() { @@ -51,7 +51,7 @@ pub trait JsonRpcClient { /// let n = LotusJsonRPCClient::new(h); /// println!( /// "wallets: {:?}", -/// n.wallet_new(ipc_client::WalletKeyType::Secp256k1).await +/// n.wallet_new(ipc_agent::lotus::WalletKeyType::Secp256k1).await /// ); /// } /// ``` diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..0bfbb0d0 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,7 @@ +pub mod cli; +pub mod config; +pub mod jsonrpc; +pub mod lotus; +pub mod manager; +pub mod server; + diff --git a/src/main.rs b/src/main.rs index a9e6de4a..bddfd094 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,4 @@ -mod cli; -mod config; -mod jsonrpc; -mod lotus; -mod manager; -mod server; +use ipc_agent::cli; #[tokio::main] async fn main() { diff --git a/src/server/jsonrpc.rs b/src/server/jsonrpc.rs index 87df0a8d..64f30d93 100644 --- a/src/server/jsonrpc.rs +++ b/src/server/jsonrpc.rs @@ -15,12 +15,12 @@ use crate::config::JSON_RPC_ENDPOINT; /// /// # Examples /// ```no_run -/// use agent::config::Config; -/// use agent::node::jsonrpc::JsonRPCServer; +/// use ipc_agent::config::Config; +/// use ipc_agent::server::jsonrpc::JsonRPCServer; /// /// #[tokio::main] /// async fn main() { -/// let config = Config::from_file("PATH TO YOUR CONFIG FILE"); +/// let config = Config::from_file("PATH TO YOUR CONFIG FILE").unwrap(); /// let n = JsonRPCServer::new(config.server); /// n.run().await; /// }