Skip to content

Commit db1e9a4

Browse files
committed
cargo fmt
1 parent 39a2e40 commit db1e9a4

File tree

5 files changed

+148
-92
lines changed

5 files changed

+148
-92
lines changed

src/index.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub enum Index {
3333
token: Option<String>,
3434

3535
#[clap(long, conflicts_with = "token")]
36-
github_token: Option<String>
36+
github_token: Option<String>,
3737
},
3838

3939
/// Invalidate all existing access tokens (logout)
@@ -353,15 +353,13 @@ pub fn subcommand(cmd: Index) {
353353
match cmd {
354354
Index::Install { id, version } => {
355355
let config = Config::new().assert_is_setup();
356-
install_mod(
357-
&config,
358-
&id,
359-
&version.unwrap_or(VersionReq::STAR),
360-
false,
361-
);
356+
install_mod(&config, &id, &version.unwrap_or(VersionReq::STAR), false);
362357
done!("Mod installed");
363358
}
364-
Index::Login { token, github_token } => index_auth::login(config, token, github_token),
359+
Index::Login {
360+
token,
361+
github_token,
362+
} => index_auth::login(config, token, github_token),
365363
Index::Invalidate => index_auth::invalidate(config),
366364
Index::Url { url } => {
367365
if let Some(u) = url {

src/index_auth.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ pub fn login(config: &mut Config, token: Option<String>, github_token: Option<St
6060

6161
let parsed: ApiResponse<String> = match response.status().as_u16() {
6262
400 => fatal!("Invalid Github Token"),
63-
200 => response.json().nice_unwrap("Unable to parse login response"),
63+
200 => response
64+
.json()
65+
.nice_unwrap("Unable to parse login response"),
6466
_ => fatal!("Unable to connect to Geode Index"),
6567
};
6668

@@ -115,10 +117,7 @@ fn poll_login(
115117
config: &mut Config,
116118
) -> Option<String> {
117119
let response = client
118-
.post(index::get_index_url(
119-
"/v1/login/github/poll",
120-
config,
121-
))
120+
.post(index::get_index_url("/v1/login/github/poll", config))
122121
.json(&json!({ "uuid": uuid }))
123122
.header(USER_AGENT, "GeodeCLI")
124123
.send()

src/project.rs

+95-51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use serde_json::json;
2-
use clap::ValueEnum;
3-
use serde::Deserialize;
41
use crate::logging::ask_value;
52
use crate::mod_file::{PlatformName, ToGeodeString};
63
use crate::util::mod_file::DependencyImportance;
@@ -14,22 +11,25 @@ use crate::{
1411
},
1512
};
1613
use clap::Subcommand;
14+
use clap::ValueEnum;
1715
use edit_distance::edit_distance;
1816
use semver::Version;
17+
use serde::Deserialize;
18+
use serde_json::json;
19+
use serde_json::Value;
1920
use std::env;
2021
use std::{
2122
collections::HashMap,
2223
fs,
2324
path::{Path, PathBuf},
2425
};
25-
use serde_json::Value;
2626

2727
#[derive(Debug, Deserialize, Hash, PartialEq, Eq, Clone, Copy, ValueEnum)]
2828
#[serde(rename_all = "lowercase")]
2929
pub enum ResourceType {
3030
Sprite,
3131
Font,
32-
File
32+
File,
3333
}
3434

3535
#[derive(Subcommand, Debug)]
@@ -69,8 +69,8 @@ pub enum Project {
6969
Add {
7070
/// Type of resource to add
7171
resource: ResourceType,
72-
files: Vec<PathBuf>
73-
}
72+
files: Vec<PathBuf>,
73+
},
7474
}
7575

7676
fn find_build_directory(root: &Path) -> Option<PathBuf> {
@@ -534,75 +534,121 @@ pub fn check_dependencies(
534534
}
535535

536536
fn add_resource(dir: &Path, resource: ResourceType, files: Vec<PathBuf>) {
537-
let mut mod_json: HashMap<String, Value> = serde_json::from_reader(fs::File::open(dir.join("mod.json")).ok().nice_unwrap("Must be inside a project with a mod.json"))
538-
.nice_unwrap("Unable to read mod.json");
539-
537+
let mut mod_json: HashMap<String, Value> = serde_json::from_reader(
538+
fs::File::open(dir.join("mod.json"))
539+
.ok()
540+
.nice_unwrap("Must be inside a project with a mod.json"),
541+
)
542+
.nice_unwrap("Unable to read mod.json");
540543

541544
let mut do_thing = |name: &str, othername: &str| {
542-
let resource = mod_json.get("resources")
545+
let resource = mod_json
546+
.get("resources")
543547
.and_then(|x| x.get(name))
544548
.and_then(|x| x.as_array())
545549
.unwrap_or(&vec![])
546550
.clone();
547551

548-
let mut new_resource: Vec<Value> = resource.into_iter().chain(files.clone().into_iter().filter_map(|x| {
549-
if !x.exists() {
550-
warn!("{} {} does not exist", othername, x.display());
551-
None
552-
} else {
553-
Some(Value::String(x.as_os_str().to_str().unwrap().to_string()))
554-
}
555-
})).collect();
552+
let mut new_resource: Vec<Value> = resource
553+
.into_iter()
554+
.chain(files.clone().into_iter().filter_map(|x| {
555+
if !x.exists() {
556+
warn!("{} {} does not exist", othername, x.display());
557+
None
558+
} else {
559+
Some(Value::String(x.as_os_str().to_str().unwrap().to_string()))
560+
}
561+
}))
562+
.collect();
556563

557-
let mut duplicates: Vec<_> = new_resource.iter().filter(|x| new_resource.iter().filter(|y| y == x).count() > 1).collect();
564+
let mut duplicates: Vec<_> = new_resource
565+
.iter()
566+
.filter(|x| new_resource.iter().filter(|y| y == x).count() > 1)
567+
.collect();
558568
duplicates.dedup();
559-
duplicates.into_iter().for_each(|x| warn!("Duplicate {}: {}", othername, x));
569+
duplicates
570+
.into_iter()
571+
.for_each(|x| warn!("Duplicate {}: {}", othername, x));
560572

561573
new_resource.dedup();
562574

563-
*mod_json.entry("resources".to_string()).or_insert(json!({}))
564-
.as_object_mut().nice_unwrap("resources is not an object")
565-
.entry(name.to_string()).or_insert(json!([]))
566-
.as_array_mut().nice_unwrap(&format!("{} is not an array", name)) = new_resource;
575+
*mod_json
576+
.entry("resources".to_string())
577+
.or_insert(json!({}))
578+
.as_object_mut()
579+
.nice_unwrap("resources is not an object")
580+
.entry(name.to_string())
581+
.or_insert(json!([]))
582+
.as_array_mut()
583+
.nice_unwrap(&format!("{} is not an array", name)) = new_resource;
567584
};
568585

569586
match resource {
570587
ResourceType::Sprite => do_thing("sprites", "Sprite"),
571588
ResourceType::File => do_thing("files", "File"),
572589

573590
ResourceType::Font => {
574-
let fonts = mod_json.get("resources")
591+
let fonts = mod_json
592+
.get("resources")
575593
.and_then(|x| x.get("fonts"))
576594
.and_then(|x| x.as_array())
577595
.unwrap_or(&vec![])
578596
.clone();
579597

580-
let mut new_fonts: Vec<Value> = fonts.into_iter().chain(files.into_iter().filter_map(|x| {
581-
if !x.exists() {
582-
warn!("Font {} does not exist", x.display());
583-
None
584-
} else {
585-
Some(json!({
586-
"path": x.as_os_str().to_str().unwrap().to_string(),
587-
"size": ask_value("Font Size", None, true).parse::<u32>().ok().nice_unwrap("Invalid font size!")
588-
}))
589-
}
590-
})).collect();
591-
592-
let mut duplicates: Vec<_> = new_fonts.iter().filter_map(|x| x.get("path")).filter(|x| new_fonts.iter().filter(|y| y.get("path").map(|y| y == *x).unwrap_or(false)).count() > 1).collect();
598+
let mut new_fonts: Vec<Value> = fonts
599+
.into_iter()
600+
.chain(files.into_iter().filter_map(|x| {
601+
if !x.exists() {
602+
warn!("Font {} does not exist", x.display());
603+
None
604+
} else {
605+
let size = ask_value("Font Size", None, true)
606+
.parse::<u32>()
607+
.ok()
608+
.nice_unwrap("Invalid font size!");
609+
610+
Some(json!({
611+
"path": x.as_os_str().to_str().unwrap().to_string(),
612+
"size": size
613+
}))
614+
}
615+
}))
616+
.collect();
617+
618+
let mut duplicates: Vec<_> = new_fonts
619+
.iter()
620+
.filter_map(|x| x.get("path"))
621+
.filter(|x| {
622+
new_fonts
623+
.iter()
624+
.filter(|y| y.get("path").map(|y| y == *x).unwrap_or(false))
625+
.count() > 1
626+
})
627+
.collect();
593628
duplicates.dedup();
594-
duplicates.into_iter().for_each(|x| warn!("Duplicate Font: {}", x));
629+
duplicates
630+
.into_iter()
631+
.for_each(|x| warn!("Duplicate Font: {}", x));
595632

596633
new_fonts.dedup();
597634

598-
*mod_json.entry("resources".to_string()).or_insert(json!({}))
599-
.as_object_mut().nice_unwrap("resources is not an object")
600-
.entry("fonts".to_string()).or_insert(json!([]))
601-
.as_array_mut().nice_unwrap("fonts is not an array") = new_fonts;
635+
*mod_json
636+
.entry("resources".to_string())
637+
.or_insert(json!({}))
638+
.as_object_mut()
639+
.nice_unwrap("resources is not an object")
640+
.entry("fonts".to_string())
641+
.or_insert(json!([]))
642+
.as_array_mut()
643+
.nice_unwrap("fonts is not an array") = new_fonts;
602644
}
603-
};
645+
};
604646

605-
fs::write(dir.join("mod.json"), serde_json::to_string_pretty(&mod_json).unwrap()).nice_unwrap("Failed to save mod.json");
647+
fs::write(
648+
dir.join("mod.json"),
649+
serde_json::to_string_pretty(&mod_json).unwrap(),
650+
)
651+
.nice_unwrap("Failed to save mod.json");
606652

607653
done!("Resource added to mod.json");
608654
}
@@ -621,10 +667,8 @@ pub fn subcommand(cmd: Project) {
621667
platform,
622668
externals,
623669
),
624-
Project::Add { resource, files } => add_resource(
625-
&std::env::current_dir().unwrap(),
626-
resource,
627-
files
628-
),
670+
Project::Add { resource, files } => {
671+
add_resource(&std::env::current_dir().unwrap(), resource, files)
672+
}
629673
}
630674
}

src/template.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,14 @@ pub fn build_template(location: Option<PathBuf>) {
247247

248248
let mod_id = format!(
249249
"{}.{}",
250-
final_developer.to_lowercase().replace(' ', "_").replace("\"", ""),
251-
final_name.to_lowercase().replace(' ', "_").replace("\"", "")
250+
final_developer
251+
.to_lowercase()
252+
.replace(' ', "_")
253+
.replace("\"", ""),
254+
final_name
255+
.to_lowercase()
256+
.replace(' ', "_")
257+
.replace("\"", "")
252258
);
253259

254260
let action = ask_confirm("Do you want to add the cross-platform Github action?", true);

src/util/mod_file.rs

+35-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::spritesheet::SpriteSheet;
22
use crate::NiceUnwrap;
33
use clap::ValueEnum;
44
use semver::{Version, VersionReq};
5-
use serde::{Deserialize, Deserializer, de::Error};
5+
use serde::{de::Error, Deserialize, Deserializer};
66
use std::collections::{HashMap, HashSet};
77
use std::fmt::Display;
88
use std::fs;
@@ -338,47 +338,56 @@ fn parse_dependencies<'de, D>(deserializer: D) -> Result<Dependencies, D::Error>
338338
where
339339
D: Deserializer<'de>,
340340
{
341-
// This is all to avoid union types having terrible errors
341+
// This is all to avoid union types having terrible errors
342342
// (they just log "failed to parse any variant of X")
343343

344344
// This is needed because deserializer is moved
345345
let value = serde_json::Value::deserialize(deserializer)?;
346-
346+
347347
match <HashMap<String, serde_json::Value>>::deserialize(value.clone()) {
348348
Ok(deps) => Ok(Dependencies(
349-
deps.into_iter().map(|(id, json)| {
350-
// Shorthand is just "[mod.id]": "[version]"
351-
match parse_comparable_version(json.clone()) {
352-
Ok(version) => Ok(Dependency {
353-
id: id.clone(),
354-
version,
355-
importance: DependencyImportance::Required,
356-
platforms: all_platforms(),
357-
}),
358-
// Longhand is "[mod.id]": { ... }
359-
Err(_) => Dependency::deserialize(json)
360-
// The ID isn't parsed from the object itself but is the key
361-
.map(|mut d| { d.id.clone_from(&id); d })
362-
.map_err(D::Error::custom)
363-
}.map(|r| (id, r))
364-
}).collect::<Result<_, _>>()?
349+
deps.into_iter()
350+
.map(|(id, json)| {
351+
// Shorthand is just "[mod.id]": "[version]"
352+
match parse_comparable_version(json.clone()) {
353+
Ok(version) => Ok(Dependency {
354+
id: id.clone(),
355+
version,
356+
importance: DependencyImportance::Required,
357+
platforms: all_platforms(),
358+
}),
359+
// Longhand is "[mod.id]": { ... }
360+
Err(_) => Dependency::deserialize(json)
361+
// The ID isn't parsed from the object itself but is the key
362+
.map(|mut d| {
363+
d.id.clone_from(&id);
364+
d
365+
})
366+
.map_err(D::Error::custom),
367+
}
368+
.map(|r| (id, r))
369+
})
370+
.collect::<Result<_, _>>()?,
365371
)),
366372
Err(e) => {
367373
// Can be removed after Geode hits v5
368374
match <Vec<LegacyDependency>>::deserialize(value) {
369375
Ok(deps) => {
370376
let mut res = Dependencies::default();
371377
for dep in deps {
372-
res.0.insert(dep.id.clone(), Dependency {
373-
id: dep.id,
374-
version: dep.version,
375-
importance: dep.importance,
376-
platforms: dep.platforms
377-
});
378+
res.0.insert(
379+
dep.id.clone(),
380+
Dependency {
381+
id: dep.id,
382+
version: dep.version,
383+
importance: dep.importance,
384+
platforms: dep.platforms,
385+
},
386+
);
378387
}
379388
Ok(res)
380389
}
381-
Err(_) => Err(D::Error::custom(e))
390+
Err(_) => Err(D::Error::custom(e)),
382391
}
383392
}
384393
}

0 commit comments

Comments
 (0)