@@ -4,6 +4,10 @@ use serde::{Deserialize, Serialize};
44use std:: fs;
55use std:: path:: { Path , PathBuf } ;
66
7+ // Embed the default recipes so the installed binary works even when the
8+ // `recipes/default.yml` file isn't present on the user's current working dir.
9+ const DEFAULT_RECIPES : & str = include_str ! ( "../recipes/default.yml" ) ;
10+
711#[ derive( Debug , Clone , Serialize , Deserialize ) ]
812pub struct RecipeFile {
913 pub rules : Vec < Rule > ,
@@ -24,15 +28,30 @@ fn default_true() -> bool { true }
2428
2529impl RecipeFile {
2630 pub fn load ( path : Option < PathBuf > ) -> Result < Self > {
27- let path = match path {
28- Some ( p) => p,
29- None => default_recipes_path ( ) ,
30- } ;
31- let content = fs:: read_to_string ( & path)
32- . with_context ( || format ! ( "Failed to read recipes at {}" , path. display( ) ) ) ?;
33- let rf: RecipeFile = serde_yaml:: from_str ( & content)
34- . with_context ( || format ! ( "Failed to parse YAML recipes at {}" , path. display( ) ) ) ?;
35- Ok ( rf)
31+ match path {
32+ Some ( p) => {
33+ let content = fs:: read_to_string ( & p)
34+ . with_context ( || format ! ( "Failed to read recipes at {}" , p. display( ) ) ) ?;
35+ let rf: RecipeFile = serde_yaml:: from_str ( & content)
36+ . with_context ( || format ! ( "Failed to parse YAML recipes at {}" , p. display( ) ) ) ?;
37+ Ok ( rf)
38+ }
39+ None => {
40+ let local = default_recipes_path ( ) ;
41+ if local. exists ( ) {
42+ let content = fs:: read_to_string ( & local)
43+ . with_context ( || format ! ( "Failed to read recipes at {}" , local. display( ) ) ) ?;
44+ let rf: RecipeFile = serde_yaml:: from_str ( & content)
45+ . with_context ( || format ! ( "Failed to parse YAML recipes at {}" , local. display( ) ) ) ?;
46+ Ok ( rf)
47+ } else {
48+ // Fallback to embedded default recipes compiled into the binary
49+ let rf: RecipeFile = serde_yaml:: from_str ( DEFAULT_RECIPES )
50+ . with_context ( || "Failed to parse embedded default recipes" ) ?;
51+ Ok ( rf)
52+ }
53+ }
54+ }
3655 }
3756
3857 pub fn compile_globset ( & self ) -> Result < GlobSet > {
0 commit comments