Skip to content

Commit 3b79e55

Browse files
committed
Add clippy cfg to simple-sample
1 parent 00c8d2b commit 3b79e55

File tree

7 files changed

+113
-74
lines changed

7 files changed

+113
-74
lines changed

Diff for: examples/simple-sample/Cargo.toml

+40
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,43 @@ cdkam_ansible.path = "../cdkam_ansible"
1414
anyhow.workspace = true
1515
serde.workspace = true
1616
serde_json.workspace = true
17+
18+
[lints.clippy]
19+
20+
# category
21+
#
22+
pedantic = { level = "warn", priority = -1 }
23+
nursery = { level = "warn", priority = -1 }
24+
restriction = { level = "warn", priority = -1 }
25+
# cargo = "warn"
26+
27+
# style
28+
#
29+
get_first = "allow"
30+
31+
# nursery
32+
#
33+
branches_sharing_code = "allow"
34+
35+
# restriction
36+
#
37+
# https://rust-lang.github.io/rust-clippy/master/index.html#blanket_clippy_restriction_lints
38+
blanket_clippy_restriction_lints = "allow"
39+
absolute_paths = "allow"
40+
arbitrary_source_item_ordering = "allow"
41+
dbg_macro = "allow"
42+
implicit_return = "allow"
43+
min_ident_chars = "allow"
44+
missing_docs_in_private_items = "allow"
45+
missing-errors-doc = "allow"
46+
mod_module_files = "allow"
47+
# FIXME: later
48+
print_stdout = "allow"
49+
pub_with_shorthand = "allow"
50+
question_mark_used = "allow"
51+
self_named_module_files = "allow"
52+
shadow_reuse = "allow"
53+
shadow_same = "allow"
54+
shadow_unrelated = "allow"
55+
single_call_fn = "allow"
56+
single_char_lifetime_names = "allow"

Diff for: examples/simple-sample/src/inventory/mod.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
use anyhow::{bail, Result};
22
use cdk_ansible::{Child, Inventory, InventoryRoot, OptU};
3-
4-
#[derive(Debug, Clone, PartialEq, Eq)]
5-
pub enum HostName {
6-
HostA,
7-
}
8-
93
use cdk_ansible_macro::FieldCount;
104

115
#[derive(Default, Debug, Clone, PartialEq, Eq, FieldCount)]
@@ -25,18 +19,18 @@ impl Hosts {
2519
///
2620
pub fn to_inventory(&self) -> Result<Inventory> {
2721
let debian_ansible_python_interpreter_tuple = (
28-
"ansible_python_interpreter".to_string(),
29-
serde_json::Value::String("/usr/bin/python3".to_string()),
22+
"ansible_python_interpreter".to_owned(),
23+
serde_json::Value::String("/usr/bin/python3".to_owned()),
3024
);
3125
let inventory = Inventory {
32-
name: "inventory".to_string(),
26+
name: "inventory".to_owned(),
3327
root: InventoryRoot {
3428
all: Child {
3529
hosts: OptU::Some(
3630
vec![(
3731
self.host_a.fqdn.clone(),
3832
Some(
39-
vec![debian_ansible_python_interpreter_tuple.clone()]
33+
vec![debian_ansible_python_interpreter_tuple]
4034
.into_iter()
4135
.collect::<serde_json::Map<String, serde_json::Value>>(),
4236
),
@@ -50,14 +44,12 @@ impl Hosts {
5044
},
5145
};
5246

53-
let hosts = if let OptU::Some(hosts) = &inventory.root.all.hosts {
54-
hosts.clone()
55-
} else {
47+
let OptU::Some(hosts) = inventory.root.all.hosts.clone() else {
5648
bail!("hosts is not set");
5749
};
5850

5951
// Validate: count Hosts' attributes
60-
if Hosts::field_count() != hosts.len() {
52+
if hosts.len() == Self::field_count() {
6153
bail!("Some hosts are not set");
6254
}
6355

@@ -71,10 +63,11 @@ pub struct HostA {
7163
pub fqdn: String,
7264
}
7365

66+
#[expect(clippy::unnecessary_wraps, reason = "use anyhow::Result interface")]
7467
pub fn get_hosts() -> Result<Hosts> {
7568
Ok(Hosts {
7669
host_a: {
77-
let name = "host-a".to_string();
70+
let name = "host-a".to_owned();
7871
HostA {
7972
name: name.clone(),
8073
fqdn: format!("{}.example.com", &name),

Diff for: examples/simple-sample/src/lib.rs

+37-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,49 @@
1+
use anyhow::Result;
12
use cdk_ansible::settings;
3+
use cdk_ansible::{run, settings::SynthSettings, Synthesizer};
4+
use cdk_ansible::{Inventory, Playbook};
25

3-
pub mod inventory;
4-
pub mod playbooks;
6+
mod inventory;
7+
mod playbooks;
58

6-
pub trait PlaybookGenArgs {
9+
use inventory::get_hosts;
10+
use playbooks::generate_all;
11+
12+
#[inline]
13+
pub fn main() -> Result<()> {
14+
run(std::env::args_os(), CustomSynthesizer {})?;
15+
Ok(())
16+
}
17+
18+
struct CustomSynthesizer;
19+
20+
impl Synthesizer for CustomSynthesizer {
21+
fn synth_playbooks(&self, args: &SynthSettings) -> Result<Vec<Playbook>> {
22+
let playbooks = generate_all(&PlaybookSynthConfig {
23+
synth_settings: args.clone(),
24+
hosts: get_hosts()?,
25+
})?;
26+
Ok(playbooks)
27+
}
28+
29+
fn synth_inventory(&self, args: &SynthSettings) -> Result<Inventory> {
30+
dbg!(&args); // FIXME: remove this
31+
let hosts = get_hosts()?;
32+
let inventory = hosts.to_inventory()?;
33+
Ok(inventory)
34+
}
35+
}
36+
37+
trait PlaybookGenArgs {
38+
#[expect(dead_code, reason = "not used yet")]
739
fn get_synth_settings(&self) -> &settings::SynthSettings;
840
fn get_hosts(&self) -> &inventory::Hosts;
941
}
1042

1143
/// A simple struct to implement [`PlaybookGenArgs`]
1244
#[derive(Debug, Clone)]
13-
pub struct PlaybookSynthConfig {
45+
struct PlaybookSynthConfig {
46+
#[expect(dead_code, reason = "not used yet")]
1447
pub synth_settings: settings::SynthSettings,
1548
pub hosts: inventory::Hosts,
1649
}

Diff for: examples/simple-sample/src/main.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
11
use anyhow::Result;
2-
use cdk_ansible::{run, settings::SynthSettings, Synthesizer};
3-
use cdk_ansible::{Inventory, Playbook};
4-
use simple_sample::inventory::get_hosts;
5-
use simple_sample::playbooks::generate_all_playbooks;
6-
use simple_sample::PlaybookSynthConfig;
7-
8-
struct CustomSynthesizer {}
9-
10-
impl Synthesizer for CustomSynthesizer {
11-
fn synth_playbooks(&self, synth_settings: &SynthSettings) -> Result<Vec<Playbook>> {
12-
let playbook_gen_config = PlaybookSynthConfig {
13-
synth_settings: synth_settings.clone(),
14-
hosts: get_hosts()?,
15-
};
16-
let playbooks = generate_all_playbooks(&playbook_gen_config)?;
17-
Ok(playbooks)
18-
}
19-
20-
fn synth_inventory(&self, synth_settings: &SynthSettings) -> Result<Inventory> {
21-
dbg!(&synth_settings);
22-
let hosts = get_hosts()?;
23-
let inventory = hosts.to_inventory()?;
24-
Ok(inventory)
25-
}
26-
}
2+
use simple_sample::main as simple_sample_main;
273

284
fn main() -> Result<()> {
29-
run(std::env::args_os(), CustomSynthesizer {})?;
30-
Ok(())
5+
simple_sample_main()
316
}

Diff for: examples/simple-sample/src/playbooks/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod playbook2;
77
use crate::PlaybookGenArgs;
88

99
/// Generate all playbooks and return them
10-
pub fn generate_all_playbooks(args: &impl PlaybookGenArgs) -> Result<Vec<Playbook>> {
10+
pub fn generate_all<T: PlaybookGenArgs>(args: &T) -> Result<Vec<Playbook>> {
1111
let playbooks = vec![playbook1::playbook1(args)?, playbook2::playbook2(args)?];
1212

1313
// validate playbook names are unique
@@ -37,15 +37,18 @@ mod test {
3737
fn test_validate_playbook_names() {
3838
let playbooks = vec![
3939
Playbook {
40-
name: "playbook".to_string(),
40+
name: "playbook".to_owned(),
4141
plays: vec![],
4242
},
4343
Playbook {
44-
name: "playbook".to_string(),
44+
name: "playbook".to_owned(),
4545
plays: vec![],
4646
},
4747
];
48-
let err = validate_playbook_names(&playbooks).unwrap_err();
49-
assert_eq!(err.to_string(), "Playbook names are not unique");
48+
#[expect(clippy::unreachable, reason = "should be an error")]
49+
match validate_playbook_names(&playbooks) {
50+
Ok(()) => unreachable!("should be an error"),
51+
Err(e) => assert_eq!(e.to_string(), "Playbook names are not unique"),
52+
}
5053
}
5154
}

Diff for: examples/simple-sample/src/playbooks/playbook1.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,33 @@ use cdk_ansible::{OptU, Play, PlayOptions, Playbook, Task, TaskOptions};
33

44
use crate::playbooks::PlaybookGenArgs;
55

6-
pub fn playbook1(args: &impl PlaybookGenArgs) -> Result<Playbook> {
6+
#[expect(clippy::unnecessary_wraps, reason = "use anyhow::Result interface")]
7+
pub fn playbook1<T: PlaybookGenArgs>(args: &T) -> Result<Playbook> {
78
Ok(Playbook {
89
// Save as `playbook1.json`
9-
name: "playbook1".to_string(),
10+
name: "playbook1".to_owned(),
1011
plays: vec![Play {
11-
name: "Debug".to_string(),
12+
name: "Debug".to_owned(),
1213
hosts: vec![args.get_hosts().host_a.fqdn.clone()],
1314
tasks: vec![
1415
Task {
15-
name: "Debug msg".to_string(),
16-
options: TaskOptions {
17-
..Default::default()
18-
},
16+
name: "Debug msg".to_owned(),
17+
options: TaskOptions::default(),
1918
command: Box::new(cdkam_ansible::builtin::debug::Module {
2019
module: cdkam_ansible::builtin::debug::Args {
2120
options: cdkam_ansible::builtin::debug::Opt {
22-
msg: OptU::Some("msg".to_string()),
21+
msg: OptU::Some("msg".to_owned()),
2322
..Default::default()
2423
},
2524
},
2625
}),
2726
},
2827
Task {
29-
name: "No options' module".to_string(),
30-
options: TaskOptions {
31-
..Default::default()
32-
},
28+
name: "No options' module".to_owned(),
29+
options: TaskOptions::default(),
3330
command: Box::new(cdkam_ansible::builtin::service_facts::Module {
3431
module: cdkam_ansible::builtin::service_facts::Args {
35-
options: Default::default(),
32+
options: cdkam_ansible::builtin::service_facts::Opt::default(),
3633
},
3734
}),
3835
},

Diff for: examples/simple-sample/src/playbooks/playbook2.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1+
use crate::playbooks::PlaybookGenArgs;
12
use anyhow::Result;
23
use cdk_ansible::{OptU, Play, PlayOptions, Playbook, Task, TaskOptions};
34

4-
use crate::playbooks::PlaybookGenArgs;
5-
6-
pub fn playbook2(args: &impl PlaybookGenArgs) -> Result<Playbook> {
5+
#[expect(clippy::unnecessary_wraps, reason = "use anyhow::Result interface")]
6+
pub fn playbook2<T: PlaybookGenArgs>(args: &T) -> Result<Playbook> {
77
Ok(Playbook {
88
// Save as `playbook2.json`
9-
name: "playbook2".to_string(),
9+
name: "playbook2".to_owned(),
1010
plays: vec![Play {
11-
name: "Debug".to_string(),
11+
name: "Debug".to_owned(),
1212
hosts: vec![args.get_hosts().host_a.fqdn.clone()],
1313
tasks: vec![Task {
14-
name: "Debug msg".to_string(),
15-
options: TaskOptions {
16-
..Default::default()
17-
},
14+
name: "Debug msg".to_owned(),
15+
options: TaskOptions::default(),
1816
command: Box::new(cdkam_ansible::builtin::debug::Module {
1917
module: cdkam_ansible::builtin::debug::Args {
2018
options: cdkam_ansible::builtin::debug::Opt {
21-
msg: OptU::Some("msg".to_string()),
19+
msg: OptU::Some("msg".to_owned()),
2220
..Default::default()
2321
},
2422
},

0 commit comments

Comments
 (0)