-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrait_basic.rs
68 lines (56 loc) · 1.45 KB
/
trait_basic.rs
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
#![allow(unused)]
struct Solidity {
version: String,
}
struct Vyper {
version: String,
}
trait Compiler {
fn compile(&self, file_path: &str) -> String;
}
trait Test {
fn test(&self, file_path: &str) -> String {
// Default implementation
format!("test {file_path}")
}
}
impl Compiler for Solidity {
fn compile(&self, file_path: &str) -> String {
format!("solc (version: {}) {}", self.version, file_path)
}
}
impl Compiler for Vyper {
fn compile(&self, file_path: &str) -> String {
format!("vyper (version: {}) {}", self.version, file_path)
}
}
impl Test for Solidity {
fn test(&self, file_path: &str) -> String {
format!("forge test {}", file_path)
}
}
impl Test for Vyper {
fn test(&self, file_path: &str) -> String {
format!("hardhat {}", file_path)
}
}
// Input lang is any type that implements the Compiler trait
fn compile(lang: &impl Compiler, file_path: &str) -> String {
lang.compile(file_path)
}
// Input lang is any type that implements the Test trait
fn test(lang: &impl Test, file_path: &str) -> String {
lang.test(file_path)
}
fn main() {
let sol = Solidity {
version: "0.8".to_string(),
};
let vy = Vyper {
version: "0.4".to_string(),
};
println!("{}", compile(&sol, "Hello.sol"));
println!("{}", compile(&vy, "Hello.vy"));
println!("{}", test(&sol, "Hello.sol"));
println!("{}", test(&vy, "Hello.vy"));
}