File: src_writer.rs

package info (click to toggle)
rust-ssh2-config 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 404 kB
  • sloc: makefile: 2
file content (109 lines) | stat: -rw-r--r-- 3,017 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::io::Write as _;
use std::path::PathBuf;

use crate::openssh::MyPrefs;

pub fn write_source(prefs: MyPrefs) -> anyhow::Result<()> {
    let SrcPaths { src_dir, src_path } = src_path();

    // create dir
    if !src_dir.exists() {
        std::fs::create_dir_all(&src_dir)?;
    }

    // open file
    let mut file = std::fs::File::create(src_path)?;

    writeln!(
        file,
        r#"//! This file is autogenerated at build-time when `RELOAD_SSH_ALGO` is set to environment."#
    )?;
    writeln!(file)?;

    writeln!(file, "use crate::DefaultAlgorithms;")?;
    writeln!(file,)?;

    writeln!(file, r#"/// Default algorithms for ssh."#)?;
    writeln!(file, r#"///"#)?;
    write_algos_in_docs(&mut file, "Ciphers algorithms", &prefs.ciphers)?;
    write_algos_in_docs(
        &mut file,
        "Key Exchange (KEX) algorithms",
        &prefs.kex_algorithms,
    )?;
    write_algos_in_docs(&mut file, "Host Key algorithms", &prefs.host_key_algorithms)?;
    write_algos_in_docs(
        &mut file,
        "Message Authentication Code (MAC) algorithms",
        &prefs.mac,
    )?;
    write_algos_in_docs(
        &mut file,
        "CA Signature algorithms",
        &prefs.ca_signature_algorithms,
    )?;
    write_algos_in_docs(
        &mut file,
        "Public Key Accepted algorithms",
        &prefs.pubkey_accepted_algorithms,
    )?;
    writeln!(file, r#"pub fn defaults() -> DefaultAlgorithms {{"#)?;
    writeln!(file, r#"    DefaultAlgorithms {{"#)?;
    write_vec(
        &mut file,
        "ca_signature_algorithms",
        &prefs.ca_signature_algorithms,
    )?;
    write_vec(&mut file, "ciphers", &prefs.ciphers)?;
    write_vec(&mut file, "host_key_algorithms", &prefs.host_key_algorithms)?;
    write_vec(&mut file, "kex_algorithms", &prefs.kex_algorithms)?;
    write_vec(&mut file, "mac", &prefs.mac)?;
    write_vec(
        &mut file,
        "pubkey_accepted_algorithms",
        &prefs.pubkey_accepted_algorithms,
    )?;
    writeln!(file, r#"    }}"#)?;
    writeln!(file, r#"}}"#)?;

    Ok(())
}

fn write_vec(file: &mut std::fs::File, name: &str, vec: &[String]) -> anyhow::Result<()> {
    writeln!(file, r#"        {name}: vec!["#)?;
    for item in vec {
        writeln!(file, r#"            "{item}".to_string(),"#,)?;
    }
    writeln!(file, r#"        ],"#)?;
    Ok(())
}

/// Writes algorithms in documentation format
fn write_algos_in_docs(
    file: &mut std::fs::File,
    group: &str,
    algos: &[String],
) -> anyhow::Result<()> {
    writeln!(file, "/// ## {group}")?;
    writeln!(file, "///")?;
    for algo in algos {
        writeln!(file, "/// - `{algo}`", algo = algo.trim_matches('"'))?;
    }
    writeln!(file, "///")?;

    Ok(())
}

struct SrcPaths {
    src_dir: PathBuf,
    src_path: PathBuf,
}

fn src_path() -> SrcPaths {
    let src_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("src")
        .join("default_algorithms");
    let src_path = src_dir.join("openssh.rs");

    SrcPaths { src_dir, src_path }
}