refactor: use &GlobalConfig to avoid clone (#217)

pull/218/head
sigoden 8 months ago committed by GitHub
parent 81624f7267
commit 87aec71e08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -52,7 +52,7 @@ macro_rules! register_client {
impl $client {
pub const NAME: &str = $name;
pub fn init(global_config: $crate::config::GlobalConfig) -> Option<Box<dyn Client>> {
pub fn init(global_config: &$crate::config::GlobalConfig) -> Option<Box<dyn Client>> {
let model = global_config.read().model.clone();
let config = {
if let ClientConfig::$config(c) = &global_config.read().clients[model.client_index] {
@ -62,7 +62,7 @@ macro_rules! register_client {
}
};
Some(Box::new(Self {
global_config,
global_config: global_config.clone(),
config,
model,
}))
@ -75,9 +75,9 @@ macro_rules! register_client {
)+
pub fn init_client(config: $crate::config::GlobalConfig) -> anyhow::Result<Box<dyn Client>> {
pub fn init_client(config: &$crate::config::GlobalConfig) -> anyhow::Result<Box<dyn Client>> {
None
$(.or_else(|| $client::init(config.clone())))+
$(.or_else(|| $client::init(config)))+
.ok_or_else(|| {
let model = config.read().model.clone();
anyhow::anyhow!(

@ -78,11 +78,11 @@ fn main() -> Result<()> {
exit(0);
}
let no_stream = cli.no_stream;
let client = init_client(config.clone())?;
let client = init_client(&config)?;
if stdin().is_terminal() {
match text {
Some(text) => start_directive(client.as_ref(), &config, &text, no_stream),
None => start_interactive(config),
None => start_interactive(&config),
}
} else {
let mut input = String::new();
@ -128,8 +128,8 @@ fn start_directive(
config.write().save_message(input, &output)
}
fn start_interactive(config: GlobalConfig) -> Result<()> {
fn start_interactive(config: &GlobalConfig) -> Result<()> {
cl100k_base_singleton();
let mut repl: Repl = Repl::init(config.clone())?;
let mut repl: Repl = Repl::init(config)?;
repl.run()
}

@ -60,17 +60,17 @@ pub struct Repl {
}
impl Repl {
pub fn init(config: GlobalConfig) -> Result<Self> {
let editor = Self::create_editor(&config)?;
pub fn init(config: &GlobalConfig) -> Result<Self> {
let editor = Self::create_editor(config)?;
let prompt = ReplPrompt::new(config.clone());
let prompt = ReplPrompt::new(config);
let abort = create_abort_signal();
let clipboard = Clipboard::new().map(RefCell::new);
Ok(Self {
config,
config: config.clone(),
editor,
prompt,
clipboard,
@ -228,7 +228,7 @@ impl Repl {
}
self.config.read().maybe_print_send_tokens(input);
let wg = WaitGroup::new();
let client = init_client(self.config.clone())?;
let client = init_client(&self.config)?;
let ret = render_stream(
input,
client.as_ref(),

@ -15,8 +15,10 @@ pub struct ReplPrompt {
}
impl ReplPrompt {
pub fn new(config: GlobalConfig) -> Self {
Self { config }
pub fn new(config: &GlobalConfig) -> Self {
Self {
config: config.clone(),
}
}
}

Loading…
Cancel
Save