Try to get rid of owned types
Inspired by https://www.youtube.com/watch?v=A4cKi7PTJSs. This turns a lot of cloning Strings and PathBufs into Rcs.
This commit is contained in:
parent
e3b425eb1c
commit
d091265d27
20 changed files with 343 additions and 232 deletions
|
|
@ -102,7 +102,7 @@ impl<_C: CommandRunner, C: Borrow<_C>, D: AsRef<str>, P: AsRef<Path>> Symbol for
|
|||
{
|
||||
Ok(false)
|
||||
} else {
|
||||
Err(String::from_utf8(output.stderr)?.into())
|
||||
Err(std::str::from_utf8(&output.stderr)?.into())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -10,11 +10,11 @@ pub struct Cron<'r, C, U, R> {
|
|||
command_runner: &'r R,
|
||||
}
|
||||
|
||||
impl<'r, U, R> Cron<'r, String, U, R> {
|
||||
impl<'r, U, R> Cron<'r, Box<str>, U, R> {
|
||||
pub fn new<C: AsRef<str>>(user: U, content: C, command_runner: &'r R) -> Self {
|
||||
Self {
|
||||
user,
|
||||
content: String::from(content.as_ref()) + "\n",
|
||||
content: (String::from(content.as_ref()) + "\n").into(),
|
||||
command_runner,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,15 +42,14 @@ impl<P: AsRef<Path>, C: CommandRunner> Symbol for GitSubmodules<'_, P, C> {
|
|||
if !self.target.as_ref().exists() {
|
||||
return Ok(false);
|
||||
}
|
||||
let output = String::from_utf8(
|
||||
self
|
||||
._run_in_target_repo(args!["submodule", "status"])
|
||||
.await?,
|
||||
)?;
|
||||
Ok(
|
||||
output
|
||||
.lines()
|
||||
.all(|line| line.is_empty() || line.starts_with(' ')),
|
||||
std::str::from_utf8(
|
||||
&self
|
||||
._run_in_target_repo(args!["submodule", "status"])
|
||||
.await?,
|
||||
)?
|
||||
.lines()
|
||||
.all(|line| line.is_empty() || line.starts_with(' ')),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ impl<T: AsRef<Path>, C: CommandRunner> Symbol for Install<'_, T, C> {
|
|||
.await?;
|
||||
Ok(
|
||||
result.status.success()
|
||||
&& !String::from_utf8(result.stdout)
|
||||
&& !std::str::from_utf8(&result.stdout)
|
||||
.unwrap()
|
||||
.contains("(empty)"),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ impl<_C: CommandRunner, C: Borrow<_C>, P: AsRef<Path>, S: Storage> Symbol
|
|||
],
|
||||
)
|
||||
.await?;
|
||||
let modified_date = u64::from_str(String::from_utf8(output)?.trim_end())?;
|
||||
let modified_date = u64::from_str(std::str::from_utf8(&output)?.trim_end())?;
|
||||
if if self.dir == StorageDirection::Store {
|
||||
modified_date > dump_date
|
||||
} else {
|
||||
|
|
@ -84,13 +84,17 @@ impl<_C: CommandRunner, C: Borrow<_C>, P: AsRef<Path>, S: Storage> Symbol
|
|||
.borrow()
|
||||
.run_with_args(
|
||||
"diff",
|
||||
args!["-rq", self.storage.read_filename()?, self.path.as_ref()],
|
||||
args![
|
||||
"-rq",
|
||||
self.storage.read_filename()?.as_os_str(),
|
||||
self.path.as_ref()
|
||||
],
|
||||
)
|
||||
.await?;
|
||||
match output.status.code() {
|
||||
Some(0) => Ok(true),
|
||||
Some(1) => Ok(false),
|
||||
_ => Err(String::from_utf8(output.stderr)?.into()),
|
||||
_ => Err(std::str::from_utf8(&output.stderr)?.into()),
|
||||
}
|
||||
} else {
|
||||
Ok(true)
|
||||
|
|
@ -109,7 +113,11 @@ impl<_C: CommandRunner, C: Borrow<_C>, P: AsRef<Path>, S: Storage> Symbol
|
|||
.borrow()
|
||||
.run_successfully(
|
||||
"cp",
|
||||
args!["-a", self.storage.read_filename()?, self.path.as_ref()],
|
||||
args![
|
||||
"-a",
|
||||
self.storage.read_filename()?.as_os_str(),
|
||||
self.path.as_ref()
|
||||
],
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
|
|
@ -118,7 +126,11 @@ impl<_C: CommandRunner, C: Borrow<_C>, P: AsRef<Path>, S: Storage> Symbol
|
|||
.borrow()
|
||||
.run_successfully(
|
||||
"cp",
|
||||
args!["-a", self.path.as_ref(), self.storage.write_filename()],
|
||||
args![
|
||||
"-a",
|
||||
self.path.as_ref(),
|
||||
self.storage.write_filename().as_os_str()
|
||||
],
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,9 +30,9 @@ impl<S: AsRef<Path>, U: AsRef<str>> UserService<'_, S, U> {
|
|||
loop {
|
||||
let result = self.command_runner.run_with_args("systemctl", args).await?;
|
||||
if result.status.success() {
|
||||
return Ok(String::from_utf8(result.stdout)?.trim_end().to_string());
|
||||
return Ok(std::str::from_utf8(&result.stdout)?.trim_end().to_string());
|
||||
}
|
||||
let raw_stderr = String::from_utf8(result.stderr)?;
|
||||
let raw_stderr = std::str::from_utf8(&result.stderr)?;
|
||||
let stderr = raw_stderr.trim_end();
|
||||
if stderr != "Failed to connect to bus: No such file or directory" {
|
||||
return Err(stderr.into());
|
||||
|
|
@ -61,8 +61,8 @@ impl<S: AsRef<Path>, U: AsRef<str>> UserService<'_, S, U> {
|
|||
"ActiveState=active" => return Ok(true),
|
||||
"ActiveState=failed" => {
|
||||
return Err(
|
||||
String::from_utf8(
|
||||
self
|
||||
std::str::from_utf8(
|
||||
&self
|
||||
.command_runner
|
||||
.get_output(
|
||||
"journalctl",
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use std::error::Error;
|
|||
use std::fs::File as FsFile;
|
||||
use std::io;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Plugin<'a, P, N, R> {
|
||||
|
|
@ -24,12 +24,13 @@ impl<'a, P: AsRef<Path>, N: AsRef<str>, R> Plugin<'a, P, N, R> {
|
|||
}
|
||||
}
|
||||
|
||||
fn get_path(&self) -> PathBuf {
|
||||
fn get_path(&self) -> Box<Path> {
|
||||
self
|
||||
.base
|
||||
.as_ref()
|
||||
.join("wp-content/plugins")
|
||||
.join(self.name.as_ref())
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -41,8 +42,8 @@ impl<P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> Symbol for Plugin<'_, P, N
|
|||
return Ok(false);
|
||||
}
|
||||
let path = base_path.join(self.name.as_ref().to_owned() + ".php");
|
||||
let mut version = String::new();
|
||||
let mut plugin_uri = String::new();
|
||||
let mut version: Option<Box<str>> = None;
|
||||
let mut plugin_uri: Option<Box<str>> = None;
|
||||
match FsFile::open(path) {
|
||||
Err(e) => {
|
||||
// Check if file exists
|
||||
|
|
@ -56,11 +57,12 @@ impl<P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> Symbol for Plugin<'_, P, N
|
|||
let reader = BufReader::new(file);
|
||||
let regex = Regex::new("(?m)^(Plugin URI|Version): (.+)$")?;
|
||||
for content in reader.lines() {
|
||||
for matches in regex.captures_iter(&(content?)) {
|
||||
let content = content?;
|
||||
for matches in regex.captures_iter(&content) {
|
||||
if &matches[1] == "Plugin URI" {
|
||||
plugin_uri = matches[2].to_string();
|
||||
plugin_uri = Some(matches[2].into());
|
||||
} else {
|
||||
version = matches[2].to_string();
|
||||
version = Some(matches[2].into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -75,14 +77,14 @@ impl<P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> Symbol for Plugin<'_, P, N
|
|||
format!(
|
||||
r###"plugins={{"plugins":{{"{0}/{0}.php":{{"Version":"{1}", "PluginURI":"{2}"}}}}}}"###,
|
||||
self.name.as_ref(),
|
||||
version,
|
||||
plugin_uri
|
||||
version.as_deref().unwrap_or_default(),
|
||||
plugin_uri.as_deref().unwrap_or_default()
|
||||
),
|
||||
"https://api.wordpress.org/plugins/update-check/1.1/",
|
||||
],
|
||||
)
|
||||
.await?;
|
||||
Ok(String::from_utf8(upstream)?.contains(r###""plugins":[]"###))
|
||||
Ok(std::str::from_utf8(&upstream)?.contains(r###""plugins":[]"###))
|
||||
}
|
||||
|
||||
async fn execute(&self) -> Result<(), Box<dyn Error>> {
|
||||
|
|
@ -97,7 +99,7 @@ impl<P: AsRef<Path>, N: AsRef<str>, R: CommandRunner> Symbol for Plugin<'_, P, N
|
|||
.await?;
|
||||
self
|
||||
.command_runner
|
||||
.run_successfully("rm", args!["-rf", self.get_path()])
|
||||
.run_successfully("rm", args!["-rf", self.get_path().as_os_str()])
|
||||
.await?;
|
||||
self
|
||||
.command_runner
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ use std::fs::File as FsFile;
|
|||
use std::io;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Translation<'a, C, D, R> {
|
||||
|
|
@ -30,7 +29,7 @@ impl<'a, D, C: AsRef<str>, R> Translation<'a, C, D, R> {
|
|||
}
|
||||
|
||||
impl<C: AsRef<str>, D: AsRef<Path>, R: CommandRunner> Translation<'_, C, D, R> {
|
||||
fn get_pairs(&self) -> Vec<(String, PathBuf)> {
|
||||
fn get_pairs(&self) -> impl Iterator<Item = (Box<str>, Box<Path>)> + '_ {
|
||||
let version_x = self
|
||||
.version
|
||||
.trim_end_matches(|c: char| c.is_ascii_digit())
|
||||
|
|
@ -42,21 +41,19 @@ impl<C: AsRef<str>, D: AsRef<Path>, R: CommandRunner> Translation<'_, C, D, R> {
|
|||
} else {
|
||||
locale.to_lowercase().replace('_', "-")
|
||||
};
|
||||
let mut res = vec![];
|
||||
for &(in_slug, out_slug) in &[
|
||||
[
|
||||
("", ""),
|
||||
("cc/", "continents-cities-"),
|
||||
("admin/", "admin-"),
|
||||
("admin/network/", "admin-network-"),
|
||||
] {
|
||||
for format in &["po", "mo"] {
|
||||
res.push((
|
||||
format!("https://translate.wordpress.org/projects/wp/{version_x}/{in_slug}{path_locale}/default/export-translations?format={format}"),
|
||||
[self.path.as_ref(), format!("{}{}.{}", out_slug, self.locale.as_ref(), format).as_ref()].iter().collect()
|
||||
));
|
||||
].into_iter().flat_map(move |(in_slug, out_slug)|{
|
||||
["po", "mo"].map(|format|
|
||||
(
|
||||
format!("https://translate.wordpress.org/projects/wp/{version_x}/{in_slug}{path_locale}/default/export-translations?format={format}").into(),
|
||||
self.path.as_ref().join(format!("{}{}.{}", out_slug, self.locale.as_ref(), format)).into()
|
||||
))
|
||||
}
|
||||
}
|
||||
res
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,7 +77,7 @@ impl<C: AsRef<str>, D: AsRef<Path>, R: CommandRunner> Symbol for Translation<'_,
|
|||
let reader = BufReader::new(file);
|
||||
for content in reader.lines() {
|
||||
if let Some(match_result) = match_date.captures(&content?) {
|
||||
newest = max(newest, match_result[1].to_string());
|
||||
newest = max(newest, match_result[1].into());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -99,7 +96,7 @@ impl<C: AsRef<str>, D: AsRef<Path>, R: CommandRunner> Symbol for Translation<'_,
|
|||
)],
|
||||
)
|
||||
.await?;
|
||||
Ok(String::from_utf8(upstream)?.contains(&format!(
|
||||
Ok(std::str::from_utf8(&upstream)?.contains(&format!(
|
||||
r###"language":"{}","version":"{}","updated":"{}"###,
|
||||
self.locale.as_ref(),
|
||||
self.version,
|
||||
|
|
@ -111,7 +108,10 @@ impl<C: AsRef<str>, D: AsRef<Path>, R: CommandRunner> Symbol for Translation<'_,
|
|||
for (source, target) in self.get_pairs() {
|
||||
self
|
||||
.command_runner
|
||||
.run_successfully("curl", args!["--compressed", "-o", target, source,])
|
||||
.run_successfully(
|
||||
"curl",
|
||||
args!["--compressed", "-o", target.as_os_str(), source.as_ref(),],
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue