import io, re

NL = chr(10)


def drop_item(path, name, kind='fn'):
    s = io.open(path, encoding='utf-8').read()
    lines = s.split(NL)
    pat = re.compile(r'^(pub )?' + kind + r' ' + re.escape(name) + r'\b')
    hit = next((i for i, l in enumerate(lines) if pat.match(l)), None)
    assert hit is not None, 'not found: %s %s in %s' % (kind, name, path)
    start = hit
    while start > 0 and (lines[start - 1].startswith('///') or lines[start - 1].startswith('//')
                         or lines[start - 1].startswith('#[')):
        start -= 1
    j = hit
    while j < len(lines) and lines[j] != '}':
        j += 1
    del lines[start:j + 1]
    s = NL.join(lines)
    s = re.sub(NL + '{3,}', NL + NL, s)
    io.open(path, 'w', encoding='utf-8', newline=NL).write(s)
    print('  dropped %s %s from %s' % (kind, name, path.rsplit(chr(92), 1)[-1]))


MAIN = r'C:\Users\decid\Documents\projects\spt-pacer-tool\src\main.rs'
DIGEST = r'C:\Users\decid\Documents\projects\spt-pacer-tool\src\digest.rs'

drop_item(MAIN, 'owes_education')
drop_item(DIGEST, 'boundaries')

# the two now-unused locals
s = io.open(MAIN, encoding='utf-8').read()
lines = s.split(NL)
out = []
skip_next_blank = False
for i, l in enumerate(lines):
    if 'let mut seen_boundaries' in l or 'let mut seeded' in l:
        continue
    # their explanatory comments go with them
    if 'False until the first digest read has seeded the boundary set' in l:
        continue
    if "`owes_education`): pre-existing boundaries must not fire education." in l:
        continue
    out.append(l)
s = NL.join(out)
s = re.sub(NL + '{3,}', NL + NL, s)
io.open(MAIN, 'w', encoding='utf-8', newline=NL).write(s)
print('  dropped seen_boundaries / seeded locals')
