kicad_monkey_core/pcb/
view_edits.rs1use super::*;
3
4impl PcbView<'_> {
5 pub fn remove_top_level_by_id(&self, identifier: &str) -> Result<PcbEdit, Error> {
7 if self.source.len() > self.limits.max_output_bytes {
8 return Err(output_limit_error());
9 }
10 if identifier.is_empty() {
11 return Err(source_error(
12 "PCB object identifier cannot be empty",
13 self.root.start,
14 ));
15 }
16 let mut matches = Vec::new();
17 for span in &self.top_level {
18 if top_level_identifier(self.source, span, self.limits)?.as_deref() == Some(identifier)
19 {
20 matches.push(span);
21 }
22 }
23 match matches.as_slice() {
24 [] => Ok(PcbEdit {
25 source: self.source.to_owned(),
26 changed: false,
27 }),
28 [span] => Ok(PcbEdit {
29 source: apply_patches_with_limit(
30 self.source,
31 &[Patch::new(span.range.start, span.range.end, "")],
32 self.limits.max_output_bytes,
33 )?,
34 changed: true,
35 }),
36 _ => Err(source_error(
37 "PCB object identifier is ambiguous",
38 self.root.start,
39 )),
40 }
41 }
42
43 pub fn set_property(&self, name: &str, value: &str) -> Result<PcbEdit, Error> {
45 if self.source.len() > self.limits.max_output_bytes {
46 return Err(output_limit_error());
47 }
48 let matches = self.matching_properties(name)?;
49 let [property] = matches.as_slice() else {
50 return Err(source_error(
51 if matches.is_empty() {
52 "PCB property was not found"
53 } else {
54 "PCB property name is ambiguous"
55 },
56 self.root.start,
57 ));
58 };
59 if property.value == value {
60 return Ok(PcbEdit {
61 source: self.source.to_owned(),
62 changed: false,
63 });
64 }
65 let replacement = build_with_limit(
66 &Sexp::Quoted(value.to_owned()),
67 self.limits.max_output_bytes,
68 )?;
69 let source = apply_patches_with_limit(
70 self.source,
71 &[Patch::new(
72 property.value_range.start,
73 property.value_range.end,
74 replacement,
75 )],
76 self.limits.max_output_bytes,
77 )?;
78 Ok(PcbEdit {
79 source,
80 changed: true,
81 })
82 }
83
84 pub fn upsert_property(&self, name: &str, value: &str) -> Result<PcbEdit, Error> {
86 let matches = self.matching_properties(name)?;
87 match matches.as_slice() {
88 [_] => self.set_property(name, value),
89 [] => self.insert_property(name, value),
90 _ => Err(source_error(
91 "PCB property name is ambiguous",
92 self.root.start,
93 )),
94 }
95 }
96
97 pub fn remove_property(&self, name: &str) -> Result<PcbEdit, Error> {
99 if self.source.len() > self.limits.max_output_bytes {
100 return Err(output_limit_error());
101 }
102 let matches = self.matching_properties(name)?;
103 match matches.as_slice() {
104 [] => Ok(PcbEdit {
105 source: self.source.to_owned(),
106 changed: false,
107 }),
108 [property] => Ok(PcbEdit {
109 source: apply_patches_with_limit(
110 self.source,
111 &[Patch::new(
112 property.source_range.start,
113 property.source_range.end,
114 "",
115 )],
116 self.limits.max_output_bytes,
117 )?,
118 changed: true,
119 }),
120 _ => Err(source_error(
121 "PCB property name is ambiguous",
122 self.root.start,
123 )),
124 }
125 }
126
127 fn matching_properties(&self, name: &str) -> Result<Vec<PcbProperty>, Error> {
128 self.top_level
129 .iter()
130 .filter(|span| span.head.as_deref() == Some("property"))
131 .map(|span| property_from_span(self.source, span))
132 .filter_map(|property| match property {
133 Ok(property) if property.name == name => Some(Ok(property)),
134 Ok(_) => None,
135 Err(error) => Some(Err(error)),
136 })
137 .collect()
138 }
139
140 fn insert_property(&self, name: &str, value: &str) -> Result<PcbEdit, Error> {
141 let property_count = self
142 .top_level
143 .iter()
144 .filter(|span| span.head.as_deref() == Some("property"))
145 .count();
146 if property_count >= self.limits.max_properties {
147 return Err(limit_error());
148 }
149 let form = build_with_limit(
150 &Sexp::List(vec![
151 Sexp::Atom("property".to_owned()),
152 Sexp::Quoted(name.to_owned()),
153 Sexp::Quoted(value.to_owned()),
154 ]),
155 self.limits.max_output_bytes,
156 )?;
157 let offset = self.root.range.end.saturating_sub(1);
158 let newline = if self.source.contains("\r\n") {
159 "\r\n"
160 } else {
161 "\n"
162 };
163 let prefix =
164 if self.source[..offset].ends_with('\n') || self.source[..offset].ends_with('\r') {
165 ""
166 } else {
167 newline
168 };
169 let replacement = format!("{prefix} {form}{newline}");
170 Ok(PcbEdit {
171 source: apply_patches_with_limit(
172 self.source,
173 &[Patch::new(offset, offset, replacement)],
174 self.limits.max_output_bytes,
175 )?,
176 changed: true,
177 })
178 }
179}